#! /usr/bin/perl # # This is a simple texi2html converter # # Why I wrote it? # # I wanted to have a small navigational TOC in front of each section and subsection. # I have spent more than a day hunting on information how to do it using texi2html # # I produced this program in 2 hours. (July 26, 2001. 8:06 to 10:03am) # # (Later I extended it, so that took some more time.) # # It is not as functional as the original texi2html, but does what I want. # # Peter Verhas # # sub getitoc { my $p = shift; my $i,$r; return $ITOC{$p} if defined $ITOC{$p}; $r = $p; $p = lc $p; $p =~ s/\W//g; return $ITOC{$p} if defined $ITOC{$p}; for $i (@TOCI){ my $k = lc $TOC{$i}; $k =~ s/\W//; # if( $k eq $p ){ print "Warning: $r is sloppy. It is supposed to be $TOC{$i}\n"; } return $i if $k eq $p; } $p = lc $r; $p =~ s/\s.*$//; $p =~ s/\W.*$//; for $i (@TOCI){ my $k = lc $TOC{$i}; $k =~ s/\s.*$//; $k =~ s/\W.*$//; # if( $k eq $p ){ "Warning: $r is sloppy. It is supposed to be $TOC{$i}\n"; } return $i if $k eq $p; } # print "Warning: xref $r is not defined\n"; return undef; } sub rtfizee { my $line = shift; my $filext = shift; $filext = 'html' unless defined $filext; $line =~ s[\@\@][SAVEALLDOUBLESOBAKA]g; $line =~ s[\\][SAVEALLDBACKSLASH]g; $line =~ s[\@file\{(.*?)\}][\`\{\\f1\\fs18 $1\}\']g; $line =~ s[\@code\{(.*?)\}][\{\\f1\\fs18 $1\}]g; $line =~ s[\@var\{(.*?)\}][\{\\f1\\fs18 $1\}]g; $line =~ s[\@command\{(.*?)\}][\{\\f1\\fs18 $1\}]g; $line =~ s[\@acronym\{(.*?)\}][\{\\f1\\fs18 $1\}]g; $line =~ s[\@b\{(.*?)\}][\{\\b\\f0\\fs18 $1\}]g; $line =~ s[\@strong\{(.*?)\}][\{\\i\\f0\\fs18 $1\}]g; $line =~ s[\@emph\{(.*?)\}][\{\\i\\f0\\fs18 $1\}]g; $line =~ s[\@option\{(.*?)\}][\`{\\f1\\fs18 $1\}\']g; $line =~ s[\@itemize][]g; $line =~ s[\@end\s+itemize][]g; $line =~ s[\@item][]g; $line =~ s[\@\{][\\\{]g; $line =~ s[\@\}][\\\}]g; $line =~ s[\@\$][\$]g; my @linarr = split /\n/ , $line; my $verbatim = 0; for $L ( @linarr ){ if( $L =~ /\@example/ ){ if( $verbatim ){ print $errs; die "Doubled verbatim"; } $errs = ''; $verbatim = 1; $L = "\{\\f1\\fs18"; next; } if( $L =~ /\@end\s+example/ ){ if( ! $verbatim ){ die "Unopened verbatim"; } $L = "\}\n"; $verbatim = 0; next; } if( $verbatim ){ $errs .= $L . "\n"; $L = "$L\\par"; } } if( $verbatim ){ die "unclosed verbatim"; } $line = join("\n",@linarr); # $line =~ s[\@example][\{\\f1\\fs18 ]g; # $line =~ s[\@end\s+example][\\par\}]g; # $line =~ s[\;][\\;]g; if( $filext eq '#' ){ while( $line =~ m[\@xref\{(.*?)\}] ){ my $itoc = &getitoc($1); $line =~ s[\@xref\{(.*?)\}][$1]; } }else{ while( $line =~ m[\@xref\{(.*?)\}] ){ my $itoc = &getitoc($1); $line =~ s[\@xref\{(.*?)\}][$1]; } } $line =~ s[\@uref\{(.*?),(.*?)\}][$2]g; $line =~ s[\@uref\{(.*?)\}][$1]g; $line =~ s[\@email\{(.*?)\}][\{\\f1\\fs18 $1\}]g; $line =~ s[SAVEALLDOUBLESOBAKA][\@]g; $line =~ s[SAVEALLDBACKSLASH][\\\\]g; $line =~ s/\n\n/\n\\par\\par\n/g; $line; } sub htmlizee { my $line = shift; my $filext = shift; $filext = 'html' unless defined $filext; $line =~ s[\@\@][SAVEALLDOUBLESOBAKA]g; $line =~ s[\&][\&]g; $line =~ s[\<][\<]g; $line =~ s[\>][\>]g; $line =~ s[\@file\{(.*?)\}][\`$1\']g; $line =~ s[\@code\{(.*?)\}][$1]g; $line =~ s[\@var\{(.*?)\}][$1]g; $line =~ s[\@command\{(.*?)\}][$1]g; $line =~ s[\@acronym\{(.*?)\}][$1]g; $line =~ s[\@b\{(.*?)\}][$1]g; $line =~ s[\@strong\{(.*?)\}][$1]g; $line =~ s[\@emph\{(.*?)\}][$1]g; $line =~ s[\@option\{(.*?)\}][\`$1\']g; $line =~ s[\@itemize][]g; $line =~ s[\@item][
  • ]g; $line =~ s[\@example][
    ]g;
      $line =~ s[\@end\s+example][
    ]g; $line =~ s[\@\{][\{]g; $line =~ s[\@\}][\}]g; $line =~ s[\@\$][\$]g; if( $filext eq '#' ){ while( $line =~ m[\@xref\{(.*?)\}] ){ my $itoc = &getitoc($1); $line =~ s[\@xref\{(.*?)\}][$1]; } }else{ while( $line =~ m[\@xref\{(.*?)\}] ){ my $itoc = &getitoc($1); $line =~ s[\@xref\{(.*?)\}][$1]; } } $line =~ s[\@uref\{(.*?),(.*?)\}][$2]g; $line =~ s[\@uref\{(.*?)\}][$1]g; $line =~ s[\@email\{(.*?)\}][$1]g; $line =~ s[SAVEALLDOUBLESOBAKA][\@]g; $line =~ s/\n\n/\n\\n/g; $line; } $infile = shift; $outfile = shift; open(F,$infile) or die "Can not open input file $infile"; @lines = ; close F; $BODYSTART = < END # for the chtm file $HBODYSTART = < END $BODYEND = < END %TOC = (); %TOCh = (); %ITOC = (); @TOCI = (); %BODY = (); $level = 0; $chapter = 0; $section = 0; $subsection = 0; $subsubsection = 0; $bodindex = undef; for $line (@lines){ # lines that are ignored if( $line =~ /^\@title\s+(.*)$/ ){ $TITLE = &htmlizee($1); next; } if( $line =~ /^\@author\s+(.*)$/ ){ $AUTHOR = &htmlizee($1); next; } if( $line =~ /^\@chapter\s+(.*)$/ ){ $chapter++; $section = 0; $subsection = 0; $subsubsection = 0; $bodindex = $chapter . '.'; $TOC{$bodindex} = $1; $ITOC{$1} = $bodindex; push @TOCI,$bodindex; next; } if( $line =~ /^\@section\s+(.*)$/ ){ $section++; $subsection = 0; $subsubsection = 0; $bodindex = $chapter . '.' . $section . '.' ; $TOC{$bodindex} = $1; $ITOC{$1} = $bodindex; push @TOCI,$bodindex; next; } if( $line =~ /^\@subsection\s+(.*)$/ ){ $subsection++; $subsubsection = 0; $bodindex = $chapter . '.' . $section . '.' . $subsection . '.' ; $TOC{$bodindex} = $1; $ITOC{$1} = $bodindex; push @TOCI,$bodindex; next; } if( $line =~ /^\@subsubsection\s+(.*)$/ ){ $subsubsection++; $bodindex = $chapter . '.' . $section . '.' . $subsection . '.' . $subsubsection . '.'; $TOC{$bodindex} = $1; $ITOC{$1} = $bodindex; push @TOCI,$bodindex; next; } $BODY{$bodindex} .= $line; } if( defined( $outfile ) ){ $infile = $outfile; }else{ $infile =~ s/\..*$//; } open(RTF,">$infile.rtf") or die "Cannot open ${infile}.rtf"; print RTF <$infile.html") or die "Cannot open ${infile}.html"; print FULL < $TITLE $BODYSTART

    $TITLE

    by $AUTHOR

    Table of Contents

      END $outputfile = $infile . '_toc.html'; open(F,">$outputfile") or die "Can not open output file $outputfile"; print F < $TITLE: Table of Contents $BODYSTART

      $TITLE

      by $AUTHOR

      Table of Contents

        END $plevel = 0; for $toc ( @TOCI ){ if( $toc =~ /\.1\.$/ ){ print F "
          \n"; print FULL "
            \n"; } $level = $toc; $level =~ s/\d//g; $level = length($level); if( $level < $plevel ){ print F "
          \n" x ($plevel - $level); print FULL "
        \n" x ($plevel - $level); } $plevel = $level; $tochtml = &htmlizee($TOC{$toc}); print F <$toc $tochtml
        END print FULL <$toc $tochtml
        END $tocrtf = &rtfizee($TOC{$toc}); print RTF "\{\\f0\\fs",(18,18,16,12,12)[$level]," ","\\tab "x$level,"$toc $tocrtf\}\\par\n"; } print FULL "
      \n" x $plevel; print RTF "\\page\n"; print F "
    \n" x $plevel; print F < END close F; # # Create title page for the compiled help file # $outputfile = $infile . '_title.thtml'; open(F,">$outputfile") or die "Can not open output file $outputfile"; print F < $TITLE $HBODYSTART

    $TITLE

    by $AUTHOR

    $BODYEND END close F; # # Create contents file for the compiled help file # $outputfile = $infile . '.hhc'; open(F,">$outputfile") or die "Can not open output file $outputfile"; print F <
      END $plevel = 0; for $toc ( @TOCI ){ if( $toc =~ /\.1\.$/ ){ print F "
        \n"; } $level = $toc; $level =~ s/\d//g; $level = length($level); if( $level < $plevel ){ print F "
      \n" x ($plevel - $level); } $plevel = $level; $tochtml = &htmlizee($TOC{$toc}); print F < END } print F < $BODYEND END close F; # # Create project file for the compiled chtm file # $outputfile = $infile . '.hhp'; open(F,">$outputfile") or die "Can not open output file $outputfile"; print F <$outputfile") or die "Can not open output file $outputfile"; $tochtml = &htmlizee($TOC{$toc}); print F < $TITLE: $toc $tochtml $BODYSTART END # # Print the navigational links from this level upward # $ulc = 1; print FULL '

      [Contents]
      ' ,"\n"; print F '

        [Contents]
        ' ,"\n"; if( $toc =~ /^(\d+\.)/ ){ $section = $1; if( $toc ne $section ){ $ulc ++; $tochtml = &htmlizee($TOC{$section}); print F '
          ', $section,' ',$tochtml,"
          \n"; } } if( $toc =~ /^(\d+\.\d+\.)/ ){ $section = $1; if( $toc ne $section ){ $ulc ++; $tochtml = &htmlizee($TOC{$section}); print F '
            ', $section,' ',$tochtml,"
            \n"; } } if( $toc =~ /^(\d+\.\d+\.\d+\.)/ ){ $section = $1; if( $toc ne $section ){ $ulc ++; $tochtml = &htmlizee($TOC{$section}); print F '
              ', $section,' ',$tochtml,"
              \n"; } } if( $toc =~ /^(\d+\.\d+\.\d+\.\d+\.)/ ){ $section = $1; if( $toc ne $section ){ $ulc ++; $tochtml = &htmlizee($TOC{$section}); print F '\n"; } print F "

              \n"; print FULL "

              \n"; print RTF "\\par\n"; $tochtml = &htmlizee($TOC{$toc}); print F "

              $toc $tochtml

              \n"; $dotcounter = $toc; $n = $dotcounter =~ s/\.//g; $tochtml = &htmlizee($TOC{$toc}); print FULL "$toc $tochtml\n"; $tocrtf = &rtfizee($TOC{$toc}); print RTF "\\par\{\\f0\\fs",(4-$n)*2+18," \\b $toc $tocrtf\}\\par\n"; if( $i > 0 ){ $STEPLINE = '[<<<] '; }else{ $STEPLINE = '[<<<] '; } if( $i < $#TOCI ){ $STEPLINE .= '[>>>]'; }else{ $STEPLINE .= '[>>>]'; } print F "$STEPLINE\n"; # # Print the navigational links from this level down # $ulc = 1; $plevel = $toc; $plevel =~ s/\d//g; $plevel = length($plevel); $toclen = $toc; $toclen =~ s/\d//g; $toclen=length($toclen); for( $j = $i+1; $j <= $#TOCI ; $j++ ){ $section = $TOCI[$j]; $seclen = $TOCI[$j]; $seclen =~ s/\d//g; $seclen = length($seclen); last if $toclen >= $seclen; $level = $section; $level =~ s/\d//g; $level = length($level); if( $level > $plevel ){ print F "
                \n" x ($level - $plevel); $ulc += $plevel - $level; $ulc = 0 if $ulc < 0; print "Warning: daingling section \"$section $TOC{$section}\"\n" if $level > $plevel+1; } if( $level < $plevel ){ print F "
              \n" x ($plevel - $level); $ulc -= $plevel - $level; $ulc = 0 if $ulc < 0; } $plevel = $level; $tochtml = &htmlizee($TOC{$section}); print F <$section $tochtml
              END } while( $ulc-- ){ print F "
            \n"; } print F "
            \n"; print F "

            \n"; # # print the body of that part # @lns = split /\n/ , $BODY{$toc}; @plns = (); while( $#lns >= 0 ){ $line = shift @lns; last if $line =~ /^\s*\@bye\s+/ ; last if $line =~ /^\s*\@bye$/ ; next if $line =~ /^\s*\@c\s+/; next if $line =~ /^\s*\@node\s+/; next if $line =~ /^\s*\@c$/; next if $line =~ /^\s*\@cindex\s+/; next if $line =~ /^\s*\@cindex$/; next if $line =~ /^\s*\@opindex\s+/; next if $line =~ /^\s*\@opindex$/; next if $line =~ /^\s*\@vindex\s+/; next if $line =~ /^\s*\@vindex$/; if( $line =~ /^\s*\@menu\s*$/ ){ while( $#lns >= 0 && $line !~ /^\s*\@end\s+menu\s*$/ ){ $line = shift @lns; } next; } push @plns , $line; } $line = &htmlizee( join( "\n", @plns)); print F $line; $line = &htmlizee( join( "\n", @plns),'#'); print FULL $line; $line = &rtfizee( join( "\n", @plns)); print RTF $line; print F < $STEPLINE $BODYEND END close F; # # Create the thtml file # $outputfile = $infile . '_' . $toc . 'thtml'; open(F,">$outputfile") or die "Can not open output file $outputfile"; $tochtml = &htmlizee($TOC{$toc}); print F < $TITLE: $tochtml $HBODYSTART END print F "

            $tochtml

            \n"; if( $i > 0 ){ $STEPLINE = '[<<<] '; }else{ $STEPLINE = '[<<<] '; } if( $i < $#TOCI ){ $STEPLINE .= '[>>>]'; }else{ $STEPLINE .= '[>>>]'; } print F "$STEPLINE\n"; print F "

            \n"; # # print the body of that part # @lns = split /\n/ , $BODY{$toc}; @plns = (); while( $#lns >= 0 ){ $line = shift @lns; last if $line =~ /^\s*\@bye\s+/ ; last if $line =~ /^\s*\@bye$/ ; next if $line =~ /^\s*\@c\s+/; next if $line =~ /^\s*\@node\s+/; next if $line =~ /^\s*\@c$/; next if $line =~ /^\s*\@cindex\s+/; next if $line =~ /^\s*\@cindex$/; next if $line =~ /^\s*\@opindex\s+/; next if $line =~ /^\s*\@opindex$/; next if $line =~ /^\s*\@vindex\s+/; next if $line =~ /^\s*\@vindex$/; if( $line =~ /^\s*\@menu\s*$/ ){ while( $#lns >= 0 && $line !~ /^\s*\@end\s+menu\s*$/ ){ $line = shift @lns; } next; } push @plns , $line; } $line = &htmlizee( join( "\n", @plns) , 'thtml'); print F $line; print F < $STEPLINE $BODYEND END close F; } close FULL; print RTF "\n\}\n"; close RTF;