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
13 file_extension = "html"
20 no_externalities = False
24 root_attrib = {'id': 'book-text'}
26 def __init__(self, gallery_path=None, gallery_url=None, base_url=None):
27 self._base_url = base_url
28 self.gallery_path = gallery_path
29 self.gallery_url = gallery_url
31 self.tree = text = etree.Element(self.root_tag, **self.root_attrib)
32 self.header = etree.Element('h1')
34 self.footnotes = etree.Element('div', id='footnotes')
36 self.nota_red = etree.Element('div', id='nota_red')
40 'header': self.header,
41 'footnotes': self.footnotes,
42 'nota_red': self.nota_red,
44 self.current_cursors = [text]
48 if self._base_url is not None:
51 return 'https://wolnelektury.pl/media/book/pictures/{}/'.format(self.document.meta.url.slug)
55 return self.current_cursors[-1]
57 def enter_fragment(self, fragment):
58 cursor = self.cursors.get(fragment, self.cursor)
59 self.current_cursors.append(cursor)
61 def exit_fragment(self):
62 self.current_cursors.pop()
64 def create_fragment(self, name, element):
65 assert name not in self.cursors
66 self.cursors[name] = element
68 def forget_fragment(self, name):
69 del self.cursors[name]
71 def build(self, document, element=None, **kwargs):
72 self.document = document
73 self.document.assign_ids()
77 element = document.tree.getroot()
79 element.html_build(self)
80 self.postprocess(document)
83 def prepare_images(self):
84 # Temporarily use the legacy method, before transitioning to external generators.
85 if self.gallery_path is None:
88 os.makedirs(self.gallery_path)
91 add_image_sizes(self.document.tree, self.gallery_path, self.gallery_url, self.base_url)
94 if not len(self.tree):
96 return OutputFile.from_bytes(
105 def postprocess(self, document):
106 _ = document.tree.getroot().gettext
108 if document.meta.translators:
109 self.enter_fragment('header')
110 self.start_element('span', {'class': 'translator'})
111 self.push_text(_("translated by") + " ")
114 translator.readable()
115 for translator in document.meta.translators
121 self.tree.insert(0, self.header)
123 if self.with_nota_red and len(self.nota_red):
124 self.tree.append(self.nota_red)
126 add_table_of_themes(self.tree)
128 add_table_of_contents(self.tree)
130 if len(self.footnotes):
131 fnheader = etree.Element("h3")
132 fnheader.text = _("Footnotes")
133 self.footnotes.insert(0, fnheader)
134 self.tree.append(self.footnotes)
136 def start_element(self, tag, attrib=None):
137 self.current_cursors.append(etree.SubElement(
143 def end_element(self):
144 self.current_cursors.pop()
146 def push_text(self, text):
149 cursor[-1].tail = (cursor[-1].tail or '') + text
151 cursor.text = (cursor.text or '') + text
153 def add_visible_number(self, element):
154 assert '_id' in element.attrib, etree.tostring(element)
155 self.start_element('a', {
156 'href': f'#{element.attrib["_id"]}',
159 self.push_text(element.attrib['_visible_numbering'])
163 class StandaloneHtmlBuilder(HtmlBuilder):
164 css_url = "https://static.wolnelektury.pl/css/compressed/book_text.css"
166 def postprocess(self, document):
167 super(StandaloneHtmlBuilder, self).postprocess(document)
169 tree = etree.Element('html')
170 body = etree.SubElement(tree, 'body')
171 body.append(self.tree)
174 head = etree.Element('head')
177 etree.SubElement(head, 'meta', charset='utf-8')
178 etree.SubElement(head, 'title').text = document.meta.title
184 content="width=device-width, initial-scale=1, maximum-scale=1"
187 if self.no_externalities:
190 ).text = urlopen(self.css_url).read().decode('utf-8')
202 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
208 src="http://malsup.github.io/min/jquery.cycle2.min.js"
212 class SnippetHtmlBuilder(HtmlBuilder):
215 with_footnotes = False
216 with_nota_red = False
218 with_numbering = False
221 class AbstraktHtmlBuilder(HtmlBuilder):
224 with_footnotes = False
225 with_nota_red = False
227 with_numbering = False
229 root_tag = 'blockquote'
232 def build(self, document, element=None, **kwargs):
234 element = document.tree.find('//abstrakt')
236 return OutputFile.from_bytes(b'')
237 element.attrib['_force'] = '1'
238 return super().build(document, element, **kwargs)
241 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
242 file_extension = 'xhtml'
245 with_footnotes = False
246 with_nota_red = False
247 with_deep_identifiers = False
248 no_externalities = True
249 with_numbering = False
252 tree = etree.ElementTree(self.tree)
253 tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
254 tree.docinfo.system_url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
255 return OutputFile.from_bytes(