2 from librarian import OutputFile
6 file_extension = "html"
9 def __init__(self, image_location='https://wolnelektury.pl/media/book/pictures/marcos-historia-kolorow/'):
10 self.image_location = image_location
12 #self.tree = etree.Element('html')
13 #body = etree.SubElement(self.tree, 'body')
14 #text = etree.SubElement(body, 'div', **{'id': 'book-text'})
15 self.tree = text = etree.Element('div', **{'id': 'book-text'})
16 toc = etree.SubElement(text, 'div', id='toc')
17 themes = etree.SubElement(text, 'div', id='themes')
18 h1 = etree.SubElement(text, 'h1')
26 self.current_cursors = [None]
28 def enter_fragment(self, fragment):
29 self.current_cursors.append(fragment)
31 def exit_fragment(self):
32 self.current_cursors.pop()
34 def build(self, document):
35 document.tree.getroot().html_build(self)
37 head = etree.Element('head')
38 self.tree.insert(0, head)
42 href="https://static.wolnelektury.pl/css/compressed/book_text.b15153e56c0a.css",
47 return OutputFile.from_bytes(
56 def start_element(self, tag, attrib):
57 self.cursors[self.current_cursors[-1]] = etree.SubElement(
58 self.cursors[self.current_cursors[-1]],
64 def end_element(self):
65 self.cursors[self.current_cursors[-1]] = self.cursors[self.current_cursors[-1]].getparent()
67 def push_text(self, text):
68 cursor = self.cursors[self.current_cursors[-1]]
70 cursor.tail = (cursor[-1].tail or '') + text
72 cursor.text = (cursor.text or '') + text