]> git.pond.sub.org Git - empserver/blobdiff - info/emp2html.pl
Update copyright notice
[empserver] / info / emp2html.pl
index 249c4180fd2f25d01256a77394acdd1cfad10cf2..abad5dea2933d1c4013a91321da80d6fe5559c35 100644 (file)
-#!/usr/local/bin/perl
+#!/usr/bin/perl
+#
+#   Empire - A multi-player, client/server Internet based war game.
+#   Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
+#                 Ken Stevens, Steve McClure, Markus Armbruster
+#
+#   Empire 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 3 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.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+#   ---
+#
+#   See files README, COPYING and CREDITS in the root of the source
+#   tree for related information and legal notices.  It is expected
+#   that future projects/authors will amend these files as needed.
+#
+#   ---
+#
+#   emp2html.pl: Convert info source to HTML
+#
+#   Known contributors to this file:
+#      Drake Diedrich, 1996
+#      Markus Armbruster, 2004-2013
+#
+# Usage: emp2html.pl INFO...
+#
+# Convert info source on standard input to HTML on standard output.
+# INFO... are the info page names.
+
+use strict;
+use warnings;
+
+my $in_nf = 0;
+my $esc = "\\";
+my $ignore = 0;
+my $is_subj;
+my @a;
+my %topic;
+
+for (@ARGV) {
+    $topic{$_} = 1;
+}
 
+print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n";
+print "   \"http://www.w3.org/TR/html4/strict.dtd\">\n";
+print "<html>\n";
+print "<head>\n";
 
