ServDoc_latex - turn output of ServDoc modules in a readable LaTeX-page
$Id: ServDoc_latex,v 1.3 2004/01/03 20:43:25 uherbst Exp $
ServDoc_latex [-h|help] [-v|version] [--debug LATEX,intensity]
ServDoc_latex reads the collected output from different ServDoc modules from STDIN and tries to put that in a LaTeX Format.
Every Headline in the input becomes a Headline in the LaTeX-code.
Before and after the LaTeX-text, two file named ``header.tex'' and ``footer.tex'' (in the tex-subdirectory) will be included.
``%h'' in the header file will be substituted with the hostname.
``%r'' in the header file will be substituted with the ServDoc-Release.
The debug feature for that module is named ``LATEX''.
Ulrich Herbst <Ulrich.Herbst@gmx.de>
#!/usr/bin/perl
#---------------------------------------------------------------------- # standard perl modules use strict; # print error about unknown variables ... use English; # long internal variable names; use FindBin; # In which directory is ServDoc itself ? # There has to be the module and the lib dir! use lib $FindBin::Bin. "/lib"; # Here are the ServDoc-perl-modules use Sys::Hostname; # our own perl modules use ServDoc; use ServDocOutput; use XML::Simple::PurePerl; #---------------------------------------------------------------------- my $options; $options->{Version} = '$Name: $'; # $Name r0-7b $ -> r0-7b $options->{Version} =~ s/\$//g; $options->{Version} =~ s/Name: r(.*) .*/$1/; #0-7b -> 0.7b $options->{Version} =~ s/-/./g; # We need the cmdline to call the modules with the same debug options. $options->{cmdline} = join " ", @ARGV; sub debug { ServDoc_debug( "LATEX", $options, shift, shift ); } $options = &process_cmdline($options); debug( 9, "ServDoc_latex is running" ); #---------------------------------------------------------------------- # Main # include header open( FILE, "< $FindBin::Bin/tex/header.tex" ); my $HOSTNAME = hostname(); # %h -> hostname # %r -> CVS-Release while (<FILE>) { s/%h/$HOSTNAME/g; s/%r/$options->{Version}/g; print; } close(FILE); my @latex_headings = ( "chapter", "section", "subsection", "subsubsection", ); my $latex_output = ''; my @last_headings = (''); # Inputs are on STDIN my $xmldata=join "",<STDIN>; my $xmltree=XMLin($xmldata,keyattr=>"",forcearray=>1,forcecontent=>1); for (my $i=0; $i<=$#{@{$xmltree->{SDitem}}}; $i++) { my ($short_desc,$long_desc,$text,$tableref,@headings) = get_and_normalize_SDitem_data($xmltree->{SDitem}->[$i]); my $latex_new_content = ''; # $text shouldn't encode things, because $text is "verbatim" #$text = tex_encode($text); $short_desc = tex_encode($short_desc); $long_desc = tex_encode($long_desc); debug( 8, "---------------------------" ); my $heading_equal; for ( $heading_equal = 0 ; $heading_equal <= $#headings ; $heading_equal++ ) { if ( $last_headings[$heading_equal] ne $headings[$heading_equal] ) { last; } } debug( 8, "Actual Headings : " . join " .. ", @headings ); debug( 8, "Last Headings : " . join " .. ", @last_headings ); debug( 8, "Heading and previous are unequal from $heading_equal" ); # in $heading_equal is the number of the first unequal heading # between the new "@headings" and the headings from the last line; # Write new headings for ( my $x = $heading_equal ; $x < $#headings ; $x++ ) { my $hnr = ( $x > 3 ? 3 : $x ); $latex_output .= "\\$latex_headings[$hnr]\{$headings[$x]}\n"; } my $hnr .= ( $#headings > 3 ? 3 : $#headings ); $latex_output .= "\\$latex_headings[$hnr]\{$headings[-1]}\n"; if ($short_desc) { $latex_output .= "$short_desc\\\\\n"; } $latex_output .= "$long_desc\n"; # Are there tables to output ? if (ref($tableref) eq "ARRAY") { $latex_output .= make_table($tableref->[0]); } $latex_output .= "\\begin{verbatim}\n". $text . "\n\\end{verbatim}\n"; @last_headings=@headings; } # while (<>) print $latex_output; open( FILE, "< $FindBin::Bin/tex/footer.tex" ); while (<FILE>) { s/%h/$HOSTNAME/g; print; } close(FILE); sub tex_encode { my $string = shift; $string =~ s/_/\\_/g; $string =~ s/'/"/g; $string =~ s/\%/\\%/g; $string =~ s/\\%\+/%+/g; $string =~ s///g; return $string; } # make a LaTeX-Table: sub make_table { my $ref = shift; my $output; # First, we put the whole table in a 2d array. # Then we count the number of columns (for the TeX-Format-String) # and output that array # $array[$row][$column] my @array; my $row; my $column; for ($row=0; $row<=$#{@{$ref->{'tr'}}};$row++) { for ($column=0; $column <=$#{@{$ref->{'tr'}->[$row]->{th}}}; $column++) { $array[$row][$column] = tex_encode($ref->{'tr'}->[$row]->{th}->[$column]->{content}); }; for ($column=0; $column <=$#{@{$ref->{'tr'}->[$row]->{td}}}; $column++) { $array[$row][$column] = tex_encode($ref->{'tr'}->[$row]->{td}->[$column]->{content}); }; }; # rows $output = "\\begin{center}\n\\begin{tabular}{|" . "l|" x ($#{@array[0]}+1) . "}\n\\hline\n"; # Output @array for ($row=0; $row <= $#array; $row++) { $output .= $array[$row][0]; for ($column=1; $column <=$#{@array[0]};$column++) { $output .= " & " . $array[$row][$column];} $output .= "\\\\\\hline\n"; } $output .= "\\end{tabular}\n\\end{center}\n"; return $output; }