Backport the latest changes to the instant preview scripts.

Also fix a glitch recently reported on the devel list.
No status line as this is a further amendement to b3d2ffc0.
This commit is contained in:
Enrico Forestieri 2015-06-04 17:58:55 +02:00
parent ebbe6c1924
commit 7013445bdd
3 changed files with 61 additions and 15 deletions

View File

@ -233,8 +233,11 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
\definecolor{bg}{rgb}{%s}
\pagecolor{bg}
\usepackage[%s,tightpage]{preview}
\IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecomp}}
\makeatletter
\def\t@a{cmr}
\if\f@family\t@a
\IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecompl}}
\fi
\g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath fill}\color{fg}}
\g@addto@macro\endpreview{\endgroup}
\makeatother
@ -302,7 +305,7 @@ def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color,
# Compile the latex file.
latex_status, latex_stdout = run_latex(latex, latex_file)
if latex_status:
warning("trying to recover from failed compilation")
progress("Will try to recover from %s failure" % latex)
if pdf_output:
return legacy_conversion_step3(latex_file, dpi, output_format, True, skipMetrics)
@ -315,10 +318,13 @@ def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color,
def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
use_pdftocairo, conv, gs_device, gs_ext, alpha, resolution, output_format):
error_count = 0
# Search for pdflatex executable
pdflatex = find_exe(["pdflatex"])
if pdflatex == None:
warning("Can't find pdflatex. Some pages failed with all the possible routes.")
failed_pages = []
else:
# Create a new LaTeX file from the original but only with failed pages
pdf_latex_file = latex_file_re.sub("_pdflatex.tex", latex_file)
@ -357,6 +363,7 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
if conv_status:
# Give up!
warning("Some pages failed with all the possible routes")
failed_pages = []
else:
# We've done it!
pdf_log_file = latex_file_re.sub(".log", pdf_latex_file)
@ -367,6 +374,7 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
for index in error_pages:
pdf_metrics.pop(index - 1)
pdf_metrics.insert(index - 1, (index, -1.0))
error_count += 1
original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file)
destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file)
@ -375,6 +383,8 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages,
original_bitmap, destination_bitmap)
return error_count
# The file has been processed through latex and we expect dvi output.
# Run dvips, taking note whether it was successful.
@ -513,9 +523,11 @@ def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMe
# Pass failed pages to pdflatex
if len(failed_pages) > 0:
warning("Now trying to obtain failed previews through pdflatex")
legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
use_pdftocairo, conv, gs_device, gs_ext, alpha, resolution,
output_format)
error_count = legacy_conversion_pdflatex(latex_file, failed_pages,
legacy_metrics, use_pdftocairo, conv, gs_device, gs_ext, alpha,
resolution, output_format)
else:
error_count = 0
# Invalidate metrics for pages that produced errors
if len(error_pages) > 0:
@ -523,6 +535,7 @@ def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMe
if index not in failed_pages:
legacy_metrics.pop(index - 1)
legacy_metrics.insert(index - 1, (index, -1.0))
error_count += 1
# Crop the ppm images
if pnmcrop != None and output_format == "ppm":
@ -534,6 +547,8 @@ def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMe
# Extract metrics info from the log file.
metrics_file = latex_file_re.sub(".metrics", latex_file)
write_metrics_info(legacy_metrics, metrics_file)
if error_count:
warning("Failed to produce %d preview snippet(s)" % error_count)
return (0, legacy_metrics)

View File

@ -158,12 +158,17 @@ def extract_metrics_info(dvipng_stdout):
return results
def fix_latex_file(latex_file):
def fix_latex_file(latex_file, pdf_output):
documentclass_re = re.compile("(\\\\documentclass\[)(1[012]pt,?)(.+)")
newcommandx_re = re.compile("(\\\\newcommandx)(\\\\[a-zA-Z])(.+)")
def_re = re.compile(r"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z])(.+)")
usepackage_re = re.compile("\\\\usepackage")
userpreamble_re = re.compile("User specified LaTeX commands")
enduserpreamble_re = re.compile("\\\\makeatother")
tmp = mkstemp()
in_user_preamble = 0
usepkg = 0
changed = 0
macros = []
for line in open(latex_file, 'r').readlines():
@ -173,7 +178,25 @@ def fix_latex_file(latex_file):
tmp.write("%s%s\n" % (match.group(1), match.group(3)))
continue
match = newcommandx_re.match(line)
if not pdf_output and not usepkg:
if userpreamble_re.search(line) != None:
in_user_preamble = 1
elif enduserpreamble_re.search(line) != None:
in_user_preamble = 0
if usepackage_re.match(line) != None and in_user_preamble:
usepkg = 1
changed = 1
tmp.write("\\def\\t@a{microtype}\n")
tmp.write("\\let\\oldusepkg\\usepackage\n")
tmp.write("\\def\\usepackage{\\@ifnextchar[\\@usepkg{\\@usepkg[]}}\n")
tmp.write("\\def\\@usepkg[#1]#2{\\@ifnextchar[")
tmp.write("{\\@@usepkg[#1]{#2}}{\\@@usepkg[#1]{#2}[]}}\n")
tmp.write("\\def\@@usepkg[#1]#2[#3]{\\def\\t@b{#2}")
tmp.write("\\ifx\\t@a\\t@b\\else\\oldusepkg[#1]{#2}[#3]\\fi}\n")
tmp.write(line)
continue
match = def_re.match(line)
if match == None:
tmp.write(line)
continue
@ -184,6 +207,10 @@ def fix_latex_file(latex_file):
tmp.write(line)
continue
definecmd = match.group(1)
if definecmd == "\\global\\long\\def":
tmp.write(line)
else:
changed = 1
tmp.write("\\renewcommandx%s%s\n" % (match.group(2), match.group(3)))
@ -405,9 +432,9 @@ def main(argv):
progress("Preprocess through lilypond-book: %s" % lilypond)
progress("Altering the latex file for font size and colors")
# Omit font size specification in latex file and make sure multiple
# defined macros are not an issue.
fix_latex_file(latex_file)
# Omit font size specification in latex file and make sure that multiple
# defined macros and the microtype package don't cause issues.
fix_latex_file(latex_file, pdf_output)
if lilypond:
progress("Preprocess the latex file through %s" % lilypond_book)
@ -452,7 +479,7 @@ def main(argv):
error_pages = []
latex_status, latex_stdout = run_latex(latex, latex_file, bibtex)
if latex_status:
warning("trying to recover from failed compilation")
progress("Will try to recover from %s failure" % latex)
error_pages = check_latex_log(latex_file_re.sub(".log", latex_file))
# The dvi output file name
@ -549,10 +576,14 @@ def main(argv):
# Invalidate metrics for pages that produced errors
if len(error_pages) > 0:
error_count = 0
for index in error_pages:
if index not in ps_pages and index not in pdf_pages:
dvipng_metrics.pop(index - 1)
dvipng_metrics.insert(index - 1, (index, -1.0))
error_count += 1
if error_count:
warning("Failed to produce %d preview snippet(s)" % error_count)
# Convert images to ppm format if necessary.
if output_format == "ppm":

View File

@ -348,7 +348,7 @@ def run_tex(tex, tex_file):
tex_status, tex_stdout = run_command(tex_call)
if tex_status:
warning("%s had problems compiling %s" \
progress("Warning: %s had problems compiling %s" \
% (os.path.basename(tex), tex_file))
return tex_status, tex_stdout