7df549f5458586dd1e82c0eef1d8c062a461e849
[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     HTML_TAG = 'div'
13     HTML_CLASS = 'stanza'
14     
15     def get_verses(self):
16         from librarian.parser import parser
17
18         verses = [
19             parser.makeelement('wers')
20         ]
21         if self.text:
22             # Before any tags. These are text-only verses.
23             pieces = self.text.split('/')
24             for piece in pieces[:-1]:
25                 verses[-1].text = piece
26                 verses.append(parser.makeelement('wers'))
27             verses[-1].text = pieces[-1]
28
29         for child in self:
30             if child.tail:
31                 pieces = child.tail.split('/')
32                 child_copy = copy(child)
33                 child_copy.tail = pieces[0]
34                 verses[-1].append(child_copy)
35
36                 for piece in pieces[1:]:
37                     verses.append(parser.makeelement('wers'))
38                     verses[-1].text = piece
39                 
40             else:
41                 verses[-1].append(child)
42
43         for verse in verses:
44             verse.stanza = self
45             if len(verse) == 1 and isinstance(verse[0], Wers):
46                 assert not (verse.text or '').strip()
47                 assert not (verse[0].tail or '').strip()
48                 yield verse[0]
49             else:
50                 yield verse
51
52     def _build_inner(self, builder, build_method):
53         for child in self.get_verses():
54             getattr(child, build_method)(builder)