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 cursor = self.cursors.get(fragment, self.cursor)
55 self.current_cursors.append(cursor)
57 def exit_fragment(self):
58 self.current_cursors.pop()
60 def create_fragment(self, name, element):
61 assert name not in self.cursors
62 self.cursors[name] = element
64 def forget_fragment(self, name):
65 del self.cursors[name]
67 def preprocess(self, document):
68 document._compat_assign_ordered_ids()
69 document._compat_assign_section_ids()
71 def build(self, document, **kwargs):
72 self.document = document
74 self.preprocess(document)
75 document.tree.getroot().html_build(self)
76 self.postprocess(document)
80 return OutputFile.from_bytes(
89 def postprocess(self, document):
90 _ = document.tree.getroot().master.gettext
92 if document.meta.translators:
93 self.enter_fragment('header')
94 self.start_element('span', {'class': 'translator'})
95 self.push_text(_("translated by") + " ")
99 for translator in document.meta.translators
104 if self.with_anchors:
105 add_anchors(self.tree)
106 if self.with_nota_red and len(self.nota_red):
107 self.tree.append(self.nota_red)
109 add_table_of_themes(self.tree)
111 add_table_of_contents(self.tree)
114 self.tree.insert(0, self.header)
116 if self.footnote_counter:
117 fnheader = etree.Element("h3")
118 fnheader.text = _("Footnotes")
119 self.footnotes.insert(0, fnheader)
120 self.tree.append(self.footnotes)
122 def start_element(self, tag, attrib=None):
123 self.current_cursors.append(etree.SubElement(
129 def end_element(self):
130 self.current_cursors.pop()
132 def push_text(self, text):
135 cursor[-1].tail = (cursor[-1].tail or '') + text
137 cursor.text = (cursor.text or '') + text
140 class StandaloneHtmlBuilder(HtmlBuilder):
141 css_url = "https://static.wolnelektury.pl/css/compressed/book_text.css"
143 def postprocess(self, document):
144 super(StandaloneHtmlBuilder, self).postprocess(document)
146 tree = etree.Element('html')
147 body = etree.SubElement(tree, 'body')
148 body.append(self.tree)
151 head = etree.Element('head')
155 etree.SubElement(head, 'meta', charset='utf-8')
156 etree.SubElement(head, 'title').text = document.meta.title
162 content="width=device-width, initial-scale=1, maximum-scale=1"
165 if self.no_externalities:
168 ).text = urlopen(self.css_url).read().decode('utf-8')
180 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
186 src="http://malsup.github.io/min/jquery.cycle2.min.js"
190 class SnippetHtmlBuilder(HtmlBuilder):
194 with_footnotes = False
195 with_nota_red = False
199 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
200 file_extension = 'xhtml'
204 with_footnotes = False
205 with_nota_red = False
206 with_deep_identifiers = False
207 no_externalities = True
210 tree = etree.ElementTree(self.tree)
211 tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
212 tree.docinfo.system_url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
213 return OutputFile.from_bytes(