New epub.
[librarian.git] / src / librarian / builders / html.py
index 165a265..5096e28 100644 (file)
@@ -19,8 +19,8 @@ class HtmlBuilder:
     with_nota_red = True
     no_externalities = False
 
     with_nota_red = True
     no_externalities = False
 
-    def __init__(self, image_location='https://wolnelektury.pl/media/book/pictures/marcos-historia-kolorow/'):
-        self.image_location = image_location
+    def __init__(self, base_url=None):
+        self._base_url = base_url
 
         self.tree = text = etree.Element('div', **{'id': 'book-text'})
         self.header = etree.SubElement(text, 'h1')
 
         self.tree = text = etree.Element('div', **{'id': 'book-text'})
         self.header = etree.SubElement(text, 'h1')
@@ -36,18 +36,21 @@ class HtmlBuilder:
             'footnotes': self.footnotes,
             'nota_red': self.nota_red,
         }
             'footnotes': self.footnotes,
             'nota_red': self.nota_red,
         }
-        self.current_cursors = [None]
+        self.current_cursors = [text]
 
     @property
 
     @property
-    def cursor(self):
-        return self.cursors[self.current_cursors[-1]]
+    def base_url(self):
+        if self._base_url is not None:
+            return self._base_url
+        else:
+            return 'https://wolnelektury.pl/media/book/pictures/{}/'.format(self.document.meta.url.slug)
 
 
-    @cursor.setter
-    def cursor(self, value):
-        self.cursors[self.current_cursors[-1]] = value
+    @property
+    def cursor(self):
+        return self.current_cursors[-1]
 
     def enter_fragment(self, fragment):
 
     def enter_fragment(self, fragment):
-        self.current_cursors.append(fragment)
+        self.current_cursors.append(self.cursors[fragment])
 
     def exit_fragment(self):
         self.current_cursors.pop()
 
     def exit_fragment(self):
         self.current_cursors.pop()
@@ -63,7 +66,9 @@ class HtmlBuilder:
         document._compat_assign_ordered_ids()
         document._compat_assign_section_ids()
 
         document._compat_assign_ordered_ids()
         document._compat_assign_section_ids()
 
-    def build(self, document):
+    def build(self, document, **kwargs):
+        self.document = document
+
         self.preprocess(document)
         document.tree.getroot().html_build(self)
         self.postprocess(document)
         self.preprocess(document)
         document.tree.getroot().html_build(self)
         self.postprocess(document)
@@ -110,19 +115,16 @@ class HtmlBuilder:
             self.tree.append(self.footnotes)
 
     def start_element(self, tag, attrib=None):
             self.tree.append(self.footnotes)
 
     def start_element(self, tag, attrib=None):
-        self.cursor = etree.SubElement(
+        self.current_cursors.append(etree.SubElement(
             self.cursor,
             tag,
             **(attrib or {})
             self.cursor,
             tag,
             **(attrib or {})
-        )
+        ))
 
     def end_element(self):
 
     def end_element(self):
-        self.cursor = self.cursor.getparent()
+        self.current_cursors.pop()
 
     def push_text(self, text):
 
     def push_text(self, text):
-        if text == 'Między nami nic nie było':
-            print(self.cursors)
-            print(self.current_cursors)
         cursor = self.cursor
         if len(cursor):
             cursor[-1].tail = (cursor[-1].tail or '') + text
         cursor = self.cursor
         if len(cursor):
             cursor[-1].tail = (cursor[-1].tail or '') + text