2 from __future__ import unicode_literals
5 from urllib.request import urlopen
7 from urllib2 import urlopen
9 from librarian.html import add_anchors, add_table_of_contents, add_table_of_themes
10 from librarian import OutputFile
14 file_extension = "html"
20 no_externalities = False
23 def __init__(self, base_url=None):
24 self._base_url = base_url
26 self.tree = text = etree.Element('div', **{'id': 'book-text'})
27 self.header = etree.SubElement(text, 'h1')
29 self.footnotes = etree.Element('div', id='footnotes')
30 self.footnote_counter = 0
32 self.nota_red = etree.Element('div', id='nota_red')
36 'header': self.header,
37 'footnotes': self.footnotes,
38 'nota_red': self.nota_red,
40 self.current_cursors = [text]
44 if self._base_url is not None:
47 return 'https://wolnelektury.pl/media/book/pictures/{}/'.format(self.document.meta.url.slug)
51 return self.current_cursors[-1]
53 def enter_fragment(self, fragment):
54 self.current_cursors.append(self.cursors[fragment])
56 def exit_fragment(self):
57 self.current_cursors.pop()
59 def create_fragment(self, name, element):
60 assert name not in self.cursors
61 self.cursors[name] = element
63 def forget_fragment(self, name):
64 del self.cursors[name]
66 def preprocess(self, document):
67 document._compat_assign_ordered_ids()
68 document._compat_assign_section_ids()
70 def build(self, document, **kwargs):
71 self.document = document
73 self.preprocess(document)
74 document.tree.getroot().html_build(self)
75 self.postprocess(document)
79 return OutputFile.from_bytes(
88 def postprocess(self, document):
89 _ = document.tree.getroot().master.gettext
91 if document.meta.translators:
92 self.enter_fragment('header')
93 self.start_element('span', {'class': 'translator'})
94 self.push_text(_("translated by") + " ")
98 for translator in document.meta.translators
103 if self.with_anchors:
104 add_anchors(self.tree)
105 if self.with_nota_red and len(self.nota_red):
106 self.tree.append(self.nota_red)
108 add_table_of_themes(self.tree)
110 add_table_of_contents(self.tree)
112 if self.footnote_counter:
113 fnheader = etree.Element("h3")
114 fnheader.text = _("Footnotes")
115 self.footnotes.insert(0, fnheader)
116 self.tree.append(self.footnotes)
118 def start_element(self, tag, attrib=None):
119 self.current_cursors.append(etree.SubElement(
125 def end_element(self):
126 self.current_cursors.pop()
128 def push_text(self, text):
131 cursor[-1].tail = (cursor[-1].tail or '') + text
133 cursor.text = (cursor.text or '') + text
136 class StandaloneHtmlBuilder(HtmlBuilder):
137 css_url = "https://static.wolnelektury.pl/css/compressed/book_text.css"
139 def postprocess(self, document):
140 super(StandaloneHtmlBuilder, self).postprocess(document)
142 tree = etree.Element('html')
143 body = etree.SubElement(tree, 'body')
144 body.append(self.tree)
147 head = etree.Element('head')
151 etree.SubElement(head, 'meta', charset='utf-8')
152 etree.SubElement(head, 'title').text = document.meta.title
158 content="width=device-width, initial-scale=1, maximum-scale=1"
161 if self.no_externalities:
164 ).text = urlopen(self.css_url).read().decode('utf-8')
176 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
182 src="http://malsup.github.io/min/jquery.cycle2.min.js"
186 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
187 file_extension = 'xhtml'
191 with_footnotes = False
192 with_nota_red = False
193 with_deep_identifiers = False
194 no_externalities = True
197 tree = etree.ElementTree(self.tree)
198 tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
199 tree.docinfo.system_url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
200 return OutputFile.from_bytes(