[Haskell-cafe] running haddock and lhs2TeX

Samuel Silva silva.samuel at gmail.com
Wed Jun 11 06:44:57 EDT 2008


Hello

I'm using haddock to my Literate Haskell code, and it runs very well.
I prefer Literate Haskell instead Haskell because old code remain untouched.
It also is possible run latex over my code.
But running lhs2TeX is even better.
However lhs2TeX doesn't proccess with some haddock information.
I won't change or format haddock documentation but using two tools is
a valuable help at development.
I would know that somebody solve thi issue and how?

To solve this issue I build processor, using perl, that escapes
characters like | and @.
This preprocessor just escapes characters within \begin{code} ... \end{code}.

#####################################################################
# This scripts process Literate Haskell Script itself to be process
#   by lhs2TeX with haddock comments.
#

use strict;
use warnings;
my @tag=();
while (<>) {
    if ($_ =~ /\\begin{(.*)}/) {push @tag => $1;}
    elsif ($_ =~ /\\end{(.*)}/ and $1 eq $tag[@tag-1]) {pop @tag;}
    if ( elem("code", at tag) and /(.*)--(.*)/) {
        print $1 => process($2) => "\n";
    }
    else {        print;    }
}
sub process{
    my ($input) = @_;
    $input =~ s/\|/\|\|/g;          # escapes |
    $input =~ s/\@/\@\@/g;    # escapes @
    $input =~ s/\^//g;               # drop ^
    $input =~ s/\"/\'/g;             # change " into '
    return "--$input";
}
sub elem {
    my ($x, at xs) = @_;
    my $res = 0;
    for (@xs) {$res=1 if $x eq $_}
    return $res;
}
#####################################################################


-- 
Don't hug that, Hugs ME!
Samuel Silva


More information about the Haskell-Cafe mailing list