python3: fix the preview framework to work with both python 2 and 3 (part 2)

This commit is contained in:
José Matos 2017-05-11 15:50:08 +01:00
parent 2488bf6eaa
commit c546977c6e
3 changed files with 25 additions and 19 deletions

View File

@ -201,7 +201,7 @@ def extract_resolution(log_file, dpi):
def legacy_latex_file(latex_file, fg_color, bg_color):
use_preview_re = re.compile(r"\s*\\usepackage\[([^]]+)\]{preview}")
use_preview_re = re.compile(b"\\s*\\\\usepackage\\[([^]]+)\\]{preview}")
fg_color_gr = make_texcolor(fg_color, True)
bg_color_gr = make_texcolor(bg_color, True)
@ -209,7 +209,7 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
success = 0
try:
f = open(latex_file, 'r')
f = open(latex_file, 'rb')
except:
# Unable to open the file, but do nothing here because
# the calling function will act on the value of 'success'.
@ -227,20 +227,20 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
success = 1
# Package order: color should be loaded before preview
# Preview options: add the options lyx and tightpage
tmp.write(r"""
\usepackage{color}
\definecolor{fg}{rgb}{%s}
\definecolor{bg}{rgb}{%s}
\pagecolor{bg}
\usepackage[%s,tightpage]{preview}
\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
tmp.write(b"""
\\usepackage{color}
\\definecolor{fg}{rgb}{%s}
\\definecolor{bg}{rgb}{%s}
\\pagecolor{bg}
\\usepackage[%s,tightpage]{preview}
\\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
""" % (fg_color_gr, bg_color_gr, match.group(1)))
if success:

View File

@ -87,6 +87,7 @@ from lyxpreview_tools import bibtex_commands, check_latex_log, copyfileobj, \
mkstemp, pdflatex_commands, progress, run_command, run_latex, run_tex, \
warning, write_metrics_info
PY2 = sys.version_info[0] == 2
def usage(prog_name):
msg = """
@ -385,6 +386,11 @@ def main(argv):
if len(dir) != 0:
os.chdir(dir)
# For python > 2 convert strings to bytes
if not PY2:
fg_color = bytes(fg_color, 'ascii')
bg_color = bytes(bg_color, 'ascii')
fg_color_dvipng = make_texcolor(fg_color, False)
bg_color_dvipng = make_texcolor(bg_color, False)

View File

@ -72,7 +72,7 @@ def error(message):
def make_texcolor(hexcolor, graphics):
# Test that the input string contains 6 hexadecimal chars.
hexcolor_re = re.compile("^[0-9a-fA-F]{6}$")
hexcolor_re = re.compile(b"^[0-9a-fA-F]{6}$")
if not hexcolor_re.match(hexcolor):
error("Cannot convert color '%s'" % hexcolor)
@ -81,9 +81,9 @@ def make_texcolor(hexcolor, graphics):
blue = float(int(hexcolor[4:6], 16)) / 255.0
if graphics:
return "%f,%f,%f" % (red, green, blue)
return b"%f,%f,%f" % (red, green, blue)
else:
return "rgb %f %f %f" % (red, green, blue)
return b"rgb %f %f %f" % (red, green, blue)
def find_exe(candidates):