1c8f2230153baf56eaa928483c3f4753a0213f8c
[librarian.git] / src / librarian / document.py
1 import gettext
2 import os
3 import re
4 from urllib.request import urlopen
5 from lxml import etree
6 from .parser import parser
7 from . import dcparser, DCNS
8 from .functions import lang_code_3to2
9
10
11 class WLDocument:
12     def __init__(self, filename=None, url=None):
13         source = filename or 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     @property
22     def meta(self):
23         # Allow metadata of the master element as document meta.
24         #master = self.tree.getroot()[-1]
25         return self.tree.getroot().meta
26         return master.meta
27
28     def build(self, builder, **kwargs):
29         return builder().build(self, **kwargs)
30
31     def _compat_assign_ordered_ids(self):
32         """
33         Compatibility: ids in document order, to be roughly compatible with legacy
34         footnote ids. Just for testing consistency, change to some sane identifiers
35         at convenience.
36         """
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)
40             i += 1
41             if getattr(elem, 'HTML_CLASS', None) == 'stanza':
42                 if elem.text:
43                     i += len(EXPR.split(elem.text)) - 1
44                 for sub in elem:
45                     i = _compat_assign_ordered_ids_in_elem(sub, i)
46                     if sub.tail:
47                         i += len(EXPR.split(sub.tail)) - 1
48             else:
49                 if elem.tag in ('uwaga', 'extra'):
50                     return i
51                 for sub in elem:
52                     i = _compat_assign_ordered_ids_in_elem(sub, i)
53             return i
54
55         _compat_assign_ordered_ids_in_elem(self.tree.getroot(), 4)
56
57     def _compat_assign_section_ids(self):
58         """
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.
61         """
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)
68