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.Element('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)
113 self.tree.insert(0, self.header)
115 if self.footnote_counter:
116 fnheader = etree.Element("h3")
117 fnheader.text = _("Footnotes")
118 self.footnotes.insert(0, fnheader)
119 self.tree.append(self.footnotes)
121 def start_element(self, tag, attrib=None):
122 self.current_cursors.append(etree.SubElement(
128 def end_element(self):
129 self.current_cursors.pop()
131 def push_text(self, text):
134 cursor[-1].tail = (cursor[-1].tail or '') + text
136 cursor.text = (cursor.text or '') + text
139 class StandaloneHtmlBuilder(HtmlBuilder):
140 css_url = "https://static.wolnelektury.pl/css/compressed/book_text.css"
142 def postprocess(self, document):
143 super(StandaloneHtmlBuilder, self).postprocess(document)
145 tree = etree.Element('html')
146 body = etree.SubElement(tree, 'body')
147 body.append(self.tree)
150 head = etree.Element('head')
154 etree.SubElement(head, 'meta', charset='utf-8')
155 etree.SubElement(head, 'title').text = document.meta.title
161 content="width=device-width, initial-scale=1, maximum-scale=1"
164 if self.no_externalities:
167 ).text = urlopen(self.css_url).read().decode('utf-8')
179 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
185 src="http://malsup.github.io/min/jquery.cycle2.min.js"
189 class SnippetHtmlBuilder(HtmlBuilder):
193 with_footnotes = False
194 with_nota_red = False
198 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
199 file_extension = 'xhtml'
203 with_footnotes = False
204 with_nota_red = False
205 with_deep_identifiers = False
206 no_externalities = True
209 tree = etree.ElementTree(self.tree)
210 tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
211 tree.docinfo.system_url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
212 return OutputFile.from_bytes(