4 from urllib.request import urlopen
6 from .parser import parser
7 from . import dcparser, DCNS
8 from .functions import lang_code_3to2
12 def __init__(self, filename=None, url=None):
13 source = filename or urlopen(url)
14 tree = etree.parse(source, parser=parser)
16 tree.getroot().document = self
17 self.base_meta = dcparser.BookInfo({}, {
18 DCNS('language'): ["pol"],
19 }, validate_required=False)
23 # Allow metadata of the master element as document meta.
24 #master = self.tree.getroot()[-1]
25 return self.tree.getroot().meta
28 def build(self, builder, **kwargs):
29 return builder().build(self, **kwargs)
31 def _compat_assign_ordered_ids(self):
33 Compatibility: ids in document order, to be roughly compatible with legacy
34 footnote ids. Just for testing consistency, change to some sane identifiers
37 EXPR = re.compile(r'/\s', re.MULTILINE | re.UNICODE)
38 def _compat_assign_ordered_ids_in_elem(elem, i):
39 elem.attrib['_compat_ordered_id'] = str(i)
41 if getattr(elem, 'HTML_CLASS', None) == 'stanza':
43 i += len(EXPR.split(elem.text)) - 1
45 i = _compat_assign_ordered_ids_in_elem(sub, i)
47 i += len(EXPR.split(sub.tail)) - 1
49 if elem.tag in ('uwaga', 'extra'):
52 i = _compat_assign_ordered_ids_in_elem(sub, i)
55 _compat_assign_ordered_ids_in_elem(self.tree.getroot(), 4)
57 def _compat_assign_section_ids(self):
59 Ids in master-section order. These need to be compatible with the
60 #secN anchors used by WL search results page to link to fragments.
62 def _compat_assigns_section_ids_in_elem(elem, prefix='sec'):
63 for i, child in enumerate(elem):
64 idfier = '{}{}'.format(prefix, i + 1)
65 child.attrib['_compat_section_id'] = idfier
66 _compat_assigns_section_ids_in_elem(child, idfier + '-')
67 _compat_assigns_section_ids_in_elem(self.tree.getroot().master)