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
49 class HtmlBuilder(TreeBuilder):
50 build_method_fn = 'html_build'
51 file_extension = "html"
58 no_externalities = False
62 root_attrib = {'id': 'book-text'}
64 def __init__(self, gallery_path=None, gallery_url=None, base_url=None):
65 self._base_url = base_url
66 self.gallery_path = gallery_path
67 self.gallery_url = gallery_url
69 self.tree = text = etree.Element(self.root_tag, **self.root_attrib)
70 self.header = etree.Element('h1')
72 self.footnotes = etree.Element('div', id='footnotes')
74 self.nota_red = etree.Element('div', id='nota_red')
78 'header': self.header,
79 'footnotes': self.footnotes,
80 'nota_red': self.nota_red,
82 self.current_cursors = [text]
86 if self._base_url is not None:
89 return 'https://wolnelektury.pl/media/book/pictures/{}/'.format(self.document.meta.url.slug)
91 def build(self, document, element=None, **kwargs):
92 self.document = document
93 self.document.assign_ids()
97 element = document.tree.getroot()
99 element.html_build(self)
100 self.postprocess(document)
103 def prepare_images(self):
104 # Temporarily use the legacy method, before transitioning to external generators.
105 if self.gallery_path is None:
108 os.makedirs(self.gallery_path)
111 add_image_sizes(self.document.tree, self.gallery_path, self.gallery_url, self.base_url)
114 if not len(self.tree):
116 return OutputFile.from_bytes(
125 def postprocess(self, document):
126 _ = document.tree.getroot().gettext
128 if document.meta.translators:
129 self.enter_fragment('header')
130 self.start_element('span', {'class': 'translator'})
131 self.push_text(_("translated by") + " ")
134 translator.readable()
135 for translator in document.meta.translators
141 self.tree.insert(0, self.header)
143 if self.with_nota_red and len(self.nota_red):
144 self.tree.append(self.nota_red)
146 add_table_of_themes(self.tree)
148 add_table_of_contents(self.tree)
150 if len(self.footnotes):
151 fnheader = etree.Element("h3")
152 fnheader.text = _("Footnotes")
153 self.footnotes.insert(0, fnheader)
154 self.tree.append(self.footnotes)
156 def add_visible_number(self, element):
157 assert '_id' in element.attrib, etree.tostring(element)
158 self.start_element('a', {
159 'href': f'#{element.attrib["_id"]}',
162 self.push_text(element.attrib['_visible_numbering'])
166 class StandaloneHtmlBuilder(HtmlBuilder):
167 css_url = "https://static.wolnelektury.pl/css/compressed/book_text.css"
169 def postprocess(self, document):
170 super(StandaloneHtmlBuilder, self).postprocess(document)
172 tree = etree.Element('html')
173 body = etree.SubElement(tree, 'body')
174 body.append(self.tree)
177 head = etree.Element('head')
180 etree.SubElement(head, 'meta', charset='utf-8')
181 etree.SubElement(head, 'title').text = document.meta.title
187 content="width=device-width, initial-scale=1, maximum-scale=1"
190 if self.no_externalities:
193 ).text = urlopen(self.css_url).read().decode('utf-8')
205 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
211 src="http://malsup.github.io/min/jquery.cycle2.min.js"
215 class SnippetHtmlBuilder(HtmlBuilder):
218 with_footnotes = False
219 with_nota_red = False
221 with_numbering = False
224 class AbstraktHtmlBuilder(HtmlBuilder):
227 with_footnotes = False
228 with_nota_red = False
230 with_numbering = False
232 root_tag = 'blockquote'
235 def build(self, document, element=None, **kwargs):
237 element = document.tree.find('//abstrakt')
239 return OutputFile.from_bytes(b'')
240 element.attrib['_force'] = '1'
241 return super().build(document, element, **kwargs)
244 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
245 file_extension = 'xhtml'
248 with_footnotes = False
249 with_nota_red = False
250 with_deep_identifiers = False
251 no_externalities = True
252 with_numbering = False
255 tree = etree.ElementTree(self.tree)
256 tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
257 tree.docinfo.system_url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
258 return OutputFile.from_bytes(