#! /usr/bin/perl
use POSIX qw(strftime);
@CheckNames =();
undef @NameOrder; undef %Level; undef %Names;
push @PostGeneration, \&x_check_names;
sub x_check_names {
# Let's do a bit checking
foreach (@CheckNames) {
print STDERR "Warning: name $_ does not exist in document\n" unless exists $Names{$_};
}
}
$Funcs{"name"} = \&makeName;
sub makeName {
my ($name, $desc, $subst) = ($_[0] =~ /^[ ]*([^ ]+)[ ]?(.*)/);
if ($name =~ s/^(\053+)//) {
$Level{$name} = length ($1) ;
} else {
$Level{$name} = 0;
}
$desc = $name unless length $desc;
$desc =~ s/^\s*//;
$desc =~ s/\s*$//;
$Names{$name} = $desc;
push @NameOrder, $name;
return "$desc";
}
$Funcs{"h3-"} = \&html_h3_index;
sub html_h3_index {
my $h3_x;
my $section_name;
$h3_x = $Vars{"h3 index"} if exists $Vars{"h3 index"};
if (!defined $h3_x) {$Vars{"h3 index"} = $h3_x = "0"; }
++ $h3_x; $Vars{"h3 index"} = $h3_x;
$section_name = "$h3_x";
return "
" . makeName ("$section_name ". $_[0]) . "
";
}
$Funcs{"h4-"} = \&html_h4_index;
sub html_h4_index {
my ($h3_x, $h4_x);
my $section_name;
$h3_x = $Vars{"h3 index"} if exists $Vars{"h3 index"};
$h4_x = $Vars{"h4 index"} if exists $Vars{"h4 index"};
if (!defined $h4_x) {$Vars{"h4 index"} = $h4_x = "0"; }
++ $h4_x; $Vars{"h4 index"} = $h4_x;
if (defined $h3) {
$section_name = "$h3_x.$h4_x";
}else{
$section_name = "$h4_x";
}
return "" . makeName ("$section_name ". $_[0]) . "
";
}
$Funcs{"h3+"} = \&html_h3_index_;
sub html_h3_index_ {
my $h3_x;
my $section_name;
$h3_x = $Vars{"h3 index"} if exists $Vars{"h3 index"};
if (!defined $h3_x) {$Vars{"h3 index"} = $h3_x = "0"; }
++ $h3_x; $Vars{"h3 index"} = $h3_x;
$section_name = "$h3_x";
return "" . makeName ("$section_name $section_name ". $_[0]) . "
";
}
$Funcs{"h4+"} = \&html_h4_index_;
sub html_h4_index_ {
my ($h3_x, $h4_x);
my $section_name;
$h3_x = $Vars{"h3 index"} if exists $Vars{"h3 index"};
$h4_x = $Vars{"h4 index"} if exists $Vars{"h4 index"};
if (!defined $h4_x) {$Vars{"h4 index"} = $h4_x = "0"; }
++ $h4_x; $Vars{"h4 index"} = $h4_x;
if (defined $h3) {
$section_name = "$h3_x.$h4_x";
}else{
$section_name = "$h4_x";
}
return "" . makeName ("$section_name $section_name ". $_[0]) . "
";
}
%ColorScheme = (
"white" => "bgcolor=#ffffff fgcolor=#000000",
"black" => "bgcolor=#000000 fgcolor=#ffffff",
"sand" => "bgcolor=#FFFFE0 fgcolor=#000000 link=#200080 vlink=#200040 alink=#8080FF",
# foreground/background
"black/white" => "bgcolor=#ffffff fgcolor=#000000",
"white/black" => "bgcolor=#000000 fgcolor=#ffffff",
);
$Funcs{"Color"} = \&setColorScheme;
$Funcs{"color"} = \&setColorScheme;
sub setColorScheme
{
my ($x) = @_;
if (exists $ColorScheme{$x}) {
for $entry (split(' ', $ColorScheme{$x})) {
$entry =~ m/([^=]+)=(.*)/;
$Vars{$1} = $2;
}
}
elsif ($x eq "white") {
$Vars{"bgcolor"}="#ffffff";
$Vars{"fgcolor"}="#000000";
} else {
die "Unknown color scheme";
}
return "";
}
# Table of contents, based on names
$Funcs{"content_table"} = \&makeContents;
$Funcs{"content-table"} = \&makeContents;
$Funcs{"content-table-#"} = \&makeContents;
sub makeContents
{
my $all = $_[0];
my ($x, $level);
$x = "{ul:\n";
$level = 0;
foreach (@NameOrder) {
# print "Level{$_} = $Level{$_}\n";
}
foreach (keys %Level) {
# print STDERR "Level{$_} = $Level{$_}\n";
}
# foreach (sort { $Position{$a} <=> $Position{$b} } keys %Names) {
foreach (@NameOrder) {
# print STDERR "Level: $Level{$_} from $level\n";
if ($Level{$_} > $level) {
$x .= (" " x ($Level{$_})) . "{ul:\n" x ($Level{$_} - $level);
} elsif ($level > $Level{$_}) {
$x .= (" " x $level) . "}\n" x ($level - $Level{$_});
}
$level = $Level{$_};
$x .= " " x (1+$level) . "{li: {href:(nounder) #$_ $Names{$_}}}\n";
}
$x .= " " x $level . "}\n" while $level-- >= 0;
# print STDERR "$x\n";
return "\n" . $x
. "\n" . $all;
}
$Funcs{"autotable"} = \&makeAutoTable;
sub makeAutoTable
{
my (@Cells,$width,$s,$col,$x);
if (exists $Args{delimiter}) {
$Args{delimiter} =~ s/\\(\d{3})/chr(oct($1))/ge;
@Cells = split($Args{delimiter},$_[0]);
delete $Args{delimiter};
} else {
@Cells = split(",",$_[0]);
}
$width = $Args{width}; delete $Args{width};
die "width not defined in autotable?" unless defined($width);
$col = 0;
$x = "\n";
foreach $s (@Cells) {
if ($col == 0) {
$x .= "";
}
$x .= "$s | \n";
if (++$col == $width) {
$x .= "
";
$col = 0;
}
}
if ($col == 0) {
$x .= "\n";
}
$x .="
";
return $x;
}
# -------------------------------------------------------------------
# commentors: tags may contain #, it is a good way to mark comment
# generating / scripting thingies with #
$Funcs{"-#"} = \&html_blind_comment;
sub html_blind_comment
{
return "";
}
$Funcs{"-#-"} = \&html_comment_END;
sub html_comment_END
{
return "";
}
$Funcs{"+#+"} = \&html_comment_START;
sub html_comment_START
{
return "";
}
$Funcs{"*--"} = \&html_comment;
sub html_comment
{
return "";
}
# -------------------------------------------------------------------
$Funcs{"Subject"} = \&html_title;
$Funcs{"title"} = \&html_title;
sub html_title
{
($Title) = @_;
$Vars{"Subject"} = $Title ;
return "{h1: $Title}" if $parse_body;
return "";
}
# When was this page last changed, handily formatted, dd.mmm'yy
$Funcs{"file-ctime"} = \&file_ctime;
$Funcs{"last-change"} = \&file_ctime;
sub file_ctime {
my ($mtime,$s) = (stat($currentFile))[9];
$s = &ctime($mtime);
return strftime "%d.%b'%y", gmtime($mtime);
}
# When was this page last changed, handily formatted, mmm'yy
$Funcs{"file-date"} = \&html_file_date;
sub html_file_date {
my ($mtime,$s) = (stat($currentFile))[9];
$s = &ctime($mtime);
return strftime "%b'%y", gmtime($mtime);
}
# When was this page last changed, handily formatted
$Funcs{"put"} = \&html_put_var;
sub html_put_var {
my ($var, $def) = ($_[0] =~ /^\s+(\S+)\s+(.*)$/);
if (!defined $var) { $var = $_[0]; $def = ""; }
if (exists $Vars{$var})
{ return $Vars{$var}; }
return $def;
}
#fields are seperated by a vertical bar
$Funcs{"box-le-re"} = \&html_box_left_right;
$Funcs{"box-2"} = \&html_box_left_right;
sub html_box_left_right
{
my $cellwidth = "";
my ($leftText, $rightText) = ($_[0] =~ /^(.*)\|(.*)$/s);
if (!defined $rightText) {
$leftText = $_[0];
$rightText = " ";
}
$Args{"width"} = "100%" unless exists $Args{"width"};
$Args{"border"} = "0" unless exists $Args{"border"};
$Args{"cellspacing"} = "0" unless exists $Args{"cellspacing"};
$Args{"cellpadding"} = "0" unless exists $Args{"cellpadding"};
$cellwidth = " width=".(delete $Args{"cellwidth"})
if (exists $Args{"cellwidth"});
return "\n\n" . $leftText
. "\n | \n" . $rightText
. "\n |
";
}
#fields are seperated by vertical bars
$Funcs{"box-le-ce-re"} = \&html_box_left_center_right;
$Funcs{"box-3"} = \&html_box_left_center_right;
sub html_box_left_center_right
{
my $cellwidth = "";
my ($leftText, $centerText, $rightText) = ($_[0] =~ m/^(.*)\|(.*)\|(.*)$/s );
if (!defined $rightText) {
($leftText, $centerText) = ($_[0] =~ m/^(.*)\|(.*)$/s );
$rightText = " ";
}
if (!defined $leftText) {
$leftText = " ";
$centerText = $_[0];
$rightText = " ";
}
$Args{"width"} = "100%" unless exists $Args{"width"};
$Args{"border"} = "0" unless exists $Args{"border"};
$Args{"cellspacing"} = "0" unless exists $Args{"cellspacing"};
$Args{"cellpadding"} = "0" unless exists $Args{"cellpadding"};
$cellwidth = " width=".(delete $Args{"cellwidth"})
if (exists $Args{"cellwidth"});
return "\n\n" . $leftText
. "\n | \n" . $centerText
. "\n | \n" . $rightText
. "\n |
";
}
$Funcs {"box-30"} = \&html_box_3_equal;
sub html_box_3_equal
{
$Args{"cellwidth"} = "30%" unless exists $Args{"cellwidth"};
html_box_left_center_right (@_);
}
$Funcs {"box-50"} = \&html_box_2_equal;
sub html_box_2_equal
{
$Args{"cellwidth"} = "50%" unless exists $Args{"cellwidth"};
html_box_left_right (@_);
}
# a nice visual: a vertcal tabb, the upper-portion is just color filled,
# while the lower-portion contains some text in a box
$Funcs{"vtabb"} = \&html_vtabb;
sub html_vtabb
{
my ($text) = $_[0];
if (!exists $Args{"border"}) { $Args{"border"} = "0"; }
if (!exists $Args{"cellspacing"}) { $Args{"cellspacing"} = "0"; }
if (!exists $Args{"cellpadding"}) { $Args{"cellpadding"} = "0"; }
if (!exists $Args{"bordercolor"}) { $Args{"bordercolor"} = $Vars{"fgcolor2"}; }
my $rep = "";
$rep .= $text ;
$rep .= "
";
}
# a nice visual: a horizontal tabb, the left-portion is just color filled
# while the right-portion contains some text in a box
$Funcs{"htabb"} = \&html_htabb;
sub html_htabb
{
my ($text) = $_[0];
if (!exists $Args{"border"}) { $Args{"border"} = "0"; }
if (!exists $Args{"cellspacing"}) { $Args{"cellspacing"} = "0"; }
if (!exists $Args{"cellpadding"}) { $Args{"cellpadding"} = "0"; }
if (!exists $Args{"bordercolor"}) { $Args{"bordercolor"} = $Vars{"fgcolor2"}; }
my $rep = "";
$rep .= $text ;
$rep .= "
";
}
# some function from the elder smart-html-package system
# just does things a little different than html_href
$Funcs{"url"} = \&html_url;
sub make_url {
my ($url, $desc) = ($_[0] =~ /^([^,]+),?(.*)/);
$desc = $url unless length $desc;
$desc =~ s/^\s*//;
$desc =~ s/\s*$//;
$desc =~ s/^#//;
if ($url =~ m#//# or $url =~ m#^/#) { # Already URI?
} elsif ($url =~ /@/) {
$url = "mailto:$url";
} elsif ($url =~ m#\.[a-zA-Z]{2,3}/# or $url =~ m#/$# or $url =~ /^www/) {
if (($url =~ /^www/) or ($url !~ /\.(gz|zip|bz2|v\d)$/)) {
$url = "http://$url";
}
}
# Remember to check those URls later
if ($url =~ /^#(.*)/) {
push @CheckNames, $1;
}
return "$desc";
}
# make a href-link, the first part is the link-path
# the latter is the definition inside of the href
$Funcs{"href"} = \&html_href;
sub html_href {
my ($url, $desc) = ($_[0] =~ /([^ ]+) ?(.*)/);
$desc = $url unless length $desc;
$desc =~ s/^\s*//;
$desc =~ s/\s*$//;
$desc =~ s/^#//;
if ($url =~ m#//# or $url =~ m#^/#) { # Already URI?
} elsif ($url =~ /@/) {
$url = "mailto:$url";
} elsif ($url =~ m#\.[a-zA-Z]{2,3}/# or
$url =~ m#/$# or $url =~ /^www/) {
if (($url =~ /^www/) or ($url !~ /\.(gz|zip|bz2|v\d)$/)) {
$url = "http://$url";
}
}
# Remember to check those URls later
if ($url =~ /^#(.*)/) {
push @CheckNames, $1;
}
return "$desc";
}
$Funcs{"e-mail"} = \&html_e_mail;
sub html_e_mail {
my ($url, $desc) = ($_[0] =~ /([^ ]+) ?(.*)/);
$desc = "" unless length $desc;
$_ = $url;
/\?/ && s:^([^\?]*)\?.*:$1:s; # chop off anything past ?
/\@/ && s:\@:\@:s; # make the 'at' smaller
return "e-mail:$_ $desc";
}
# create an img-tag - the first portion is the image-filename
# the rest held to be the alt-tag, if nothing besides the filename
# could be found, try to figure out a good alt-tag from the filename
$Funcs{"img"} = \&html_img;
sub html_img {
my ($url,$alt) = ($_[0] =~ m/[ ]*([^\ ]*)[ ]?(.*)/ );
if (!exists $Args{"alt"}) {
getalt: {
if (defined($alt)) { last getalt; }
# now try to guess a good $alt-tag from the $url
$alt = $url;
$alt =~ s{^([^\?]*)[\?]?(.*)$}{$1}s ; # strip dynamic parts (if any)
$alt =~ s{^(.*)\.\w+$}{$1}sg ; # strip file extensions
$alt =~ s{file:(.*)}{$1}s;
# maybe there's a file with the alt-text in there ...
if ($alt !~ m/^\w+:/ and -f "$alt.alt") {
$altalt = `cat $alt.alt`;
if (2 < length $altalt) {
$alt = $altalt;
last getalt;
}
}
if ($alt =~ m:/.+/:) { # button/new.gif -> "button new"
$alt =~ s{^(.*)/(.+)/(.*)$}{$2 $3}s ;
} elsif ($alt =~ m:./:) {
$alt =~ s{^(.+)/(.*)$}{$2 $3}s ;
} else {
$alt =~ s{^(.+)\b(\w+)$}{$2}s ;
}
}
$alt =~ s/[ ]*$//sg ;
$Args{"alt"} = $alt;
}
return "
";
}
# turn all spaces into ie. hardspace
# if text is empty, it will return exactly one
$Funcs{"nbsp"} = \&html_nbsp;
sub html_nbsp {
my ($text) = @_;
if (length $text) {
$text =~ s/[ ]/\ \;/g ;
return $text;
} else {
return " ";
}
}
# -----------------------------------------------------------------
$Funcs{"some-code"} = \&html_some_code;
sub html_some_code {
$_ = $_[0];
s///sg ; # cut out soft-breaks, since we want to apply
then
$Args{"bgcolor"} = $Vars{"bgcolor2"} if (exists $Vars{"bgcolor2"});
$Args{"bgcolor"} = $Args{"bgcolor2"} if (exists $Args{"bgcolor2"});
$Args{"border"} = 1 if !exists $Args{"border"};
$Args{"align"} = "center" if !exists $Args{"align"};
$Args{"width"} = "80%" if !exists $Args{"width"};
# transfer lines that look like real extra-comments
s:(^)(\#\#.*)$:$1$2:mg ;
return "
";
}
$Funcs{"c-code"} = \&html_c_code;
sub html_c_code {
$_ = $_[0];
s///sg ; # cut out soft-breaks, since we want to apply
then
# mark comments (at end of line, per line)
s:(\/\*.*\*\/)(\s*)$:$1$2:mg ;
# mark double-quotes
s:(\"\;):$1:g ;
# mark precompiler lines
s:(^)(\#.*)$:$1$2:mg ;
$Args{"bgcolor"} = $Vars{"bgcolor2"} if (exists $Vars{"bgcolor2"});
$Args{"bgcolor"} = $Args{"bgcolor2"} if (exists $Args{"bgcolor2"});
$Args{"border"} = 1 if !exists $Args{"border"};
$Args{"align"} = "center" if !exists $Args{"align"};
$Args{"width"} = "80%" if !exists $Args{"width"};
return "
";
}
$Funcs{"td-code"} = \&html_td_code;
sub html_td_code {
$_ = $_[0];
s///sg ; # cut out soft-breaks, since we want to apply
then
$Args{"bgcolor"} = $Vars{"bgcolor2"} if (exists $Vars{"bgcolor2"});
$Args{"bgcolor"} = $Args{"bgcolor2"} if (exists $Args{"bgcolor2"});
return "$_ | ";
}
$Funcs{"td-c-code"} = \&html_td_c_code;
sub html_td_c_code {
$_ = $_[0];
s///sg ; # cut out soft-breaks, since we want to apply
then
# mark comments (at end of line, per line)
s:(\/\*.*\*\/)(\s*)$:$1$2:mg ;
# mark double-quotes
s:(\"\;):$1:g ;
# mark precompiler lines
s:(^)(\# *\w*):$1$2:mg ;
$Args{"bgcolor"} = $Vars{"bgcolor2"} if (exists $Vars{"bgcolor2"});
$Args{"bgcolor"} = $Args{"bgcolor2"} if (exists $Args{"bgcolor2"});
return "$_ ";
}
# -----------------------------------------------------------------
@table_td = ("");
@table_td_args = ("");
$Funcs{"tr-make"} = \&html_tr_make;
$Funcs{"tr_make"} = \&html_tr_make;
sub html_tr_make {
($columns, $rep) = int ($_[0]);
($rep) = "\n | " ;
for $i (1 .. $columns) {
$rep .= " |