-$[ = 1;                        # set array base to 1
+line: while (<STDIN>) {
+    chomp;                     # strip record separator
+    s/((^|[^\\])(\\\\)*)\\\".*/$1/g; # strip comments
 
-line: while (<>) {
-    chop;      # strip record separator
-    @Fld = split(' ', $_, 9999);
+    @a = req($_);
 
-procline:
-    if (/^\.TH/) {
-       $str=$Fld[3];
-       for ($i=4;$i <= $#Fld; $i++) {
-           $str .= " " . $Fld[$i];
+    if (!@a) {
+       if ($is_subj && $in_nf) {
+           while ($_ =~ /[A-Za-z0-9\-\.]+/g) {
+               print htmlify("$`");
+               print anchor("$&");
+               $_="$'";
+           }
        }
-       $str = &htmlify($str);
-       printf("<title>%s : %s</title><h1>%s : %s</h1>\n",
-              $Fld[2],$str,$Fld[2], $str);
+       print htmlify($_), "\n" unless $ignore;
        next line;
     }
 
-    if (/^\.SY/) {
-#      $i = $_ =~ "\"" && ($RLENGTH = length($&), $RSTART = length($`)+1);
-#      $str = substr($_, $i + 1, length($_) - $i - 1);
-       $str = substr($_,5);
-       $str = &htmlify($str);
-       printf "<samp>[##:##] </samp><KBD>%s</KBD><p>\n", $str;
+    # requests
+
+    if ($a[1] eq "ig") { $ignore = 1; next line; }
+    if ($ignore) {
+       $ignore = 0 if $a[1] eq "..";
        next line;
     }
 
-    if (/^\.EX/) {
-       $str = substr($_, 5);
-       printf "<br><samp>[##:##] </samp><kbd>%s</kbd><p>\n", &htmlify($str);
+    if ($a[1] eq "TH") {
+       @a = checkarg(2, @a);
+       $is_subj = $a[2] eq 'Subject' || $a[2] eq 'Info';
+       $a[3] = htmlify($a[3]);
+       print "<title>$a[2] : $a[3]</title>\n";
+       print "</head>\n";
+       print "<body>\n";
+       print "<h1>$a[2] : $a[3]</h1>\n";
+       print "<p>\n";
        next line;
     }
 
-    if (/^\.L/) {
-       $str = substr($_, 4);
-       printf "<h2>%s</h2>\n", &htmlify($str);
+    if ($a[1] eq "SY") {
+       @a = checkarg(1, @a);
+       $a[2] = htmlify($a[2]);
+       print "<samp>[##:##] Command : </samp><KBD>$a[2]</KBD><p>\n";
        next line;
     }
 
-    if (/^\.(NF|nf)/) {        printf (("<p><pre>\n")); next line;    }
-    if (/^\.(FI|fi)/) {        printf (("</pre><p>\n")); next line;   }
-    if (/^\.s3/) { printf (("<p>\n")); next line; }
-    if (/^\.s1/) {printf (("<hr> \n")); next line; }
-    if (/^\.br/) { printf "<br>\n"; next line; }
-    if (/^\.SA/) {
-       @a = split('[: ",.]+');
-
-       printf("See also : %s\n",&anchor($a[3]) );
-       for ($i = 4; $i <= $#a ; ($i)++) {       
-           printf(", %s\n",&anchor($a[$i]));
-       }
+    if ($a[1] eq "EX") {
+       my $str = htmlify(join(' ',@a[2..$#a]));
+       print "<br><samp>[##:##] Command : </samp><kbd>$str</kbd><p>\n";
+       next line;
+    }
 
-       while (<>) {
-           chop;       # strip record separator
-           @a = split('[: ,.]+');
-           @Fld = split(' ', $_, 9999);
-           if (/^\./) { goto procline; }
-           for ($i = 1; $i <= $#a ; ($i)++) {       
-               printf(", %s\n",&anchor($a[$i]));
-           }
-       }
-           
-       exit(0);
+    if ($a[1] eq "L" && $is_subj) {
+       $a[2] =~ / /;
+       print "<br>" . anchor("$`") . " $'\n";
+       next line;
     }
-           
-    if (/^\./) {       next line; }
 
-    if (/^(See also|See Also|see also)/) {
-       @a = split('[: ,.]+');
+    if ($a[1] =~ /^LV?$/) {
+       @a = checkarg(1, @a);
+       $a[2] = htmlify($a[2]);
+       print "<h2>$a[2]</h2>\n";
+       print "<p>\n";
+       next line;
+    }
 
-       printf("See also : %s\n",&anchor($a[3]) );
-       for ($i = 4; $i <= $#a ; ($i)++) {       
-           printf(", %s\n",&anchor($a[$i]));
-       }
+    if ($a[1] eq "eo") { $esc = 0; next line; }
+    if ($a[1] eq "ec") { $esc = $#a == 1 ? "\\" : $a[2]; next line; }
 
-       while (<>) {
-           chop;       # strip record separator
-           @a = split('[: ,.]+');
-           @Fld = split(' ', $_, 9999);
-           if (/^\./) { goto procline; }
-           for ($i = 1; $i <= $#a ; ($i)++) {       
-               printf(", %s\n",&anchor($a[$i]));
-           }
+    if ($a[1] =~ /NF|nf/i) { $in_nf = 1; printf (("<p><pre>\n")); next line; }
+    if ($a[1] =~ /FI|fi/i) { $in_nf = 0; printf (("</pre><p>\n")); next line; }
+    if ($a[1] eq "s1") { printf (("<hr><p>\n")); next line; }
+    if ($a[1] eq "br") { printf "<br>\n"; next line; }
+
+    if ($a[1] eq "SA") {
+       @a = checkarg(1, @a);
+       @a = split(/[\: \"\,\.]+/, $a[2]);
+       for my $a (@a) {
+           $a = anchor($a);
        }
-           
-       exit(0);
+       print "<p>See also : ", join("\n, ", @a), "\n";
     }
-           
-    printf "%s\n", &htmlify($_);
+
+    # ignore unknown request
 }
 
-#sub anchor {
-#    local($_) = @_;
-#    $file = $_ . ".t";
-#    if (-r $file) {
-#      return ("<a href=" . $_ . ".html" . ">" . $_ . "</a>");
-#    } else {
-#      return ( " " . $_ . " ");
-#    }
-#}
+print "</body>\n";
+print "</html>\n";
 
-sub anchor {
-    local($_) = @_;
-    local(@file,$file);
-    $file = $_ . ".t";
-#    if (-r $file) {
-    if (1) {
-      $file =~ s/.t$/.html/;
-      return ("<a href=\"$file\">$_</a>");
-    } else {
-       @file = <$_*t>;
-       if (@file) {
-         warn "Expanding $_ to $file[$[]\n";
-         $file[$[] =~ s/.t$/.html/;
-         return ("<a href=\"$file[$[]\">$_</a>");
-       } else {
-         warn "Unable to link $_\n";
-         return ( "<em>$_</em>");
+sub req {
+    local ($_) = @_;
+    if (/^([\.\'])[ \t]*([^ ]*) *(.*)/) {
+       my @a = ($1, $2);
+       $_ = $3;
+       while (/\G(\"((\\.|[^\\\"])*)(\"|\Z))|\G(([^ ]|\\.)+) */g) {
+           push(@a, $2 || $5);
        }
+       return @a;
+    }
+    return ();
+}
+
+sub checkarg {
+    my ($n, @a) = @_;
+    warn "extra arguments for $a[1] ignored" if $#a > $n+1;
+    warn "missing arguments for $a[1] supplied" if $#a < $n+1;
+    while ($#a < $n+1) {
+       push @a, "";
     }
+    return @a;
+}
+
+sub anchor {
+    local ($_) = @_;
+    return $topic{$_} ? "<a href=\"$_.html\">$_</a>" : $_;
 }
 
-    
 # Translate HTML special characters into escape sequences
 sub htmlify {
-        local($_) = @_;
-       s/^\"(.*)\"$/$1/;
-       s/\\&//g;               # a nothing character
-        s/\&/&amp;/g;
-        s/\</&lt;/g;
-        s/\>/&gt;/g;
-       while (@a = /(\\\*Q)([A-Za-z0-9\-\.]+)(\\\*U)/) {
-           /(\\\*Q)([A-Za-z\-]+)(\\\*U)/;
-           $_ = $` . &anchor($a[2]) . $';
-       }
-       while (@a = /(\\\*Q)(\"info )([A-Za-z0-9\-\.]+)(\\\*U)/) {
-           /(\\\*Q)(\"info )([\w\-\.]+)(\\\*U)/;
-           $_ = $` . "\"info " . &anchor($a[3]) . $';
-       }
-       while (@a = /(\"info )([A-Za-z0-9\-\.]+)/) {
-           /(\"info )([\w\-\.]+)/;
-           $_ = $` . "\"info " . &anchor($a[2]) . $';
+    local ($_) = @_;
+    die "funny escape character `$esc' not supported"
+       if $esc && $esc ne "\\";
+    # translate some troff escapes
+    s/\\&//g if $esc;          # zero width space character
+    # escape HTML special characters
+    s/\&/&amp;/g;
+    s/\</&lt;/g;
+    s/\>/&gt;/g;
+    return $_ unless $esc;
+    # translate more troff escapes
+    s/\\e/&\#92;/g;            # escape character
+    # turn quoted strings that look like info names into links
+    # tacky...
+    my $pfx = "";
+    while (/\\\*Q([A-Za-z0-9\-\.]+)\\\*U|\"info ([A-Za-z0-9\-\.]+)\"/) {
+       if (defined $1 && $topic{$1}) {
+           $pfx = $` . anchor($1);
+       } elsif (defined $2 && $topic{$2}) {
+           $pfx = "$`\"info " . anchor($2) . "\"";
+       } else {
+           $pfx .= $` . $&;
        }
-        s/\\\*Q/<em>/g;
-        s/\\\*U/<\/em>/g;
-        s/\\fI/<em>/g;
-        s/\\fR/<\/em><\/em>/g;
-        s/\\fB/<strong>/g;
-        s/\\fP/<\/strong><\/em>/g;
-       s/\\\*\(bF/<strong>/g;  # bold font
-       s/\\\*\(pF/<\/strong><\/em>/g; # pica font
-       s/\\\*\(nF/<\/strong><\/em>/g; # normal font
-       s/\\\*\(iF/<em>/g;      # italic font
-       s/\\\(mu/x/g;           # multiply symbol
-       s/\\ / /g;              # should be non breaking space
-        return $_;
+       $_ = "$'";
+    }
+    $_ = "$pfx$_";
+    # tranlate more troff escapes and strings
+    s/\\\*Q/<em>/g;
+    s/\\\*U/<\/em>/g;
+    s/\\fI/<em>/g;
+    s/\\fR/<\/em><\/em>/g;
+    s/\\fB/<strong>/g;
+    s/\\fP/<\/strong><\/em>/g;
+    s/\\\*\(bF/<strong>/g;     # bold font
+    s/\\\*\(pF/<\/strong><\/em>/g; # pica font
+    s/\\\*\(nF/<\/strong><\/em>/g; # normal font
+    s/\\\*\(iF/<em>/g;         # italic font
+    s/\\\(mu/x/g;              # multiply symbol
+    s/\\ /&nbsp;/g;            # non breaking space
+    return $_;
 }
-
-#sub htmlify {
-#    local($str) = @_;
-#    $str;
-#}
-
-