Support for block annotations. Option to use endnotes in PDF.
[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     NO_TOC = True
9     START_INLINE = True
10
11     def signal(self, signal):
12         if signal == 'INLINE':
13             self.START_INLINE = False
14         else:
15             super().signal(signal)
16     
17     def txt_build(self, builder):
18         pass
19
20     def html_build(self, builder):
21         if not builder.with_footnotes:
22             return
23
24         builder.footnote_counter += 1
25         fn_no = builder.footnote_counter
26         footnote_id = 'footnote-idm{}'.format(self.attrib['_compat_ordered_id'])
27         anchor_id = 'anchor-idm{}'.format(self.attrib['_compat_ordered_id'])
28
29         # Add anchor.
30         builder.start_element(
31             'a',
32             {
33                 "href": '#{}'.format(footnote_id),
34                 "class": "annotation-anchor",
35                 "id": anchor_id,
36             }
37         )
38         builder.push_text('[{}]'.format(fn_no))
39         builder.end_element()
40         
41         # Add actual footnote.
42         builder.enter_fragment('footnotes')
43         builder.start_element('div', {'class': 'fn-{}'.format(self.tag)})
44         builder.push_text('\n') # Compat
45         builder.start_element('a', {'name': footnote_id})
46         builder.end_element()
47         builder.start_element('a', {
48             'href': '#{}'.format(anchor_id), 'class': 'annotation'
49         })
50         builder.push_text('[{}]'.format(fn_no))
51         builder.end_element()
52
53         builder.start_element('p')
54         super(Footnote, self).html_build(builder)
55
56         builder.push_text(' [{}]'.format(self.qualifier))
57         builder.end_element()
58         builder.end_element()
59         builder.exit_fragment()
60
61     def epub_build(self, builder):
62         fn_no = builder.assign_footnote_number()
63         part_number = getattr(
64             builder,
65             'chunk_counter',
66             1
67         )
68
69         builder.start_element(
70             'a',
71             {
72                 'class': 'anchor',
73                 'id': f'anchor-{fn_no}',
74                 'href': f'annotations.xhtml#annotation-{fn_no}',
75             }
76         )
77         builder.start_element('sup', {})
78         builder.push_text(str(fn_no))
79         builder.end_element()
80         builder.end_element()
81
82         
83         builder.enter_fragment('footnotes')
84         builder.start_element('div', {
85             'id': f'annotation-{fn_no}',
86             'class': "annotation"
87         })
88         builder.start_element('a', {
89             'href': f"part{part_number}.xhtml#anchor-{fn_no}"
90         })
91         builder.push_text(str(fn_no))
92         builder.end_element()
93         builder.push_text('. ')
94
95         super().epub_build(builder)
96         builder.push_text(' [' + self.qualifier + ']')
97         builder.end_element()
98
99         builder.push_text('\n')
100
101         builder.exit_fragment()
102
103         
104
105 class PA(Footnote):
106     """Przypis autorski."""
107     @property
108     def qualifier(self):
109         _ = self.gettext
110         return _("author's footnote")
111
112
113 class PT(Footnote):
114     """Przypis tłumacza."""
115     @property
116     def qualifier(self):
117         _ = self.gettext
118         return _("translator's footnote")
119
120
121 class PR(Footnote):
122     """Przypis redakcyjny."""
123     @property
124     def qualifier(self):
125         _ = self.gettext
126         return _("editor's footnote")
127
128
129 class PE(Footnote):
130     """Przypis redakcji źródła."""
131     @property
132     def qualifier(self):
133         _ = self.gettext
134         return _("source editor's footnote")