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
22 def __init__(self, base_url=None):
23 self._base_url = base_url
25 self.tree = text = etree.Element('div', **{'id': 'book-text'})
26 self.header = etree.SubElement(text, 'h1')
28 self.footnotes = etree.Element('div', id='footnotes')
29 self.footnote_counter = 0
31 self.nota_red = etree.Element('div', id='nota_red')
35 'header': self.header,
36 'footnotes': self.footnotes,
37 'nota_red': self.nota_red,
39 self.current_cursors = [text]
43 if self._base_url is not None:
46 return 'https://wolnelektury.pl/media/book/pictures/{}/'.format(self.document.meta.url.slug)
50 return self.current_cursors[-1]
52 def enter_fragment(self, fragment):
53 self.current_cursors.append(self.cursors[fragment])
55 def exit_fragment(self):
56 self.current_cursors.pop()
58 def create_fragment(self, name, element):
59 assert name not in self.cursors
60 self.cursors[name] = element
62 def forget_fragment(self, name):
63 del self.cursors[name]
65 def preprocess(self, document):
66 document._compat_assign_ordered_ids()
67 document._compat_assign_section_ids()
69 def build(self, document, **kwargs):
70 self.document = document
72 self.preprocess(document)
73 document.tree.getroot().html_build(self)
74 self.postprocess(document)
78 return OutputFile.from_bytes(
87 def postprocess(self, document):
88 _ = document.tree.getroot().master.gettext
90 if document.meta.translators:
91 self.enter_fragment('header')
92 self.start_element('span', {'class': 'translator'})
93 self.push_text(_("translated by") + " ")
97 for translator in document.meta.translators
102 if self.with_anchors:
103 add_anchors(self.tree)
104 if self.with_nota_red and len(self.nota_red):
105 self.tree.append(self.nota_red)
107 add_table_of_themes(self.tree)
109 add_table_of_contents(self.tree)
111 if self.footnote_counter:
112 fnheader = etree.Element("h3")
113 fnheader.text = _("Footnotes")
114 self.footnotes.insert(0, fnheader)
115 self.tree.append(self.footnotes)
117 def start_element(self, tag, attrib=None):
118 self.current_cursors.append(etree.SubElement(
124 def end_element(self):
125 self.current_cursors.pop()
127 def push_text(self, text):
130 cursor[-1].tail = (cursor[-1].tail or '') + text
132 cursor.text = (cursor.text or '') + text
135 class StandaloneHtmlBuilder(HtmlBuilder):
136 css_url = "https://static.wolnelektury.pl/css/compressed/book_text.css"
138 def postprocess(self, document):
139 super(StandaloneHtmlBuilder, self).postprocess(document)
141 tree = etree.Element('html')
142 body = etree.SubElement(tree, 'body')
143 body.append(self.tree)
146 head = etree.Element('head')
150 etree.SubElement(head, 'meta', charset='utf-8')
151 etree.SubElement(head, 'title').text = document.meta.title
157 content="width=device-width, initial-scale=1, maximum-scale=1"
160 if self.no_externalities:
163 ).text = urlopen(self.css_url).read().decode('utf-8')
175 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
181 src="http://malsup.github.io/min/jquery.cycle2.min.js"
185 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
186 file_extension = 'xhtml'
190 with_footnotes = False
191 with_nota_red = False
192 with_deep_identifiers = False
193 no_externalities = True
196 tree = etree.ElementTree(self.tree)
197 tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
198 tree.docinfo.system_url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
199 return OutputFile.from_bytes(