1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
4 from collections import defaultdict
6 from urllib.request import urlopen
8 from librarian.html import add_table_of_contents, add_table_of_themes, add_image_sizes
9 from librarian import OutputFile
15 return self.current_cursors[-1]
17 def enter_fragment(self, fragment):
18 cursor = self.cursors.get(fragment, self.cursor)
19 self.current_cursors.append(cursor)
21 def exit_fragment(self):
22 self.current_cursors.pop()
24 def create_fragment(self, name, element):
25 assert name not in self.cursors
26 self.cursors[name] = element
28 def forget_fragment(self, name):
29 del self.cursors[name]
31 def start_element(self, tag, attrib=None):
32 self.current_cursors.append(etree.SubElement(
38 def end_element(self):
39 self.current_cursors.pop()
41 def push_text(self, text):
44 cursor[-1].tail = (cursor[-1].tail or '') + text
46 cursor.text = (cursor.text or '') + text
48 def simple_element(self, tag, text='', attrib=None):
49 self.start_element(tag, attrib)
54 class HtmlBuilder(TreeBuilder):
55 build_method_fn = 'html_build'
56 file_extension = "html"
63 no_externalities = False
67 root_attrib = {'id': 'book-text'}
69 def __init__(self, gallery_path=None, gallery_url=None, base_url=None):
70 self._base_url = base_url
71 self.gallery_path = gallery_path
72 self.gallery_url = gallery_url
74 self.tree = text = etree.Element(self.root_tag, **self.root_attrib)
75 self.header = etree.Element('h1')
77 self.footnotes = etree.Element('div', id='footnotes')
79 self.nota_red = etree.Element('div', id='nota_red')
83 'header': self.header,
84 'footnotes': self.footnotes,
85 'nota_red': self.nota_red,
87 self.current_cursors = [text]
91 if self._base_url is not None:
94 return 'https://wolnelektury.pl/media/book/pictures/{}/'.format(self.document.meta.url.slug)
96 def build(self, document, element=None, **kwargs):
97 self.document = document
98 self.document.assign_ids()
102 element = document.tree.getroot()
104 element.html_build(self)
105 self.postprocess(document)
108 def prepare_images(self):
109 # Temporarily use the legacy method, before transitioning to external generators.
110 if self.gallery_path is None:
113 os.makedirs(self.gallery_path)
116 add_image_sizes(self.document.tree, self.gallery_path, self.gallery_url, self.base_url)
119 if not len(self.tree):
121 return OutputFile.from_bytes(
130 def postprocess(self, document):
131 _ = document.tree.getroot().gettext
133 if document.meta.translators:
134 self.enter_fragment('header')
135 self.start_element('span', {'class': 'translator'})
136 self.push_text(_("translated by") + " ")
139 translator.readable()
140 for translator in document.meta.translators
146 self.tree.insert(0, self.header)
148 if self.with_nota_red and len(self.nota_red):
149 self.tree.append(self.nota_red)
151 add_table_of_themes(self.tree)
153 add_table_of_contents(self.tree)
155 if len(self.footnotes):
156 fnheader = etree.Element("h3")
157 fnheader.text = _("Footnotes")
158 self.footnotes.insert(0, fnheader)
159 self.tree.append(self.footnotes)
161 def add_visible_number(self, element):
162 assert '_id' in element.attrib, etree.tostring(element)
163 self.start_element('a', {
164 'href': f'#{element.attrib["_id"]}',
167 self.push_text(element.attrib['_visible_numbering'])
171 class StandaloneHtmlBuilder(HtmlBuilder):
172 css_url = "https://static.wolnelektury.pl/css/compressed/book_text.css"
174 def postprocess(self, document):
175 super(StandaloneHtmlBuilder, self).postprocess(document)
177 tree = etree.Element('html')
178 body = etree.SubElement(tree, 'body')
179 body.append(self.tree)
182 head = etree.Element('head')
185 etree.SubElement(head, 'meta', charset='utf-8')
186 etree.SubElement(head, 'title').text = document.meta.title
192 content="width=device-width, initial-scale=1, maximum-scale=1"
195 if self.no_externalities:
198 ).text = urlopen(self.css_url).read().decode('utf-8')
210 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
216 src="http://malsup.github.io/min/jquery.cycle2.min.js"
220 class SnippetHtmlBuilder(HtmlBuilder):
223 with_footnotes = False
224 with_nota_red = False
226 with_numbering = False
229 class AbstraktHtmlBuilder(HtmlBuilder):
232 with_footnotes = False
233 with_nota_red = False
235 with_numbering = False
237 root_tag = 'blockquote'
240 def build(self, document, element=None, **kwargs):
242 element = document.tree.find('//abstrakt')
244 return OutputFile.from_bytes(b'')
245 element.attrib['_force'] = '1'
246 return super().build(document, element, **kwargs)
249 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
250 file_extension = 'xhtml'
253 with_footnotes = False
254 with_nota_red = False
255 with_deep_identifiers = False
256 no_externalities = True
257 with_numbering = False
260 tree = etree.ElementTree(self.tree)
261 tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
262 tree.docinfo.system_url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
263 return OutputFile.from_bytes(