1 # -*- coding: utf-8 -*-
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
7 # \author Thibaut Cuvelier
9 # Full author contact details are available in file CREDITS
12 # python docbook_copy.py lilypond_book_command in.docbook out.docbook
13 # This script copies the original DocBook file (directly produced by LyX) to the output DocBook file,
14 # potentially applying a post-processing step. For now, the only implemented post-processing step is
16 # /!\ The original file may be modified by this script!
24 def need_lilypond(file):
25 # Really tailored to the kind of output lilypond.module makes (in lib/layouts).
26 with open(file, 'r') as f:
27 return "language='lilypond'" in f.read()
30 def copy_docbook(args):
32 print('Exactly four arguments are expected, only %s found: %s.' % (len(args), args))
35 # Parse the command line.
36 lilypond_command = args[1]
40 has_lilypond = lilypond_command != ""
42 # Apply LilyPond to the original file if available and needed.
43 if has_lilypond and need_lilypond(in_file):
44 # LilyPond requires that its input file has the .lyxml extension.
45 # Move the file, so that LilyPond doesn't have to erase the contents of the original file before
46 # writing the converted output.
47 in_lily_file = in_file.replace(".xml", ".lyxml")
48 shutil.move(in_file, in_lily_file)
50 # Start LilyPond on the copied file.
51 command = lilypond_command + ' --format=docbook ' + in_lily_file
52 if os.system(command) != 0:
53 print('Error from LilyPond')
56 # Now, in_file should have the LilyPond-processed contents.
58 # Perform the final copy.
59 shutil.copyfile(in_file, out_file, follow_symlinks=False)
62 if __name__ == '__main__':
63 copy_docbook(sys.argv)