Some clean up of the lyxpreview code.

Move some common variables to lyxpreview_tools. Otherwise,
add a few variable names, making it easier to track things.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@39656 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Julien Rioux 2011-09-11 18:22:29 +00:00
parent 2dfa11904f
commit 675f233f34
3 changed files with 57 additions and 46 deletions

View File

@ -74,15 +74,11 @@
import glob, os, pipes, re, string, sys
from lyxpreview_tools import copyfileobj, error, find_exe, \
find_exe_or_terminate, make_texcolor, mkstemp, run_command, warning, \
write_metrics_info, filter_pages, join_metrics_and_rename
from lyxpreview_tools import copyfileobj, error, filter_pages, find_exe, \
find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
latex_file_re, make_texcolor, mkstemp, run_command, warning, \
write_metrics_info
# Pre-compiled regular expression.
latex_file_re = re.compile("\.tex$")
# PATH environment variable
path = string.split(os.environ["PATH"], os.pathsep)
def usage(prog_name):
return "Usage: %s <latex file> <dpi> ppm <fg color> <bg color>\n" \
@ -250,12 +246,13 @@ def crop_files(pnmcrop, basename):
def legacy_conversion(argv, skipMetrics = False):
latex_commands = ["latex", "pplatex", "platex", "latex2e"]
# Parse and manipulate the command line arguments.
if len(argv) == 7:
latex_commands = [argv[6]]
latex = [argv[6]]
elif len(argv) != 6:
error(usage(argv[0]))
else:
latex = None
dir, latex_file = os.path.split(argv[1])
if len(dir) != 0:
@ -270,7 +267,7 @@ def legacy_conversion(argv, skipMetrics = False):
bg_color_gr = make_texcolor(argv[5], True)
# External programs used by the script.
latex = find_exe_or_terminate(latex_commands, path)
latex = find_exe_or_terminate(latex or latex_commands)
# Move color information into the latex file.
if not legacy_latex_file(latex_file, fg_color, bg_color, bg_color_gr):
@ -293,7 +290,7 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
gs_device, gs_ext, alpha, resolution, output_format):
# Search for pdflatex executable
pdflatex = find_exe(["pdflatex"], path)
pdflatex = find_exe(["pdflatex"])
if pdflatex == None:
warning("Can't find pdflatex. Some pages failed with all the possible routes.")
else:
@ -333,9 +330,9 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
def legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics = False):
# External programs used by the script.
dvips = find_exe_or_terminate(["dvips"], path)
gs = find_exe_or_terminate(["gswin32c", "gs"], path)
pnmcrop = find_exe(["pnmcrop"], path)
dvips = find_exe_or_terminate(["dvips"])
gs = find_exe_or_terminate(["gswin32c", "gs"])
pnmcrop = find_exe(["pnmcrop"])
# Run the dvi file through dvips.
dvi_file = latex_file_re.sub(".dvi", latex_file)

View File

