New EPUB builder, other minor changes.
[librarian.git] / src / librarian / elements / footnotes / __init__.py
1 # -*- coding: utf-8
2 from __future__ import unicode_literals
3
4 from ..base import WLElement
5
6
7 class Footnote(WLElement):
8     def txt_build(self, builder):
9         pass
10
11     def html_build(self, builder):
12         if not builder.with_footnotes:
13             return
14
15         builder.footnote_counter += 1
16         fn_no = builder.footnote_counter
17         footnote_id = 'footnote-idm{}'.format(self.attrib['_compat_ordered_id'])
18         anchor_id = 'anchor-idm{}'.format(self.attrib['_compat_ordered_id'])
19
20         # Add anchor.
21         builder.start_element(
22             'a',
23             {
24                 "href": '#{}'.format(footnote_id),
25                 "class": "annotation-anchor",
26                 "id": anchor_id,
27             }
28         )
29         builder.push_text('[{}]'.format(fn_no))
30         builder.end_element()
31         
32         # Add actual footnote.
33         builder.enter_fragment('footnotes')
34         builder.start_element('div', {'class': 'fn-{}'.format(self.tag)})
35         builder.push_text('\n') # Compat
36         builder.start_element('a', {'name': footnote_id})
37         builder.end_element()
38         builder.start_element('a', {
39             'href': '#{}'.format(anchor_id), 'class': 'annotation'
40         })
41         builder.push_text('[{}]'.format(fn_no))
42         builder.end_element()
43
44         builder.start_element('p')
45         super(Footnote, self).html_build(builder)
46
47         builder.push_text(' [{}]'.format(self.qualifier))
48         builder.end_element()
49         builder.end_element()
50         builder.exit_fragment()
51
52     def epub_build(self, builder):
53         fn_no = builder.assign_footnote_number()
54         part_number = getattr(
55             builder,
56             'chunk_counter',
57             1
58         )
59
60         builder.start_element(
61             'a',
62             {
63                 'class': 'anchor',
64                 'id': f'anchor-{fn_no}',
65                 'href': f'annotations.xhtml#annotation-{fn_no}',
66             }
67         )
68         builder.start_element('sup', {})
69         builder.push_text(str(fn_no))
70         builder.end_element()
71         builder.end_element()
72
73         
74         builder.enter_fragment('footnotes')
75         builder.start_element('p', {
76             'id': f'annotation-{fn_no}',
77             'class': "annotation"
78         })
79         builder.start_element('a', {
80             'href': f"part{part_number}.xhtml#anchor-{fn_no}"
81         })
82         builder.push_text(str(fn_no))
83         builder.end_element()
84         builder.push_text('. ')
85
86         super().epub_build(builder)
87         builder.push_text(' [' + self.qualifier + ']')
88         builder.end_element()
89
90         builder.push_text('\n')
91
92         builder.exit_fragment()
93
94         
95
96 class PA(Footnote):
97     """Przypis autorski."""
98     @property
99     def qualifier(self):
100         _ = self.gettext
101         return _("author's footnote")
102
103
104 class PT(Footnote):
105     """Przypis tłumacza."""
106     @property
107     def qualifier(self):
108         _ = self.gettext
109         return _("translator's footnote")
110
111
112 class PR(Footnote):
113     """Przypis redakcyjny."""
114     @property
115     def qualifier(self):
116         _ = self.gettext
117         return _("editor's footnote")
118
119
120 class PE(Footnote):
121     """Przypis redakcji źródła."""
122     @property
123     def qualifier(self):
124         _ = self.gettext
125         return _("source editor's footnote")