wip change fb2 api
[librarian.git] / src / librarian / elements / poetry / strofa.py
index 7df549f..93e3a4a 100644 (file)
@@ -1,18 +1,36 @@
+# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
+#
 from copy import copy
 from copy import copy
+import re
 from ..base import WLElement
 from .wers import Wers
 
 
 class Strofa(WLElement):
 from ..base import WLElement
 from .wers import Wers
 
 
 class Strofa(WLElement):
+    NUMBERING = 'i'
+
     TXT_TOP_MARGIN = 2
     TXT_BOTTOM_MARGIN = 2
     TXT_TOP_MARGIN = 2
     TXT_BOTTOM_MARGIN = 2
-    TXT_LEGACY_TOP_MARGIN = 1
-    TXT_LEGACY_BOTTOM_MARGIN = 0
 
 
-    HTML_TAG = 'div'
-    HTML_CLASS = 'stanza'
+    EPUB_TAG = HTML_TAG = 'div'
+    EPUB_CLASS = HTML_CLASS = 'stanza'
+
+    FB2_TAG = 'stanza'
     
     
-    def get_verses(self):
+
+    def epub_build(self, builder):
+        super().epub_build(builder)
+        builder.start_element(
+            'div',
+            {
+                'class': 'stanza-spacer'
+            }
+        )
+        builder.push_text('\u00a0');
+        builder.end_element()
+
+    def preprocess(self):
         from librarian.parser import parser
 
         verses = [
         from librarian.parser import parser
 
         verses = [
@@ -20,7 +38,7 @@ class Strofa(WLElement):
         ]
         if self.text:
             # Before any tags. These are text-only verses.
         ]
         if self.text:
             # Before any tags. These are text-only verses.
-            pieces = self.text.split('/')
+            pieces = re.split(r"/\s+", self.text)
             for piece in pieces[:-1]:
                 verses[-1].text = piece
                 verses.append(parser.makeelement('wers'))
             for piece in pieces[:-1]:
                 verses[-1].text = piece
                 verses.append(parser.makeelement('wers'))
@@ -28,7 +46,7 @@ class Strofa(WLElement):
 
         for child in self:
             if child.tail:
 
         for child in self:
             if child.tail:
-                pieces = child.tail.split('/')
+                pieces = re.split(r"/\s+", child.tail)
                 child_copy = copy(child)
                 child_copy.tail = pieces[0]
                 verses[-1].append(child_copy)
                 child_copy = copy(child)
                 child_copy.tail = pieces[0]
                 verses[-1].append(child_copy)
@@ -40,15 +58,15 @@ class Strofa(WLElement):
             else:
                 verses[-1].append(child)
 
             else:
                 verses[-1].append(child)
 
-        for verse in verses:
-            verse.stanza = self
-            if len(verse) == 1 and isinstance(verse[0], Wers):
-                assert not (verse.text or '').strip()
-                assert not (verse[0].tail or '').strip()
-                yield verse[0]
-            else:
-                yield verse
+        verses = [
+            verse[0] if len(verse) == 1 and isinstance(verse[0], Wers)
+            else verse
+            for verse in verses
+        ]
 
 
-    def _build_inner(self, builder, build_method):
-        for child in self.get_verses():
-            getattr(child, build_method)(builder)
+        while len(self):
+            self.remove(self[0])
+        self.text = None
+
+        for verse in verses:
+            self.append(verse)