@ -66,19 +66,14 @@
import glob, os, re, string, sys
from legacy_lyxpreview2ppm import legacy_conversion, \
legacy_conversion_step2, legacy_extract_metrics_info, filter_pages
legacy_conversion_step2, legacy_extract_metrics_info
from lyxpreview_tools import copyfileobj, error, find_exe, \
find_exe_or_terminate, make_texcolor, mkstemp, run_command, warning, \
write_metrics_info, join_metrics_and_rename
from lyxpreview_tools import copyfileobj, error, filter_pages, find_exe, \
find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
latex_file_re, make_texcolor, mkstemp, run_command, warning, \
write_metrics_info
# Pre-compiled regular expressions.
latex_file_re = re.compile("\.tex$")
# PATH environment variable
path = string.split(os.environ["PATH"], os.pathsep)
def usage(prog_name):
return "Usage: %s <format> <latex file> <dpi> <fg color> <bg color>\n" \
"\twhere the colors are hexadecimal strings, eg 'faf0e6'" \
@ -215,7 +210,7 @@ def find_ps_pages(dvi_file):
# Check for PostScript specials in the dvi, badly supported by dvipng
# This is required for correct rendering of PSTricks and TikZ
dv2dt = find_exe_or_terminate(["dv2dt"], path)
dv2dt = find_exe_or_terminate(["dv2dt"])
dv2dt_call = '%s "%s"' % (dv2dt, dvi_file)
# The output from dv2dt goes to stdout
@ -290,40 +285,47 @@ def main(argv):
if len(argv) != 6 and len(argv) != 7:
error(usage(argv[0]))
script_name = argv[0]
output_format = string.lower(argv[1])
dir, latex_file = os.path.split(argv[2])
input_path = argv[2]
dir, latex_file = os.path.split(input_path)
if len(dir) != 0:
os.chdir(dir)
dpi = string.atoi(argv[3])
fg_color = make_texcolor(argv[4], False)
bg_color = make_texcolor(argv[5], False)
fg_color = argv[4]
bg_color = argv[5]
fg_color_gr = make_texcolor(argv[4], True)
bg_color_gr = make_texcolor(argv[5], True)
fg_color_dvipng = make_texcolor(fg_color, False)
bg_color_dvipng = make_texcolor(bg_color, False)
fg_color_gr = make_texcolor(fg_color, True)
bg_color_gr = make_texcolor(bg_color, True)
# External programs used by the script.
if len(argv) == 7:
latex = argv[6]
latex = [argv[6]]
else:
latex = find_exe_or_terminate(["latex", "pplatex", "platex", "latex2e"], path)
latex = None
latex = find_exe_or_terminate(latex or latex_commands)
# Omit font size specification in latex file.
fix_latex_file(latex_file)
# This can go once dvipng becomes widespread.
dvipng = find_exe(["dvipng"], path)
dvipng = find_exe(["dvipng"])
if dvipng == None:
# The data is input to legacy_conversion in as similar
# as possible a manner to that input to the code used in
# LyX 1.3.x.
vec = [ argv[0], argv[2], argv[3], argv[1], argv[4], argv[5], latex ]
vec = [ script_name, input_path, str(dpi), output_format, fg_color, bg_color, latex ]
return legacy_conversion(vec)
pngtopnm = ""
if output_format == "ppm":
pngtopnm = find_exe_or_terminate(["pngtopnm"], path)
pngtopnm = find_exe_or_terminate(["pngtopnm"])
# Move color information for PDF into the latex file.
if not color_pdf(latex_file, bg_color_gr, fg_color_gr):
@ -365,12 +367,12 @@ def main(argv):
# If all pages need PostScript, directly use the legacy method.
if len(ps_pages) == page_count:
vec = [argv[0], argv[2], argv[3], argv[1], argv[4], argv[5], latex]
vec = [ script_name, input_path, str(dpi), output_format, fg_color, bg_color, latex ]
return legacy_conversion(vec)
# Run the dvi file through dvipng.
dvipng_call = '%s -Ttight -depth -height -D %d -fg "%s" -bg "%s" %s "%s"' \
% (dvipng, dpi, fg_color, bg_color, pages_parameter, dvi_file)
% (dvipng, dpi, fg_color_dvipng, bg_color_dvipng, pages_parameter, dvi_file)
dvipng_status, dvipng_stdout = run_command(dvipng_call)
if dvipng_status != None:
@ -391,8 +393,8 @@ def main(argv):
filter_pages(latex_file, legacy_latex_file, ps_pages)
# Pass the new LaTeX file to the legacy method
vec = [ argv[0], latex_file_re.sub("_legacy.tex", argv[2]),
argv[3], argv[1], argv[4], argv[5], latex ]
vec = [ script_name, latex_file_re.sub("_legacy.tex", input_path),
str(dpi), output_format, fg_color, bg_color, latex ]
legacy_metrics = legacy_conversion(vec, True)[1]
# Now we need to mix metrics data from dvipng and the legacy method

View File

@ -16,6 +16,20 @@
import os, re, string, sys, tempfile
# Known flavors of latex
latex_commands = ("latex", "pplatex", "platex", "latex2e")
pdflatex_commands = ("pdflatex", "xelatex", "lualatex")
# Pre-compiled regular expressions
latex_file_re = re.compile(r"\.tex$")
# PATH and PATHEXT environment variables
path = os.environ["PATH"].split(os.pathsep)
extlist = ['']
if "PATHEXT" in os.environ:
extlist += os.environ["PATHEXT"].split(os.pathsep)
use_win32_modules = 0
if os.name == "nt":
use_win32_modules = 1
@ -59,10 +73,8 @@ def make_texcolor(hexcolor, graphics):
return "rgb %f %f %f" % (red, green, blue)
def find_exe(candidates, path):
extlist = ['']
if "PATHEXT" in os.environ:
extlist = extlist + os.environ["PATHEXT"].split(os.pathsep)
def find_exe(candidates):
global extlist, path
for prog in candidates:
for directory in path:
@ -78,8 +90,8 @@ def find_exe(candidates, path):
return None
def find_exe_or_terminate(candidates, path):
exe = find_exe(candidates, path)
def find_exe_or_terminate(candidates):
exe = find_exe(candidates)
if exe == None:
error("Unable to find executable from '%s'" % string.join(candidates))