-# -*- coding: utf-8 -*-
-
# file lyxpreview2bitmap.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.
# Moreover dvipng can't work with PDF files, so, if the CONVERTER
# paramter is pdflatex we have to fallback to legacy route (step 2).
-from __future__ import print_function
import getopt, glob, os, re, shutil, sys, tempfile
pdflatex_commands, progress, run_command, run_latex, run_tex, \
warning, write_metrics_info
-PY2 = sys.version_info[0] == 2
-
def usage(prog_name):
msg = """
Usage: %s <options> <input file>
# "\[[0-9]+" can match two kinds of numbers: page numbers from dvipng
# and glyph numbers from mktexpk. The glyph numbers always match
# "\[[0-9]+\]" while the page number never is followed by "\]". Thus:
- page_re = re.compile("\[([0-9]+)[^]]");
+ page_re = re.compile(r"\[([0-9]+)[^]]");
metrics_re = re.compile("depth=(-?[0-9]+) height=(-?[0-9]+)")
success = 0
def convert_to_ppm_format(pngtopnm, basename):
- png_file_re = re.compile("\.png$")
+ png_file_re = re.compile(r"\.png$")
for png_file in glob.glob("%s*.png" % basename):
ppm_file = png_file_re.sub(".ppm", png_file)
- p2p_cmd = '%s "%s"' % (pngtopnm, png_file)
+ p2p_cmd = f'{pngtopnm} "{png_file}"'
p2p_status, p2p_stdout = run_command(p2p_cmd)
if p2p_status:
error("Unable to convert %s to ppm format" % png_file)
# and inclusion of PDF/PNG/JPG files.
# This is required for correct rendering of PSTricks and TikZ
dv2dt = find_exe_or_terminate(["dv2dt"])
- dv2dt_call = '%s "%s"' % (dv2dt, dvi_file)
+ dv2dt_call = f'{dv2dt} "{dvi_file}"'
# The output from dv2dt goes to stdout
dv2dt_status, dv2dt_output = run_command(dv2dt_call)
"debug", "dpi=", "fg=", "help", "latex=", "lilypond",
"lilypond-book=", "png", "ppm", "verbose"])
except getopt.GetoptError as err:
- error("%s\n%s" % (err, usage(script_name)))
+ error(f"{err}\n{usage(script_name)}")
opts.reverse()
for opt, val in opts:
# Determine input file
if len(args) != 1:
err = "A single input file is required, %s given" % (len(args) or "none")
- error("%s\n%s" % (err, usage(script_name)))
+ error(f"{err}\n{usage(script_name)}")
input_path = args[0]
dir, latex_file = os.path.split(input_path)
progress("Resolution (dpi): %s" % dpi)
progress("File to process: %s" % input_path)
- # 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)
+ fg_color = bytes(fg_color, 'ascii')
+ bg_color = bytes(bg_color, 'ascii')
- # For python > 2 convert bytes to string
- if not PY2:
- fg_color_dvipng = fg_color_dvipng.decode('ascii')
- bg_color_dvipng = bg_color_dvipng.decode('ascii')
+ fg_color_dvipng = make_texcolor(fg_color, False).decode('ascii')
+ bg_color_dvipng = make_texcolor(bg_color, False).decode('ascii')
# External programs used by the script.
latex = find_exe_or_terminate(latex or latex_commands)