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 = [None]
43 return self.cursors[self.current_cursors[-1]]
46 def cursor(self, value):
47 self.cursors[self.current_cursors[-1]] = value
49 def enter_fragment(self, fragment):
50 self.current_cursors.append(fragment)
52 def exit_fragment(self):
53 self.current_cursors.pop()
55 def create_fragment(self, name, element):
56 assert name not in self.cursors
57 self.cursors[name] = element
59 def forget_fragment(self, name):
60 del self.cursors[name]
62 def preprocess(self, document):
63 document._compat_assign_ordered_ids()
64 document._compat_assign_section_ids()
66 def build(self, document):
67 self.preprocess(document)
68 document.tree.getroot().html_build(self)
69 self.postprocess(document)
73 return OutputFile.from_bytes(
82 def postprocess(self, document):
83 _ = document.tree.getroot().master.gettext
85 if document.meta.translators:
86 self.enter_fragment('header')
87 self.start_element('span', {'class': 'translator'})
88 self.push_text(_("translated by") + " ")
92 for translator in document.meta.translators
98 add_anchors(self.tree)
99 if self.with_nota_red and len(self.nota_red):
100 self.tree.append(self.nota_red)
102 add_table_of_themes(self.tree)
104 add_table_of_contents(self.tree)
106 if self.footnote_counter:
107 fnheader = etree.Element("h3")
108 fnheader.text = _("Footnotes")
109 self.footnotes.insert(0, fnheader)
110 self.tree.append(self.footnotes)
112 def start_element(self, tag, attrib=None):
113 self.cursor = etree.SubElement(
119 def end_element(self):
120 self.cursor = self.cursor.getparent()
122 def push_text(self, text):
123 if text == 'Między nami nic nie było':
125 print(self.current_cursors)
128 cursor[-1].tail = (cursor[-1].tail or '') + text
130 cursor.text = (cursor.text or '') + text
133 class StandaloneHtmlBuilder(HtmlBuilder):
134 css_url = "https://static.wolnelektury.pl/css/compressed/book_text.css"
136 def postprocess(self, document):
137 super(StandaloneHtmlBuilder, self).postprocess(document)
139 tree = etree.Element('html')
140 body = etree.SubElement(tree, 'body')
141 body.append(self.tree)
144 head = etree.Element('head')
148 etree.SubElement(head, 'meta', charset='utf-8')
149 etree.SubElement(head, 'title').text = document.meta.title
155 content="width=device-width, initial-scale=1, maximum-scale=1"
158 if self.no_externalities:
161 ).text = urlopen(self.css_url).read().decode('utf-8')
173 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
179 src="http://malsup.github.io/min/jquery.cycle2.min.js"
183 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
184 file_extension = 'xhtml'
188 with_footnotes = False
189 with_nota_red = False
190 with_deep_identifiers = False
191 no_externalities = True
194 tree = etree.ElementTree(self.tree)
195 tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
196 tree.docinfo.system_url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
197 return OutputFile.from_bytes(