Jose' Matos wrote:
>> I'm trying to write 'lyxpreview2png.py', to be used by lyx to
>> generate previews using dvipng. What versions of python do we
>> support? (Should I go use 'atoi' or do it the 'int' way?)
> 
>    We support 1.5.2, so to be on the compatible side 'atoi' is the
>    way to go.

Thanks, Jose.

I have an 'almost finished' python script, attached. Would you mind
casting a look over it and telling me how to improve it?

One thing I don't know how to do:
    # Ascertain whether the latex and dvipng 
    # executables are available.
    latex = "latex"
    dvipng = "dvipng"

I'd like to write a little function to loop over a list of 'possible'
names (lib/configure checks for pplatex latex2e latex) and return the
first one found in the path. Any ideas on how to do that?

-- 
Angus
#! /usr/bin/env python

# file lyxpreview2png.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.
#
# author Angus Leeming
# with much advice from David Kastrup, [EMAIL PROTECTED]
#
# Full author contact details are available in file CREDITS

# This script takes a LaTeX file and generates a collection of
# png image files, one per previewed snippet.

# Pre-requisites:
# * A latex executable.
# * preview.sty
# * dvipng
# preview.sty and dvipng are part of the preview-latex project
# (http://preview-latex.sourceforge.net/).

# preview.sty can alternatively be obtained from
# CTAN/macros/latex/contrib/supported/preview.

# This script takes four arguments:
# TEXFILE:       the name of the .tex file to be converted.
# DPI:           a scale factor, passed to dvipng.
# FG_COLOR       the foreground color as a hexadecimal string, eg '000000'.
# BG_COLOR       the background color as a hexadecimal string, eg 'faf0e6'.

# If successful, this script will leave in dir ${DIR}:
# a (possibly large) number of image files with names like
#         ${BASE}\([0-9]*\).png
# a file containing info needed by LyX to position the images correctly
# on the screen.
#         ${BASE}.metrics

import os, re, sys
from string import atoi 

hexcolor_re = re.compile("^[0-9a-fA-F]{6}$")
latex_file_re = re.compile("\.tex$")
preview_re = re.compile("^Preview: [ST]")

def usage(prog_name):
    print "Usage: %s <latex file> <dpi> <fg color> <bg color>" % (prog_name)
    print "\twhere the colors are hexadecimal strings, eg 'faf0e6'"


def make_texcolor(hexcolor):
    # Test that the input string contains 6 hexadecimal chars
    if not hexcolor_re.match(hexcolor):
        print "Cannot convert '%s'" %(hexcolor)
        sys.exit(1)

    red   = float(atoi(hexcolor[0:2], 16)) / 255.0
    green = float(atoi(hexcolor[2:4], 16)) / 255.0
    blue  = float(atoi(hexcolor[4:6], 16)) / 255.0
    return "rgb %f %f %f" % (red, green, blue)


def run_command(prog_name, prog_call):
    pid = os.fork()
    if pid == 0:
        os.execvp(prog_name, prog_call)
        print "%s failed", prog_name
        os.exit(1)
    pid, exit_status = os.waitpid(pid, 0)
    return exit_status == 0


def extract_metrics_info(log_file, metrics_file):
    success = 0
    metrics = open(metrics_file, 'w')
    for line in open(log_file, 'r').readlines():
        if preview_re.match(line) > 0:
            success = 1
            metrics.write(line)
    return success


def main(argv):
    # Parse and manipulate the command line arguments
    if len(argv) != 5:
        usage(argv[0])
        sys.exit(1)

    latex_file = argv[1]
    dpi = int(argv[2])
    fg_color = make_texcolor(argv[3])
    bg_color = make_texcolor(argv[4])

    # Ascertain whether the latex and dvipng executables are available.
    latex = "latex"
    dvipng = "dvipng"

    # Compile the latex file.
    latex_call = [latex, latex_file]

    if not run_command(latex, latex_call):
        print "%s failed to compile %s" % (latex, latex_file)
        sys.exit(1)

    # Run the dvi file through dvipng.
    dvi_file = latex_file_re.sub(".dvi", latex_file)
    dvipng_call = [dvipng, "-T", "tight", "-depth", "-D", "%d" % dpi, \
                   "-fg", fg_color, "-bg", bg_color, dvi_file]

    if not run_command(dvipng, dvipng_call):
        print "%s failed to generate images from %s" % (dvipng, dvi_file)
        sys.exit(1)

    # Extract metrics info from the latex log file.
    log_file = latex_file_re.sub(".log", latex_file)
    metrics_file = latex_file_re.sub(".metrics", latex_file)
    if not extract_metrics_info(log_file, metrics_file):
        print "Failed to extract metrics info from %s" %(metrics_file)
        sys.exit(1)

if __name__ == "__main__":
    main(sys.argv)

Reply via email to