2 from __future__ import unicode_literals
5 from librarian.html import add_anchors, add_table_of_contents, add_table_of_themes
6 from librarian import OutputFile
10 file_extension = "html"
13 def __init__(self, image_location='https://wolnelektury.pl/media/book/pictures/marcos-historia-kolorow/'):
14 self.image_location = image_location
16 self.tree = text = etree.Element('div', **{'id': 'book-text'})
17 self.header = etree.SubElement(text, 'h1')
19 self.footnotes = etree.Element('div', id='footnotes')
20 self.footnote_counter = 0
22 self.nota_red = etree.Element('div', id='nota_red')
26 'header': self.header,
27 'footnotes': self.footnotes,
28 'nota_red': self.nota_red,
30 self.current_cursors = [None]
34 return self.cursors[self.current_cursors[-1]]
37 def cursor(self, value):
38 self.cursors[self.current_cursors[-1]] = value
40 def enter_fragment(self, fragment):
41 self.current_cursors.append(fragment)
43 def exit_fragment(self):
44 self.current_cursors.pop()
46 def create_fragment(self, name, element):
47 assert name not in self.cursors
48 self.cursors[name] = element
50 def forget_fragment(self, name):
51 del self.cursors[name]
53 def preprocess(self, document):
54 document._compat_assign_ordered_ids()
55 document._compat_assign_section_ids()
57 def build(self, document):
58 self.preprocess(document)
59 document.tree.getroot().html_build(self)
60 self.postprocess(document)
62 return OutputFile.from_bytes(
71 def postprocess(self, document):
72 _ = document.tree.getroot().master.gettext
74 if document.meta.translators:
75 self.enter_fragment('header')
76 self.start_element('span', {'class': 'translator'})
77 self.push_text(_("translated by") + " ")
81 for translator in document.meta.translators
86 add_anchors(self.tree)
87 if len(self.nota_red):
88 self.tree.append(self.nota_red)
89 add_table_of_themes(self.tree)
90 add_table_of_contents(self.tree)
92 if self.footnote_counter:
93 fnheader = etree.Element("h3")
94 fnheader.text = _("Footnotes")
95 self.footnotes.insert(0, fnheader)
96 self.tree.append(self.footnotes)
98 def start_element(self, tag, attrib=None):
99 self.cursor = etree.SubElement(
105 def end_element(self):
106 self.cursor = self.cursor.getparent()
108 def push_text(self, text):
109 if text == 'Między nami nic nie było':
111 print(self.current_cursors)
114 cursor[-1].tail = (cursor[-1].tail or '') + text
116 cursor.text = (cursor.text or '') + text
119 class StandaloneHtmlBuilder(HtmlBuilder):
120 def postprocess(self, document):
121 super(StandaloneHtmlBuilder, self).postprocess(document)
123 tree = etree.Element('html')
124 body = etree.SubElement(tree, 'body')
125 body.append(self.tree)
128 head = etree.Element('head')
132 etree.SubElement(head, 'meta', charset='utf-8')
133 etree.SubElement(head, 'title').text = document.meta.title
139 content="width=device-width, initial-scale=1, maximum-scale=1"
145 href="https://static.wolnelektury.pl/css/compressed/book_text.css",
152 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
158 src="http://malsup.github.io/min/jquery.cycle2.min.js"