views:

29

answers:

1

Background

OpenOffice Writer lacks the ability to link to an ASCII text source file, apply syntax highlighting, wrap it in a frame, and update the frame contents whenever the source file changes. However, OpenOffice Writer can link to images, and will update the images automatically when they change.

Problem

The images need to be high-resolution (300 dpi or greater) with syntax colouring appropriate for a white background (i.e., a printed page).

Question

How can high-quality images be created automatically from source code files, such as:

  • SQL;
  • PostgreSQL functions;
  • Java;
  • bash scripts; and
  • R and PL/R?

Attempts

Most attempts have been a variation on the following theme:

$ enscript --color -f Courier12 -B -1 --highlight=sql -h -o - source.sql |\
  convert - -trim -border 10 source.png

There are a few problems with this approach:

  1. The resolution is lacking (using -resample and -density offer no improvement).
  2. The syntax highlighting is unsuitable for a white page (can probably change enscript's colour theme).
  3. Using Courier100 produces several .png files, which would need to be stitched together.
  4. The -border 10 unexpectedly changes the background colour from white to lightgray.

Manual Solution

Converting the source files to PostScript -- avoiding ImageMagick altogether -- and then importing them into The GIMP will produce the desired results. Unfortunately, that solution involves a bit of manual work, and my GIMP batch programming experience is next to nil.

Thank you!

+1  A: 

Not ideal, but this works:

$ enscript -f Courier16 -X ps -B -1 --highlight=sql -h -o - source.sql | \
  gs -dSAFER -sDEVICE=png16m -dGraphicsAlphaBits=4 -r300 \
  -sOutputFile=source.png -dNOPAUSE > /dev/null && \
  convert -trim source.png source-trimmed.png && \
  mv source-trimmed.png source.png
Dave Jarvis
@Dave Jarvis: Excellent answer!
pipitas