The Accidental Rubyist

invalid byte sequence in UTF-8

Pretty Print Code script (hack)

The hack presented below has been developed on a MAC OS X. It uses the TOhtml function of the Vim Editor (i use Version 7.0). It runs in HTML Mode only.

f=$1vim -f +"syn on" +"run! syntax/2html.vim" +"wq" +"q" $f

sed  -n '/<pre>/,/<\\/pre>/p' $f.html | sed 's~<pre>~<pre style="margin: 1.5em 0px; padding: 10px 10px 10px 10px; color: #333; background: #000000;color:#ffffff; font-family: monaco,monospace; font-size: 120%; border-left: solid #ccc 1px; overflow: auto;">~p' > $f

rm $f.html

Line 1. saves temp file name to a variable f.

Line 2. Invokes a vim plugin on the temporary file. Vim creates a file with an html extension.

Line 3. Extracts lines from output file between PRE tags. Then replace opening PRE tag with out own styling. This is required for me, since this blog does not allow me to modify the CSS.

Line 4. Delete the temp file created by Vim.

Note: If most of the code you post is in one language then the input file given to Vim should have that extension. That way the Vim plugin will do a more comprehensive syntax highlighting.

The other option is to detect the programming language from the first line of the temp file (assuming it has the interpreter name in it) and rename the file accordingly.

Source (please note that this program does not treat the escape character \, and WP does not print the \)

Advertisements

Written by totalrecall

March 1, 2007 at 1:00 pm

Posted in ecto, Mac OS X, ruby, vim

%d bloggers like this: