]> git.donarmstrong.com Git - lilypond.git/blobdiff - python/book_latex.py
Doc: fix broken ref
[lilypond.git] / python / book_latex.py
index fbf98e66bf7fd8af28ffa2eccf6df4fec82d94f0..f2aa5e36cf6106e2c427fe30465bf9c1d446be6f 100644 (file)
@@ -3,6 +3,7 @@
 import re
 import tempfile
 import os
+import sys
 import subprocess
 import book_base as BookBase
 from book_snippets import *
@@ -124,19 +125,20 @@ Latex_output = {
   \expandafter\preLilyPondExample
 \fi
 \def\lilypondbook{}%%
-\input %(base)s-systems.tex
+\input{%(base)s-systems.tex}
 \ifx\postLilyPondExample \undefined
 \else
   \expandafter\postLilyPondExample
 \fi
 }''',
 
-    PRINTFILENAME: '''\\texttt{%(filename)s}
+    PRINTFILENAME: r'''\texttt{%(filename)s}
+\linebreak
 ''',
 
-    QUOTE: r'''\begin{quotation}
+    QUOTE: r'''\begin{quote}
 %(str)s
-\end{quotation}''',
+\end{quote}''',
 
     VERBATIM: r'''\noindent
 \begin{verbatim}%(verb)s\end{verbatim}
@@ -174,8 +176,10 @@ def get_latex_textwidth (source, global_options):
     latex_document = LATEX_INSPECTION_DOCUMENT % {'preamble': preamble}
 
     (handle, tmpfile) = tempfile.mkstemp('.tex')
-    logfile = os.path.splitext (tmpfile)[0] + '.log'
-    logfile = os.path.split (logfile)[1]
+    tmpfileroot = os.path.splitext (tmpfile)[0]
+    tmpfileroot = os.path.split (tmpfileroot)[1]
+    auxfile = tmpfileroot + '.aux'
+    logfile = tmpfileroot + '.log'
 
     tmp_handle = os.fdopen (handle,'w')
     tmp_handle.write (latex_document)
@@ -184,14 +188,41 @@ def get_latex_textwidth (source, global_options):
     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]);
+    ly.debug_output ("Executing: %s\n" % cmd);
+    run_env = os.environ.copy()
+    run_env['LC_ALL'] = 'C'
+
+    ### unknown why this is necessary
+    universal_newlines = True
+    if sys.platform == 'mingw32':
+        universal_newlines = False
+        ### use os.system to avoid weird sleep() problems on
+        ### GUB's python 2.4.2 on mingw
+        # make file to write to
+        output_dir = tempfile.mkdtemp()
+        output_filename = os.path.join(output_dir, 'output.txt')
+        # call command
+        cmd += " > %s" % output_filename
+        returncode = os.system(cmd)
+        parameter_string = open(output_filename).read()
+        if returncode != 0:
+            warning (_ ("Unable to auto-detect default settings:\n"))
+        # clean up
+        os.remove(output_filename)
+        os.rmdir(output_dir)
+    else:
+        proc = subprocess.Popen (cmd,
+            env=run_env,
+            universal_newlines=universal_newlines,
+            shell=True,
+            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        (parameter_string, error_string) = proc.communicate ()
+        if proc.returncode != 0:
+            warning (_ ("Unable to auto-detect default settings:\n%s")
+                    % error_string)
     os.unlink (tmpfile)
-    parameter_string = ""
+    if os.path.exists (auxfile):
+        os.unlink (auxfile)
     if os.path.exists (logfile):
         parameter_string = file (logfile).read()
         os.unlink (logfile)
@@ -267,8 +298,11 @@ class BookLatexOutputFormat (BookBase.BookOutputFormat):
     def snippet_output (self, basename, snippet):
         str = ''
         rep = snippet.get_replacements ();
-        rep['base'] = basename
-        str += self.output_print_filename (basename, snippet)
+        rep['base'] = basename.replace ('\\', '/')
+        rep['filename'] = os.path.basename (snippet.filename).replace ('\\', '/')
+        rep['ext'] = snippet.ext
+        if PRINTFILENAME in snippet.option_dict:
+            str += self.output[PRINTFILENAME] % rep
         if VERBATIM in snippet.option_dict:
             rep['verb'] = snippet.verb_ly ()
             str += self.output[VERBATIM] % rep