X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=python%2Fbook_latex.py;h=ec5412b1d8192bee41e292e31bc62a78798cc45d;hb=5d84bfad4626892bcffd05adcced53c8a2329047;hp=be7c4e18830c2b10a014c6f61e0a635c10ea7865;hpb=7488b4ec5c3042b30b106cbf77186117def9c934;p=lilypond.git diff --git a/python/book_latex.py b/python/book_latex.py index be7c4e1883..ec5412b1d8 100644 --- a/python/book_latex.py +++ b/python/book_latex.py @@ -13,6 +13,7 @@ global _;_=ly._ progress = ly.progress warning = ly.warning error = ly.error +debug = ly.debug_output # Recognize special sequences in the input. # @@ -132,7 +133,8 @@ Latex_output = { \fi }''', - PRINTFILENAME: '''\\texttt{%(filename)s} + PRINTFILENAME: r'''\texttt{%(filename)s} +\linebreak ''', QUOTE: r'''\begin{quote} @@ -164,12 +166,13 @@ LATEX_INSPECTION_DOCUMENT = r''' # Do we need anything else besides `textwidth'? def get_latex_textwidth (source, global_options): + # default value + textwidth = 550.0 + m = re.search (r'''(?P\\begin\s*{document})''', source) if m == None: warning (_ ("cannot find \\begin{document} in LaTeX document")) - - ## what's a sensible default? - return 550.0 + return textwidth preamble = source[:m.start (0)] latex_document = LATEX_INSPECTION_DOCUMENT % {'preamble': preamble} @@ -185,11 +188,13 @@ def get_latex_textwidth (source, global_options): tmp_handle.close () 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); - ly.debug_output ("Executing: %s\n" % cmd); + % (global_options.latex_program, tmpfile)) + cmd = '%s %s' % (global_options.latex_program, tmpfile) + debug ("Executing: %s\n" % cmd) run_env = os.environ.copy() run_env['LC_ALL'] = 'C' + run_env['TEXINPUTS'] = '%s:%s' % \ + (global_options.input_dir, run_env.get('TEXINPUTS',"")) ### unknown why this is necessary universal_newlines = True @@ -202,7 +207,13 @@ def get_latex_textwidth (source, global_options): output_filename = os.path.join(output_dir, 'output.txt') # call command cmd += " > %s" % output_filename + oldtexinputs = os.environ.get ('TEXINPUTS') + os.environ['TEXINPUTS'] = run_env['TEXINPUTS'] returncode = os.system(cmd) + if oldtexinputs: + os.environ['TEXINPUTS'] = oldtexinputs + else: + del os.environ['TEXINPUTS'] parameter_string = open(output_filename).read() if returncode != 0: warning (_ ("Unable to auto-detect default settings:\n")) @@ -236,12 +247,22 @@ def get_latex_textwidth (source, global_options): if m: columnsep = float (m.group (1)) - textwidth = 0 m = re.search ('textwidth=([0-9.]+)pt', parameter_string) if m: textwidth = float (m.group (1)) - if columns: - textwidth = (textwidth - columnsep) / columns + else: + warning (_ ("cannot detect textwidth from LaTeX")) + return textwidth + + debug ('Detected values:') + debug (' columns = %s' % columns) + debug (' columnsep = %s' % columnsep) + debug (' textwidth = %s' % textwidth) + + if m and columns: + textwidth = (textwidth - columnsep) / columns + debug ('Adjusted value:') + debug (' textwidth = %s' % textwidth) return textwidth @@ -282,9 +303,10 @@ class BookLatexOutputFormat (BookBase.BookOutputFormat): def input_fullname (self, input_filename): # Use kpsewhich if available, otherwise fall back to the default: if ly.search_exe_path ('kpsewhich'): - return os.popen ('kpsewhich ' + input_filename).read()[:-1] - else: - return BookBase.BookOutputFormat.input_fullname (self, input_filename) + trial = os.popen ('kpsewhich ' + input_filename).read()[:-1] + if trial: + return trial + return BookBase.BookOutputFormat.input_fullname (self, input_filename) def process_chunks (self, chunks): for c in chunks: @@ -298,7 +320,10 @@ class BookLatexOutputFormat (BookBase.BookOutputFormat): str = '' rep = snippet.get_replacements (); rep['base'] = basename.replace ('\\', '/') - str += self.output_print_filename (basename, snippet) + 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