Using pygmentize with WML

My pygmentize macro for WML does exactly what it's name says: it processes its content mit [Ext. Link]Pygments' pygmentize source highlighter.
So it turns this piece of wml-code:
<pygmentize lang=scala>
/* Don't see this class */
case class Fnord(foo: Int)
into formatted html that looks like this:
/* Don't see this class */
case class Fnord(foo: Int)
Here is the code:
<define-tag pygmentize endtag=required>
<preserve lang />
<set-var %attributes />
 my $lang=qq/<get-var lang />/;
 open OUT, "| perl -pe 's/^\\\\#/#/;'>Tmp$$";
 close OUT;
 open IN, "pygmentize -l $lang -f html Tmp$$ |";
 while (<IN>) {
   print $_;
 close IN;
<restore lang />
It will fail horribly for scala code that contains xml literals that look like mp4h macros. The slightly safer way to call it is
<pygmentize lang=xml><protect pass=2>
<div>XML Content Here</div>
<div>XML Content Here</div>
Another pitfall is that is runs after wml_p1_ipp, so you have to escape hash marks at the start of a line and backslashes at the end.
Florian Hars <>, 2009-05-22 (orig: 2009-01-24)