]> git.donarmstrong.com Git - lilypond.git/blobdiff - python/book_latex.py
Lilypond-book: don't crash in linewidth detection in latex mode when latex cannot...
[lilypond.git] / python / book_latex.py
index ec4ce93156565a0a7af613d1d295dda2ef2981bf..fbf98e66bf7fd8af28ffa2eccf6df4fec82d94f0 100644 (file)
@@ -3,9 +3,15 @@
 import re
 import tempfile
 import os
+import subprocess
 import book_base as BookBase
 from book_snippets import *
 import lilylib as ly
+global _;_=ly._
+
+progress = ly.progress
+warning = ly.warning
+error = ly.error
 
 # Recognize special sequences in the input.
 #
@@ -63,6 +69,17 @@ Latex_snippet_res = {
            (?P<filename>\S+?)
           })''',
 
+    'musicxml_file':
+         r'''(?smx)
+          ^[^%\n]*?
+          (?P<match>
+          \\musicxmlfile\s*(
+          \[
+           \s*(?P<options>.*?)\s*
+          \])?\s*\{
+           (?P<filename>\S+?)
+          })''',
+
     'singleline_comment':
          r'''(?mx)
           ^.*?
@@ -101,6 +118,7 @@ Latex_output = {
 
     OUTPUT: r'''{%%
 \parindent 0pt
+\noindent
 \ifx\preLilyPondExample \undefined
 \else
   \expandafter\preLilyPondExample
@@ -163,12 +181,20 @@ def get_latex_textwidth (source, global_options):
     tmp_handle.write (latex_document)
     tmp_handle.close ()
 
-    ly.system ('%s %s' % (global_options.latex_program, tmpfile),
-               be_verbose=global_options.verbose)
-    parameter_string = file (logfile).read()
-
+    progress (_ ("Running `%s' on file `%s' to detect default page settings.\n")
+              % (global_options.latex_program, tmpfile));
+    cmd = '%s %s' % (global_options.latex_program, tmpfile);
+    proc = subprocess.Popen (cmd,
+        universal_newlines=True, shell=True,
+        stdout=subprocess.PIPE, stderr=subprocess.PIPE);
+    if proc.returncode != 0:
+        warning (_ ("Unable to auto-detect default page settings:\n%s")
+                 % proc.communicate ()[1]);
     os.unlink (tmpfile)
-    os.unlink (logfile)
+    parameter_string = ""
+    if os.path.exists (logfile):
+        parameter_string = file (logfile).read()
+        os.unlink (logfile)
 
     columns = 0
     m = re.search ('columns=([0-9.]+)', parameter_string)
@@ -255,8 +281,7 @@ class BookLatexOutputFormat (BookBase.BookOutputFormat):
             str += "".ljust (breaks, "\n").replace ("\n","%\n")
 
         if QUOTE in snippet.option_dict:
-            rep['str'] = str
-            str = self.output[QUOTE] % rep
+            str = self.output[QUOTE] % {'str': str}
         return str