commit e983676f6c9b53c94c0c94c2805073a8fa6832c8
Author: Thibaut Cuvelier <[email protected]>
Date:   Sun Sep 26 21:00:59 2021 +0200

    DocBook: work around bug in LilyPond.
    
    https://lists.gnu.org/archive/html/bug-lilypond/2021-09/msg00039.html
---
 lib/scripts/docbook_copy.py |   60 +++++++++++++++++++++++++++++++++++-------
 1 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/lib/scripts/docbook_copy.py b/lib/scripts/docbook_copy.py
index 8e00db7..034d504 100644
--- a/lib/scripts/docbook_copy.py
+++ b/lib/scripts/docbook_copy.py
@@ -19,6 +19,8 @@
 
 
 import os
+import os.path
+import re
 import shutil
 import sys
 
@@ -42,21 +44,59 @@ def copy_docbook(args):
 
     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.
     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")
-        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.
-        command = lilypond_command + ' --format=docbook ' + in_lily_file
-        print(command)
-        if os.system(command) != 0:
-            command = 'python -tt "' + lilypond_command + '" --format=docbook 
' + in_lily_file
-            print(command)
-            if os.system(command) != 0:
+        command_raw = lilypond_command + ' --format=docbook ' + in_lily_file
+        command_python = 'python -tt "' + lilypond_command + '" 
--format=docbook ' + in_lily_file
+
+        if os.system(command_raw) == 0:
+            print("Success running LilyPond:")
+            print(command_raw)
+        else:
+            if os.system(command_python) == 0:
+                print("Success running LilyPond:")
+                print(command_python)
+            else:
                 print('Error from LilyPond')
                 sys.exit(1)
 
-- 
lyx-cvs mailing list
[email protected]
http://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to