+ @classmethod
+ def swap_endlines(cls, tree):
+ # only swap inside stanzas
+ for elem in tree.iter('strofa'):
+ for child in list(elem):
+ if child.tail:
+ chunks = cls.LINE_SWAP_EXPR.split(child.tail)
+ ins_index = elem.index(child) + 1
+ while len(chunks) > 1:
+ ins = etree.Element('br')
+ ins.tail = chunks.pop()
+ elem.insert(ins_index, ins)
+ child.tail = chunks.pop(0)
+ if elem.text:
+ chunks = cls.LINE_SWAP_EXPR.split(elem.text)
+ while len(chunks) > 1:
+ ins = etree.Element('br')
+ ins.tail = chunks.pop()
+ elem.insert(0, ins)
+ elem.text = chunks.pop(0)
+