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, image_location='https://wolnelektury.pl/media/book/pictures/marcos-historia-kolorow/'):
23 self.image_location = image_location
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 return self.current_cursors[-1]
45 def enter_fragment(self, fragment):
46 self.current_cursors.append(self.cursors[fragment])
48 def exit_fragment(self):
49 self.current_cursors.pop()
51 def create_fragment(self, name, element):
52 assert name not in self.cursors
53 self.cursors[name] = element
55 def forget_fragment(self, name):
56 del self.cursors[name]
58 def preprocess(self, document):
59 document._compat_assign_ordered_ids()
60 document._compat_assign_section_ids()
62 def build(self, document, **kwargs):
63 self.preprocess(document)
64 document.tree.getroot().html_build(self)
65 self.postprocess(document)
69 return OutputFile.from_bytes(
78 def postprocess(self, document):
79 _ = document.tree.getroot().master.gettext
81 if document.meta.translators:
82 self.enter_fragment('header')
83 self.start_element('span', {'class': 'translator'})
84 self.push_text(_("translated by") + " ")
88 for translator in document.meta.translators
94 add_anchors(self.tree)
95 if self.with_nota_red and len(self.nota_red):
96 self.tree.append(self.nota_red)
98 add_table_of_themes(self.tree)
100 add_table_of_contents(self.tree)
102 if self.footnote_counter:
103 fnheader = etree.Element("h3")
104 fnheader.text = _("Footnotes")
105 self.footnotes.insert(0, fnheader)
106 self.tree.append(self.footnotes)
108 def start_element(self, tag, attrib=None):
109 self.current_cursors.append(etree.SubElement(
115 def end_element(self):
116 self.current_cursors.pop()
118 def push_text(self, text):
121 cursor[-1].tail = (cursor[-1].tail or '') + text
123 cursor.text = (cursor.text or '') + text
126 class StandaloneHtmlBuilder(HtmlBuilder):
127 css_url = "https://static.wolnelektury.pl/css/compressed/book_text.css"
129 def postprocess(self, document):
130 super(StandaloneHtmlBuilder, self).postprocess(document)
132 tree = etree.Element('html')
133 body = etree.SubElement(tree, 'body')
134 body.append(self.tree)
137 head = etree.Element('head')
141 etree.SubElement(head, 'meta', charset='utf-8')
142 etree.SubElement(head, 'title').text = document.meta.title
148 content="width=device-width, initial-scale=1, maximum-scale=1"
151 if self.no_externalities:
154 ).text = urlopen(self.css_url).read().decode('utf-8')
166 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
172 src="http://malsup.github.io/min/jquery.cycle2.min.js"
176 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
177 file_extension = 'xhtml'
181 with_footnotes = False
182 with_nota_red = False
183 with_deep_identifiers = False
184 no_externalities = True
187 tree = etree.ElementTree(self.tree)
188 tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
189 tree.docinfo.system_url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
190 return OutputFile.from_bytes(