bugfix
[librarian.git] / src / librarian / builders / html.py
index ee50cb8..0499d43 100644 (file)
@@ -1,10 +1,7 @@
-# coding: utf-8
-from __future__ import unicode_literals
-
-try:
-    from urllib.request import urlopen
-except ImportError:
-    from urllib2 import urlopen
+# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
+#
+from urllib.request import urlopen
 from lxml import etree
 from librarian.html import add_anchors, add_table_of_contents, add_table_of_themes
 from librarian import OutputFile
 from lxml import etree
 from librarian.html import add_anchors, add_table_of_contents, add_table_of_themes
 from librarian import OutputFile
@@ -18,12 +15,13 @@ class HtmlBuilder:
     with_footnotes = True
     with_nota_red = True
     no_externalities = False
     with_footnotes = True
     with_nota_red = True
     no_externalities = False
+    orphans = True
 
 
-    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.tree = text = etree.Element('div', **{'id': 'book-text'})
-        self.header = etree.SubElement(text, 'h1')
+        self.header = etree.Element('h1')
 
         self.footnotes = etree.Element('div', id='footnotes')
         self.footnote_counter = 0
 
         self.footnotes = etree.Element('div', id='footnotes')
         self.footnote_counter = 0
@@ -38,12 +36,20 @@ class HtmlBuilder:
         }
         self.current_cursors = [text]
 
         }
         self.current_cursors = [text]
 
+    @property
+    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)
+
     @property
     def cursor(self):
         return self.current_cursors[-1]
 
     def enter_fragment(self, fragment):
     @property
     def cursor(self):
         return self.current_cursors[-1]
 
     def enter_fragment(self, fragment):
-        self.current_cursors.append(self.cursors[fragment])
+        cursor = self.cursors.get(fragment, self.cursor)
+        self.current_cursors.append(cursor)
 
     def exit_fragment(self):
         self.current_cursors.pop()
 
     def exit_fragment(self):
         self.current_cursors.pop()
@@ -60,6 +66,8 @@ class HtmlBuilder:
         document._compat_assign_section_ids()
 
     def build(self, document, **kwargs):
         document._compat_assign_section_ids()
 
     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)
@@ -90,6 +98,9 @@ class HtmlBuilder:
             )
             self.exit_fragment()
 
             )
             self.exit_fragment()
 
+        if len(self.header):
+            self.tree.insert(0, self.header)
+            
         if self.with_anchors:
             add_anchors(self.tree)
         if self.with_nota_red and len(self.nota_red):
         if self.with_anchors:
             add_anchors(self.tree)
         if self.with_nota_red and len(self.nota_red):
@@ -173,6 +184,15 @@ class StandaloneHtmlBuilder(HtmlBuilder):
             )
 
 
             )
 
 
+class SnippetHtmlBuilder(HtmlBuilder):
+    with_anchors = False
+    with_themes = False
+    with_toc = False
+    with_footnotes = False
+    with_nota_red = False
+    with_refs = False
+
+            
 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
     file_extension = 'xhtml'
     with_anchors = False
 class DaisyHtmlBuilder(StandaloneHtmlBuilder):
     file_extension = 'xhtml'
     with_anchors = False