0dbb14f16ca556dfa69691351d3bb1fb045902c3
[librarian.git] / src / librarian / document.py
1 import gettext
2 import os
3 import re
4 from lxml import etree
5 import six
6 from .parser import parser
7 from . import dcparser, DCNS, DirDocProvider
8 from .functions import lang_code_3to2
9
10
11 class WLDocument:
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)
15         self.tree = tree
16         tree.getroot().document = self
17         self.base_meta = dcparser.BookInfo({}, {
18             DCNS('language'): ["pol"],
19         }, validate_required=False)
20
21         self.provider = provider if provider is not None else DirDocProvider('.')
22
23     @property
24     def meta(self):
25         # Allow metadata of the master element as document meta.
26         #master = self.tree.getroot()[-1]
27         return self.tree.getroot().meta
28         return master.meta
29
30     @property
31     def children(self):
32         for part_uri in self.meta.parts or []:
33             yield type(self)(
34                 filename=self.provider.by_uri(part_uri),
35                 provider=self.provider
36             )
37             
38     
39     def build(self, builder, **kwargs):
40         return builder().build(self, **kwargs)
41
42     def _compat_assign_ordered_ids(self):
43         """
44         Compatibility: ids in document order, to be roughly compatible with legacy
45         footnote ids. Just for testing consistency, change to some sane identifiers
46         at convenience.
47         """
48         EXPR = re.compile(r'/\s', re.MULTILINE | re.UNICODE)
49         def _compat_assign_ordered_ids_in_elem(elem, i):
50             elem.attrib['_compat_ordered_id'] = str(i)
51             i += 1
52             if getattr(elem, 'HTML_CLASS', None) == 'stanza':
53                 if elem.text:
54                     i += len(EXPR.split(elem.text)) - 1
55                 for sub in elem:
56                     i = _compat_assign_ordered_ids_in_elem(sub, i)
57                     if sub.tail:
58                         i += len(EXPR.split(sub.tail)) - 1
59             else:
60                 if elem.tag in ('uwaga', 'extra'):
61                     return i
62                 for sub in elem:
63                     i = _compat_assign_ordered_ids_in_elem(sub, i)
64             return i
65
66         _compat_assign_ordered_ids_in_elem(self.tree.getroot(), 4)
67
68     def _compat_assign_section_ids(self):
69         """
70         Ids in master-section order. These need to be compatible with the
71         #secN anchors used by WL search results page to link to fragments.
72         """
73         def _compat_assigns_section_ids_in_elem(elem, prefix='sec'):
74             for i, child in enumerate(elem):
75                 idfier = '{}{}'.format(prefix, i + 1)
76                 child.attrib['_compat_section_id'] = idfier
77                 _compat_assigns_section_ids_in_elem(child, idfier + '-')
78         _compat_assigns_section_ids_in_elem(self.tree.getroot().master)
79
80
81     def editors(self):
82         persons = set(self.meta.editors
83                       + self.meta.technical_editors)
84         #for child in self.parts():
85         #    persons.update(child.editors())
86         #if None in persons:
87         #    persons.remove(None)
88         return persons
89