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