2d3a4c996da521fd762abc83aa51b340bbbf4e27
[librarian.git] / src / librarian / elements / poetry / strofa.py
1 from copy import copy
2 from ..base import WLElement
3 from .wers import Wers
4
5
6 class Strofa(WLElement):
7     TXT_TOP_MARGIN = 2
8     TXT_BOTTOM_MARGIN = 2
9     TXT_LEGACY_TOP_MARGIN = 1
10     TXT_LEGACY_BOTTOM_MARGIN = 0
11
12     def get_verses(self):
13         from librarian.parser import parser
14
15         verses = [
16             parser.makeelement('wers')
17         ]
18         if self.text:
19             # Before any tags. These are text-only verses.
20             pieces = self.text.split('/')
21             for piece in pieces[:-1]:
22                 verses[-1].text = piece
23                 verses.append(parser.makeelement('wers'))
24             verses[-1].text = pieces[-1]
25
26         for child in self:
27             if child.tail:
28                 pieces = child.tail.split('/')
29                 child_copy = copy(child)
30                 child_copy.tail = pieces[0]
31                 verses[-1].append(child_copy)
32
33                 for piece in pieces[1:]:
34                     verses.append(parser.makeelement('wers'))
35                     verses[-1].text = piece
36                 
37             else:
38                 verses[-1].append(child)
39
40         for verse in verses:
41             if len(verse) == 1 and isinstance(verse[0], Wers):
42                 assert not (verse.text or '').strip()
43                 assert not (verse[0].tail or '').strip()
44                 yield verse[0]
45             else:
46                 yield verse
47
48     def _build_inner(self, builder, build_method):
49         for child in self.get_verses():
50             getattr(child, build_method)(builder)