8015c6a4c30f84af2fb4117e86d4bec11662f55e
[librarian.git] / src / librarian / builders / html.py
1 from lxml import etree
2 from librarian import OutputFile
3
4
5 class HtmlBuilder:
6     file_extension = "html"
7     identifier = "html"
8
9     def __init__(self, image_location='https://wolnelektury.pl/media/book/pictures/marcos-historia-kolorow/'):
10         self.image_location = image_location
11
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')
19
20         self.cursors = {
21             None: text,
22             'toc': toc,
23             'themes': themes,
24             'header': h1,
25         }
26         self.current_cursors = [None]
27
28     def enter_fragment(self, fragment):
29         self.current_cursors.append(fragment)
30
31     def exit_fragment(self):
32         self.current_cursors.pop()
33         
34     def build(self, document):
35         document.tree.getroot().html_build(self)
36
37         head = etree.Element('head')
38         self.tree.insert(0, head)
39         etree.SubElement(
40             head,
41             'link',
42             href="https://static.wolnelektury.pl/css/compressed/book_text.b15153e56c0a.css",
43             rel="stylesheet",
44             type="text/css",
45         )
46         
47         return OutputFile.from_bytes(
48             etree.tostring(
49                 self.tree,
50                 method='html',
51                 encoding='utf-8',
52                 pretty_print=True
53             )
54         )
55
56     def start_element(self, tag, attrib):
57         self.cursors[self.current_cursors[-1]] = etree.SubElement(
58             self.cursors[self.current_cursors[-1]],
59             tag,
60             **attrib
61         )
62         print(self.cursors)
63
64     def end_element(self):
65         self.cursors[self.current_cursors[-1]] = self.cursors[self.current_cursors[-1]].getparent()
66
67     def push_text(self, text):
68         cursor = self.cursors[self.current_cursors[-1]]
69         if len(cursor):
70             cursor.tail = (cursor[-1].tail or '') + text
71         else:
72             cursor.text = (cursor.text or '') + text