Basic biblical tools.
[librarian.git] / src / librarian / elements / base.py
index 2349f16..2d656ae 100644 (file)
@@ -37,7 +37,9 @@ class WLElement(etree.ElementBase):
     EPUB_ATTR = {}
     EPUB_CLASS = None
     EPUB_START_CHUNK = False
-   
+
+    FB2_TAG = None
+
     CAN_HAVE_TEXT = True
     STRIP = False
     NUMBERING = None
@@ -45,12 +47,11 @@ class WLElement(etree.ElementBase):
     text_substitutions = [
         ('---', '—'),
         ('--', '–'),
-        #('...', '…'),  # Temporary turnoff for epub
+        ('...', '…'),
         (',,', '„'),
         ('"', '”'),
         ('\ufeff', ''),
-
-        ("'", "\u2019"),    # This was enabled for epub.
+        ("'", "\u2019"),
     ]
 
     @property
@@ -118,9 +119,9 @@ class WLElement(etree.ElementBase):
         text = text or ''
         for e, s in self.text_substitutions:
             text = text.replace(e, s)
-            # FIXME: TEmporary turnoff
-#        text = re.sub(r'\s+', ' ', text)
-### TODO: Added now for epub
+
+        if getattr(builder, 'normalize_whitespace', False):
+            text = re.sub(r'\s+', ' ', text)
 
         if getattr(builder, 'hyphenator', None) is not None:
             newt = ''
@@ -134,7 +135,8 @@ class WLElement(etree.ElementBase):
 
         return text
 
-    def _build_inner(self, builder, build_method):
+    def build_inner(self, builder):
+        build_method = builder.build_method_fn
         child_count = len(self)
         if self.CAN_HAVE_TEXT and self.text:
             text = self.normalize_text(self.text, builder)
@@ -144,8 +146,12 @@ class WLElement(etree.ElementBase):
                     text = text.rstrip()
             builder.push_text(text)
         for i, child in enumerate(self):
+            real_child_count = 0
             if isinstance(child, WLElement):
                 getattr(child, build_method)(builder)
+                self.after_child(builder, real_child_count)
+                real_child_count += 1
+
             # FIXME base builder api
             elif getattr(builder, 'debug', False) and child.tag is etree.Comment:
                 builder.process_comment(child)
@@ -155,24 +161,26 @@ class WLElement(etree.ElementBase):
                     text = text.rstrip()
                 builder.push_text(text)
 
-    def _txt_build_inner(self, builder):
-        self._build_inner(builder, 'txt_build')
+    def after_child(self, builder, child_count):
+        fn = getattr(builder, 'after_child_fn', None)
+        if fn:
+            getattr(self, builder.after_child_fn)(builder, child_count)
+
+    def txt_after_child(self, builder, child_count):
+        pass
+
+    def txt_build_inner(self, builder):
+        self.build_inner(builder)
 
     def txt_build(self, builder):
-        if hasattr(self, 'TXT_LEGACY_TOP_MARGIN'):
-            builder.push_legacy_margin(self.TXT_LEGACY_TOP_MARGIN)
-        else:
-            builder.push_margin(self.TXT_TOP_MARGIN)
+        builder.push_margin(self.TXT_TOP_MARGIN)
         builder.push_text(self.TXT_PREFIX, True)
-        self._txt_build_inner(builder)
+        self.txt_build_inner(builder)
         builder.push_text(self.TXT_SUFFIX, True)
-        if hasattr(self, 'TXT_LEGACY_BOTTOM_MARGIN'):
-            builder.push_legacy_margin(self.TXT_LEGACY_BOTTOM_MARGIN)
-        else:
-            builder.push_margin(self.TXT_BOTTOM_MARGIN)
+        builder.push_margin(self.TXT_BOTTOM_MARGIN)
 
-    def _html_build_inner(self, builder):
-        self._build_inner(builder, 'html_build')
+    def html_build_inner(self, builder):
+        self.build_inner(builder)
 
     def get_html_attr(self, builder):
         attr = self.HTML_ATTR.copy()
@@ -199,12 +207,31 @@ class WLElement(etree.ElementBase):
                 self.get_html_attr(builder),
             )
 
-        self._html_build_inner(builder)
+        self.html_build_inner(builder)
         if self.HTML_TAG:
             builder.end_element()
 
-    def _epub_build_inner(self, builder):
-        self._build_inner(builder, 'epub_build')
+    def fb2_build(self, builder):
+        if self.SECTION_PRECEDENCE:
+            builder.start_section(self.SECTION_PRECEDENCE)
+            builder.start_element('title')
+            builder.start_element('p')
+
+        if self.FB2_TAG:
+            builder.start_element(
+                self.FB2_TAG,
+                #self.get_fb2_attr(builder),
+            )
+
+        self.build_inner(builder)
+        if self.FB2_TAG:
+            builder.end_element()
+        if self.SECTION_PRECEDENCE:
+            builder.end_element()
+            builder.end_element()
+
+    def epub_build_inner(self, builder):
+        self.build_inner(builder)
 
     def get_epub_attr(self, builder):
         attr = self.EPUB_ATTR.copy()
@@ -250,7 +277,7 @@ class WLElement(etree.ElementBase):
                 attr
             )
 
-        self._epub_build_inner(builder)
+        self.epub_build_inner(builder)
         if self.EPUB_TAG:
             builder.end_element()
 
@@ -349,16 +376,16 @@ class WLElement(etree.ElementBase):
             prefix = 'f' # default numbering prefix
         return prefix
 
-    def assign_id(self, builder):
+    def assign_id(self, document):
         numbering = self.numbering
         if numbering:
-            number = str(builder.counters[numbering])
+            number = str(document.counters[numbering])
             self.attrib['_id'] = self.id_prefix + number
-            builder.counters[numbering] += 1
+            document.counters[numbering] += 1
 
             if numbering == 'main':
-                self.attrib['_visible_numbering'] = str(builder.counters['_visible'])
-                builder.counters['_visible'] += 1
+                self.attrib['_visible_numbering'] = str(document.counters['_visible'])
+                document.counters['_visible'] += 1
 
             if numbering == 'fn':
                 self.attrib['_visible_numbering'] = number