DocBook: work around bug in LilyPond.

https://lists.gnu.org/archive/html/bug-lilypond/2021-09/msg00039.html
This commit is contained in:
Thibaut Cuvelier 2021-09-26 21:00:59 +02:00
parent 7c1d4fd3c4
commit e983676f6c

View File

@ -19,6 +19,8 @@
import os import os
import os.path
import re
import shutil import shutil
import sys import sys
@ -42,21 +44,59 @@ def copy_docbook(args):
has_lilypond = lilypond_command != "" and lilypond_command != "none" has_lilypond = lilypond_command != "" and lilypond_command != "none"
# Guess the path for LilyPond.
lilypond_folder = os.path.split(lilypond_command)[0] if has_lilypond else ''
# Help debugging.
print("Given arguments:")
print("LilyPond: " + ("present" if has_lilypond else "not found") + " " + lilypond_command)
print("LilyPond path: " + lilypond_folder)
print("Input file: " + in_file)
print("Output file: " + out_file)
# Apply LilyPond to the original file if available and needed. # Apply LilyPond to the original file if available and needed.
if has_lilypond and need_lilypond(in_file): if has_lilypond and need_lilypond(in_file):
# LilyPond requires that its input file has the .lyxml extension.
# Move the file, so that LilyPond doesn't have to erase the contents of the original file before
# writing the converted output.
in_lily_file = in_file.replace(".xml", ".lyxml") in_lily_file = in_file.replace(".xml", ".lyxml")
shutil.move(in_file, in_lily_file) print("The input file needs a LilyPond pass and LilyPond is available.")
print("Rewriting " + in_file + " as " + in_lily_file)
# LilyPond requires that its input file has the .lyxml extension. Due to a bug in LilyPond,
# use " instead of ' to encode XML attributes.
# https://lists.gnu.org/archive/html/bug-lilypond/2021-09/msg00039.html
# Typical transformation:
# FROM: language='lilypond' role='fragment verbatim staffsize=16 ragged-right relative=2'
# TO: language="lilypond" role="fragment verbatim staffsize=16 ragged-right relative=2"
with open(in_file, 'r', encoding='utf-8') as f, open(in_lily_file, 'w', encoding='utf-8') as f_lily:
for line in f:
if "language='lilypond'" in line:
# print(line)
# print(re.match('<programlisting\\s+language=\'lilypond\'.*?(role=\'(?P<options>.*?)\')?>', line))
line = re.sub(
'<programlisting\\s+language=\'lilypond\'.*?(role=\'(?P<options>.*?)\')?>',
'<programlisting language="lilypond" role="\\g<options>">',
line
)
# print(line)
f_lily.write(line)
os.unlink(in_file)
# shutil.move(in_file, in_lily_file)
# Add LilyPond to the PATH.
if os.path.isdir(lilypond_folder):
os.environ['PATH'] += os.pathsep + lilypond_folder
# Start LilyPond on the copied file. First test the binary, then check if adding Python helps. # Start LilyPond on the copied file. First test the binary, then check if adding Python helps.
command = lilypond_command + ' --format=docbook ' + in_lily_file command_raw = lilypond_command + ' --format=docbook ' + in_lily_file
print(command) command_python = 'python -tt "' + lilypond_command + '" --format=docbook ' + in_lily_file
if os.system(command) != 0:
command = 'python -tt "' + lilypond_command + '" --format=docbook ' + in_lily_file if os.system(command_raw) == 0:
print(command) print("Success running LilyPond:")
if os.system(command) != 0: print(command_raw)
else:
if os.system(command_python) == 0:
print("Success running LilyPond:")
print(command_python)
else:
print('Error from LilyPond') print('Error from LilyPond')
sys.exit(1) sys.exit(1)