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