+def move_motifs_inside(doc):
+ """ moves motifs to be into block elements """
+ for master in doc.xpath('//powiesc|//opowiadanie|//liryka_l|//liryka_lp|//dramat_wierszowany_l|//dramat_wierszowany_lp|//dramat_wspolczesny'):
+ for motif in master.xpath('motyw'):
+ for sib in motif.itersiblings():
+ if sib.tag not in ('sekcja_swiatlo', 'sekcja_asterysk', 'separator_linia', 'begin', 'end', 'motyw', 'extra', 'uwaga'):
+ # motif shouldn't have a tail - it would be untagged text
+ motif.tail = None
+ motif.getparent().remove(motif)
+ sib.insert(0, motif)
+ break
+
+
+def hack_motifs(doc):
+ """ dirty hack for the marginpar-creates-orphans LaTeX problem
+ see http://www.latex-project.org/cgi-bin/ltxbugs2html?pr=latex/2304
+
+ moves motifs in stanzas from first verse to second
+ and from next to last to last, then inserts negative vspace before them
+ """
+ for motif in doc.findall('//strofa//motyw'):
+ # find relevant verse-level tag
+ verse, stanza = motif, motif.getparent()
+ while stanza is not None and stanza.tag != 'strofa':
+ verse, stanza = stanza, stanza.getparent()
+ breaks_before = sum(1 for i in verse.itersiblings('br', preceding=True))
+ breaks_after = sum(1 for i in verse.itersiblings('br'))
+ if (breaks_before == 0 and breaks_after > 0) or breaks_after == 1:
+ move_by = 1
+ if breaks_after == 2:
+ move_by += 1
+ moved_motif = deepcopy(motif)
+ motif.tag = 'span'
+ motif.text = None
+ moved_motif.tail = None
+ moved_motif.set('moved', str(move_by))
+
+ for br in verse.itersiblings('br'):
+ if move_by > 1:
+ move_by -= 1
+ continue
+ br.addnext(moved_motif)
+ break
+
+
+def parse_creator(doc):
+ """ find all dc:creator tags and add dc:creator_parsed with forenames first """
+ for creator in doc.findall('//'+DCNS('creator')):
+ p = Person.from_text(creator.text)
+ creator_parsed = deepcopy(creator)
+ creator_parsed.tag = DCNS('creator_parsed')
+ creator_parsed.text = ' '.join(p.first_names + (p.last_name,))
+ creator.getparent().insert(0, creator_parsed)
+
+