6ac284276bc9f0e87e1a0af056dce585bd97957b
[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         self.tree.getroot().validate()
24
25     @property
26     def meta(self):
27         # Allow metadata of the master element as document meta.
28         #master = self.tree.getroot()[-1]
29         return self.tree.getroot().meta
30         return master.meta
31
32     @property
33     def children(self):
34         for part_uri in self.meta.parts or []:
35             yield type(self)(
36                 filename=self.provider.by_slug(part_uri.slug),
37                 provider=self.provider
38             )
39     
40     def build(self, builder, base_url=None, **kwargs):
41         return builder(base_url=base_url).build(self, **kwargs)
42
43     def assign_ids(self, existing=None):
44         # Find all existing IDs.
45         existing = existing or set()
46         que = [self.tree.getroot()]
47         while que:
48             item = que.pop(0)
49             try:
50                 item.normalize_insides()
51             except AttributeError:
52                 pass
53             existing.add(item.attrib.get('id'))
54             que.extend(item)
55
56         i = 1
57         que = [self.tree.getroot()]
58         while que:
59             item = que.pop(0)
60             que.extend(item)
61             if item.attrib.get('id'):
62                 continue
63             if not getattr(item, 'SHOULD_HAVE_ID', False):
64                 continue
65             while f'e{i}' in existing:
66                 i += 1
67             item.attrib['id'] = f'e{i}'
68             i += 1
69     
70     def _compat_assign_ordered_ids(self):
71         """
72         Compatibility: ids in document order, to be roughly compatible with legacy
73         footnote ids. Just for testing consistency, change to some sane identifiers
74         at convenience.
75         """
76         EXPR = re.compile(r'/\s', re.MULTILINE | re.UNICODE)
77         def _compat_assign_ordered_ids_in_elem(elem, i):
78             elem.attrib['_compat_ordered_id'] = str(i)
79             i += 1
80             if getattr(elem, 'HTML_CLASS', None) == 'stanza':
81                 if elem.text:
82                     i += len(EXPR.split(elem.text)) - 1
83                 for sub in elem:
84                     i = _compat_assign_ordered_ids_in_elem(sub, i)
85                     if sub.tail:
86                         i += len(EXPR.split(sub.tail)) - 1
87             else:
88                 if elem.tag in ('uwaga', 'extra'):
89                     return i
90                 for sub in elem:
91                     i = _compat_assign_ordered_ids_in_elem(sub, i)
92             return i
93
94         _compat_assign_ordered_ids_in_elem(self.tree.getroot(), 4)
95
96     def _compat_assign_section_ids(self):
97         """
98         Ids in master-section order. These need to be compatible with the
99         #secN anchors used by WL search results page to link to fragments.
100         """
101         def _compat_assigns_section_ids_in_elem(elem, prefix='sec'):
102             for i, child in enumerate(elem):
103                 idfier = '{}{}'.format(prefix, i + 1)
104                 child.attrib['_compat_section_id'] = idfier
105                 _compat_assigns_section_ids_in_elem(child, idfier + '-')
106         _compat_assigns_section_ids_in_elem(self.tree.getroot().master)
107
108
109     def editors(self):
110         persons = set(self.meta.editors
111                       + self.meta.technical_editors)
112         for child in self.children:
113             persons.update(child.editors())
114         if None in persons:
115             persons.remove(None)
116         return persons
117