6 from .parser import parser
7 from . import dcparser, DCNS, DirDocProvider
8 from .functions import lang_code_3to2
12 def __init__(self, filename=None, url=None, provider=None):
13 source = filename or six.moves.urllib.request.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)
21 self.provider = provider if provider is not None else DirDocProvider('.')
23 self.tree.getroot().validate()
27 # Allow metadata of the master element as document meta.
28 #master = self.tree.getroot()[-1]
29 return self.tree.getroot().meta
34 for part_uri in self.meta.parts or []:
36 filename=self.provider.by_uri(part_uri),
37 provider=self.provider
41 def build(self, builder, base_url=None, **kwargs):
42 return builder(base_url=base_url).build(self, **kwargs)
44 def _compat_assign_ordered_ids(self):
46 Compatibility: ids in document order, to be roughly compatible with legacy
47 footnote ids. Just for testing consistency, change to some sane identifiers
50 EXPR = re.compile(r'/\s', re.MULTILINE | re.UNICODE)
51 def _compat_assign_ordered_ids_in_elem(elem, i):
52 elem.attrib['_compat_ordered_id'] = str(i)
54 if getattr(elem, 'HTML_CLASS', None) == 'stanza':
56 i += len(EXPR.split(elem.text)) - 1
58 i = _compat_assign_ordered_ids_in_elem(sub, i)
60 i += len(EXPR.split(sub.tail)) - 1
62 if elem.tag in ('uwaga', 'extra'):
65 i = _compat_assign_ordered_ids_in_elem(sub, i)
68 _compat_assign_ordered_ids_in_elem(self.tree.getroot(), 4)
70 def _compat_assign_section_ids(self):
72 Ids in master-section order. These need to be compatible with the
73 #secN anchors used by WL search results page to link to fragments.
75 def _compat_assigns_section_ids_in_elem(elem, prefix='sec'):
76 for i, child in enumerate(elem):
77 idfier = '{}{}'.format(prefix, i + 1)
78 child.attrib['_compat_section_id'] = idfier
79 _compat_assigns_section_ids_in_elem(child, idfier + '-')
80 _compat_assigns_section_ids_in_elem(self.tree.getroot().master)
84 persons = set(self.meta.editors
85 + self.meta.technical_editors)
86 for child in self.children:
87 persons.update(child.editors())