#!/usr/local/bin/perl # Title: gentable.pl # Version: 5 # Synopsis: creates both TeX and HTML tables from ASCII representation # Author: Geoffrey S. Knauth # Created: 22-May-1996 # Modified: 23-May-1996 # # Copyright (C) 1996 Geoffrey S. Knauth # Distribution under the terms of the GNU General Public License (see bottom). use Getopt::Long; $result = GetOptions (html, tex); @fileargs = @ARGV; $usage = "usage: gentable.pl {-html | -tex} INPUTFILES...\n"; if ( $opt_html && $opt_tex || (!$opt_html && !$opt_tex)) { print STDERR $usage; exit (1); } $got_template = false; $is_title = true; while (<>) { chop; $line = $_; next if ($line =~ /^\s*%/); if ($line =~ /^-+$/) { print "\\tr\n" if ($opt_tex); next; } if ($opt_html) { next if ($line =~ /^:tex:/); if ($line =~ /^:html:(.*)/) { print "$1\n"; next; } } if ($opt_tex) { next if ($line =~ /^:html:/); if ($line =~ /^:tex:(.*)/) { print "$1\n"; next; } } if ($got_template ne true) { @template = split (/\|/, $line, -1); $got_template = true; @template = &remove_begend_whitespace_from_array_elts (@template); if ($opt_tex) { &tex_prologue; &tex_define_fields (@template); } if ($opt_html) { &html_prologue; } unshift @template,""; # only for symmetry with @data } else { # once we've got the template, we'll tend to come here @data = split (/\|/, $line, $#template+1); @data = &remove_begend_whitespace_from_array_elts (@data); unshift @data,""; # initial && for TeX only if ( $#data == 1 && $#data < $#template && !($data[1] =~ /\\!colspan\d+/)) { $width = $#template - $#data + 1; $data[1] .= "\\!colspan$width"; # convenience for user } for ($i = 0; $i <= $#data; $i++) { if ($data[$i] =~ s/\s*\\!colspan(\d+)\s*//) { $multispan = 2 * $1 - 1; # TeX $colspan = $1; # HTML } else { $multispan = $colspan = 1; } $nobar = ($data[$i] =~ s/\s*\\!nobar\s*//g); if ($opt_tex) { if ($multispan > 1) { print "\\multispan$multispan"; ($lfil, $alignment, $rfil, $selfformat) = &get_alignment ($template[$i], $data[$i]); print "$lfil" if (!$selfformat); } print "{$data[$i]}" if ($data[$i] ne ""); print "$rfil" if ($multispan > 1 && !selfformat); print "&"; # close this field if ($nobar) { print "\\omit"; # omits vertical line $nobar = 0; } if ($i == $#data) { print "\\cr\n"; # final vertical line } else { print "&"; # mid vertical lines } $multispan = 1; # reset } if ($opt_html) { next if ($i == 0); # for TeX only print "\n" if ($i == 1); print (($is_title eq true) ? " 1); ($lfil, $alignment, $rfil, $selfformat) = &get_alignment ($template[$i], $data[$i]); print "ALIGN=$alignment>"; if ($colspan > 1) { $data[$i] =~ s/^\\hfil\s+(.*)/\1/g; $data[$i] =~ s/(.*)\s*\\hfil$/\1/g; } print &remove_tex_from_html ("$data[$i]"); print (($is_title eq true) ? "\n" : "\n"); print "\n" if ($i == $#data); $is_title = false; $colspan = 1; # reset } } } } print "}}\n" if ($opt_tex); print "\n" if ($opt_html); exit 0; sub tex_prologue { print "% This TeX table was generated automatically by `gentable.pl'.\n"; print "% Here are the input file(s) we used:\n%\n% @fileargs\n\n"; print "\\vbox{\\tabskip=0pt\\offinterlineskip\n"; print "\\def\\tr{\\noalign{\\hrule}}\\halign{\n"; print "\\strut#&\\vrule#\\tabskip=1em plus 2em&\n"; } sub tex_define_fields { local ($i); for ($i = 0; $i <= $#_; $i++) { print "$_[$i]&\\vrule#"; print "&\n" if ($i < $#_); print "\\tabskip=0pt\\cr" if ($i == $#_); } print "\n"; } sub html_prologue { print "\n"; print "\n"; print "\n\n\n"; print "\n"; } sub remove_begend_whitespace_from_array_elts { local ($i); for ($i = 0; $i <= $#_; $i++) { $_[$i] = &remove_leading_trailing_whitespace ($_[$i]); } return @_; } sub remove_leading_trailing_whitespace { local ($result) = $_[0]; $result = $1 if ($result =~ /^\s+(\S.*)$/); $result = $1 if ($result =~ /^(.*\S)\s+$/); $result = "" if ($result =~ /^\s+$/); return $result; } sub get_alignment { local ($templatecell, $datacell) = @_; local ($arbiter, $selfformat); local (@alignment); if ($datacell =~ /\\hfil+/) { $arbiter = $datacell; $selfformat = true; } else { $arbiter = $templatecell; $selfformat = false; } if ($arbiter =~ /^\\hfil+.*\\hfil+$/) { @alignment= ("\\hfil", "CENTER", "\\hfil", $selfformat); } elsif ($arbiter =~ /^\\hfil+/) { @alignment = ("\\hfil", "RIGHT", "", $selfformat); } else { @alignment = ("", "LEFT", "\\hfil", $selfformat); } return @alignment; } sub remove_tex_from_html { # The source may have simple TeX directives. # Turn them into something that will look palatable in HTML. $_[0] =~ s/\\%/%/g; $_[0] =~ s/\\&/&/g; # no more & subs after this $_[0] =~ s//>/g; $_[0] =~ s/([^\\])(\$)/\1/g; $_[0] =~ s/(\\times)/x/g; $_[0] =~ s/(\\\$)/\$/g; # no more $ subs after this $_[0] =~ s/(--)/-/g; $_[0] =~ s/\\quad/  /g; $_[0] =~ s/\\qquad/    /g; $_[0] =~ s/(\\hfil+)//g; $_[0] =~ s/{\\tt\s*(.*)}/\1<\/TT>/g; $_[0] =~ s/{\\it\s*(.*)}/\1<\/I>/g; $_[0] =~ s/{\\bf\s*(.*)}/\1<\/B>/g; $_[0] =~ s/\\Leftarrow/<-/g; $_[0] =~ s/\\Rightarrow/->/g; return $_[0]; } __END__ LICENSE TO USE THIS SOFTWARE This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. Free Software Foundation 59 Temple Place - Suite 330 Boston, MA 02111-1307 Voice: +1-617-542-5942 Fax: +1-617-542-2652