+ 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)
+
+
+def get_resource(path):
+ return os.path.join(os.path.dirname(__file__), path)
+
+def get_stylesheet(name):
+ return get_resource(STYLESHEETS[name])
+
+
+def package_available(package, args='', verbose=False):
+ """ check if a verion of a latex package accepting given args is available """
+ tempdir = mkdtemp('-wl2pdf-test')
+ fpath = os.path.join(tempdir, 'test.tex')
+ f = open(fpath, 'w')
+ f.write(r"""
+ \documentclass{book}
+ \usepackage[%s]{%s}
+ \begin{document}
+ \end{document}
+ """ % (args, package))
+ f.close()
+ if verbose:
+ p = call(['xelatex', '-output-directory', tempdir, fpath])
+ else:
+ p = call(['xelatex', '-interaction=batchmode', '-output-directory', tempdir, fpath], stdout=PIPE, stderr=PIPE)
+ shutil.rmtree(tempdir)
+ return p == 0
+
+
+def transform(provider, slug=None, file_path=None,
+ output_file=None, output_dir=None, make_dir=False, verbose=False, save_tex=None, morefloats=None):
+ """ produces a PDF file with XeLaTeX
+
+ provider: a DocProvider
+ slug: slug of file to process, available by provider
+ file_path can be provided instead of a slug
+ output_file: file-like object or path to output file
+ output_dir: path to directory to save output file to; either this or output_file must be present
+ make_dir: writes output to <output_dir>/<author>/<slug>.pdf istead of <output_dir>/<slug>.pdf
+ verbose: prints all output from LaTeX
+ save_tex: path to save the intermediary LaTeX file to
+ morefloats (old/new/none): force specific morefloats