home   sections   references   cd:s   about   links   heptagon 
 no margins   view as black text on white background 


(By the way, if you want to print this page out you may want to switch to wide margins and white text on black background).

Listing of parse.cgi

#!/usr/local/bin/perl5 -T

$index = ($0 =~ /index.cgi/i);
$heading_two = 4;
$heading_one = 2;
$keywords = "Jens Johansson home page";
$footnotes_used = 0;
$unix = ($ENV{'COMSPEC'} eq "" );
$user_agent = $ENV{'HTTP_USER_AGENT'};
$lynx = ($user_agent =~ /lynx/i);
$msie = ($user_agent =~ /msie/i);
if ($lynx) {
  $heading_two = 2;
  $heading_one = 1;
}

#$smiley = "";
#$smiley = "<moronic giggle>";
$smiley = "<g>";
$hspace = 15;
$spacerzzz = "<img src=\"/stuff/2x2.gif\" alt=\"\" hspace=$hspace>";
$title = "n/a";
$fontname = "arial,helvetica";

srand;                           # randomize

$* = 1;                    # multiline string matching

if ($index) {
   open (IN, "./index.par"); 
   $in_file = "index.par";
} else {
   if ($ENV{'PATH_INFO'} eq "") {
      open (IN, "-");
      $stdin_infile = 1;
   } else {

      $arguments = $ENV{'PATH_INFO'};

      if ($ENV{'CONTENT_LENGTH'} !~ /^\s*0\s*$/) {
         read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
         @postdata = split(/&/, $buffer);
         foreach $pair (@postdata) {
            ($post_name, $post_value) = split(/=/, $pair);
            $post_value =~ tr/+/ /;
            $post_value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
            if ($post_name =~ /COOKIE/i && $post_name !~ /SHOWCOOKIES/i) {
               $post_arguments .= "^" . $post_value;
            } else {
               $post_arguments .= "^" . $post_name . "_" . $post_value;
            }
        }
        $post_arguments =~ s/^\^//;
     }

      $arguments =~ s/(^[^\^]*)\^?//;
      $in_file = $1;
      open (IN, $in_file);

      @set_cookies = (split(/\^/, $arguments), split(/\^/, $post_arguments));
      foreach (@ARGV) {
         push (@set_cookies, split(/\^/) );
      }
      foreach (@set_cookies) {if (/^fc(.+)$/) {push(@fake_cookies, $1); undef $_; }}
   }
}

($in_dev, $in_ino, $in_mode, $in_nlink, $in_uid, $in_gid, $in_rdev,
$in_size, $in_atime, $in_mtime, $in_ctime, $in_blksize, $in_blocks) 
= stat(IN);

 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) 
= localtime($in_mtime);

$in_modtime = sprintf("%s %d, %d at %02d:%02d",
('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')[$mon],
$mday, $year+1900, $hour, $min);

$in_file =~ s/\#.*$//;
$in_file =~ s/\^.*$//;

while (<IN>) {

   if (/^\s*\#\# \/omit \#\#/i) {$convert_omit = 0; next;}
   if ($convert_omit) {next; }

   if (/^\s*\#\# \/graphical \#\#/i) {$omit_g = 0; next;}
   if ($omit_g && ($lynx) ) {next; }

   if (/^\s*\#\# \/nographical \#\#/i) {$omit_ng = 0; next;}
   if ($omit_ng && (!$lynx) ) {next; }

   if (/\s*^\#\# fakecookies? (.*) \#\#$/) {
      push(@fake_cookies, split(/\^/, $1));
      next;
   }
   if (/^\s*\#\# polyexpand (.*)\s*\#\#/i) {
      my $p = $1; my $pe = $p; my @lines;
      $pe =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($&))/eg;
      unless ($lynx) { #arrgghh
         while ($p =~ /(.{40,80}?\|)(.+)/ or $p =~ /(.{50,100}?) (.+)/) {
            push(@lines, $1); $p = $2;
         }
         push(@lines, $p);
         $_ = "<pre>";
         foreach my $line (@lines) {
            foreach ($line) {s/^\s+//; s/\s+$//; }
            $_ .= "\t<a href=\"/polymath.cgi?string=$pe\">$line</a>\n";
         }
         $_ .= "</pre>\n";
      } else {
         $_ = "<p><pre>\t<a href=\"/polymath.cgi?string=$pe\">$p</a>\n</pre>";
      }
   }
   if (/^\s*\#\# body (.*)\#\#/i) {$variable_body_tag_content = $1; next;}
   if (/^\s*\#\# title (.*)\#\#/i) {$title = $1; next;}
   if (/^\s*\#\# (\/)?polyexpand-collect \#\#/i) {
      if ($1 eq "/") {
         $polyexpand_collect = 0;
      } else {
         $polyexpand_collect = 1;
         $polyexpand_collect_string = "";
      }
      next;
   }
   if ($polyexpand_collect) {$polyexpand_collect_string .= $_; }
   if (/^\s*\#\# polyexpand-emit \#\#/i) {
      $polyexpand_collect_string =~ s/\#.+$//g;
      $polyexpand_collect_string =~ s/\s+/ /g;
      $_ = "
<form action = \"polymath.cgi\" method=post>
<input type=hidden name=string value=\"$polyexpand_collect_string\">
<input type=submit value=\"play that\"></form>
";

   }
   if (/^\s*\#\# literal (.*)\#\#/i) {$convert_literal = 1; $literal_opt = $1; next;}
   if (/^\s*\#\# \/literal \#\#/i) {$convert_literal = 0; next;}

   if (/^\s*\#\# graphical \#\#/i) {$omit_g = 1; next;}
   if (/^\s*\#\# nographical \#\#/i) {$omit_ng = 1; next;}
   if (/^\s*\#\# omit \#\#/i) {$convert_omit = 1; next;}
   if ($convert_literal) {
      s/([^A-Za-z0-9\t])/sprintf("&#%03d;", ord($&))/eg;
      if ($literal_opt =~ /tab/) {s/\t/$spacerzzz/g; }
      unless ($literal_opt =~ /nobr/) {s/$/<br>/;} 
   }

   if (/^\s*\#\#/) {next; }
   s/\#\#.*$//;
   $page .= $_;
}
close(IN);

foreach (split (/;/, $ENV{'HTTP_COOKIE'}) ) {
   /\s*(.*)=(.*)/;
   $cookie{$1} = $2;
}

if ($cookie{'fontname'}) {$fontname = $cookie{'fontname'}; }

#
# headings etc
#

$hed_tag = 0; $seq_tag = 0; 

$tmsgt = scalar(localtime);

if ($tmsgt =~ /2001/ && $tmsgt =~ /Sep 1[12]/) {
$tmsg = "<font size=+2><font color=red><br><br><center>
Phone and email service spotty. I am alive and OK, at least as of
21:33 local time (EDT, Sep 11). Lots of other people 
here were not so fortunate.
Let's hope nothing more happens.
I appreciate your concern.
<br><br></center>
</font></font>\n";
$page = $tmsg . $page;
}

if ($tmsgt =~ /Sep 11/) {
$tmsg = "<font color=red><br><br><center>
Let's all hope for a calm and peaceful September 11 this year
<br><br></center>
</font>\n";
$page = $tmsg . $page;
}

if ($page =~ /^\/\/\/\s*(.*)\s*$/) {$title = $1;} 

$page =~ s/^\/\/\/(.*)$/<h${heading_one}>$1<\/h${heading_one}>/g;

while ($page 
 =~ s/^\/\/\s*(.*)\s*$/<a name=\"HED_TAG_$hed_tag\"><\/a><h${heading_two}><u>$1<\/u><\/h${heading_two}>/
) {$headings[$hed_tag] = $1; $hed_tag++; $nheadings++;}

if (1) { 

#Pragma: no-cache
#Cache-Control: no-cache
#Expires: Thursday, 01-Jan-1970 00:00:00 GMT

   $html_head = 
"Content-type: text/html
Keywords: $keywords
$doc_specific_header";

   unless($html_head =~ /^Cache-control/) {
      $html_head .= "Cache-Control: no-cache\n";
   }

   foreach (@set_cookies) {
      if (/^$/) {next; }
      /(.+)_(.*)/;
      $c1 = $1; $c2 = $2;
      if ($c1 eq "HILITE") {next; }
#
# special cookie 'timestamp' when set will aqcuire local time 
#   (in seconds since midnight 1/1/1970)
#
      if ($_ eq "timestamp") {$c1= "timestamp"; $c2 = $timestamp = time(); }
      $html_head .= 
         "Set-Cookie: $c1=$c2; expires=Monday, 18-Jan-2038 22:14:07 GMT; path=/\n";
      if ($c1 eq 'showcookies') {
        if ($c2 eq 'yes') {$showcookies = 1; } else
          {$showcookies = -1; }

      }
   }
} else {
   $html_head = "";
}

if ( (($cookie{'showcookies'} eq 'yes') + $showcookies) > 0 ) {
   $toppage .= "<center><font size=-1>First, me saw cookie: ";
   foreach (keys %cookie) {
      $toppage .= "'$_' = '$cookie{$_}'; "; 
   }
   $toppage .= " Then, me set cookie: ";

   foreach (@set_cookies) {
      if (/^$/) {next; }
      /(.+)_(.*)/;
      $c1 = $1; $c2 = $2;
      $toppage .= "'$c1' = '$c2'; "; 
   }

   foreach (@fake_cookies) {
      /(.+)_(.*)/;
      $c1 = $1; $c2 = $2;
      $toppage .= "'$c1' = '$c2' (fake cookie); "; 
   }

   $toppage .= "<br>You can turn this annoying cookie text off ";
   if ($in_file) {
      $toppage .= "<a href=\"$in_file^showcookies_normal\">here</a>, ";
   } else {
      $toppage .= "by going <a href=\"about.par?showcookies_normal\">here</a> ('about' page), ";
   } 
   $toppage .= "if you want!<br></font></center>";
}

foreach (@set_cookies, @fake_cookies) {

   if (/^VARNISH/) {
      $varnish_cookie = 1;
      next;
   }

   if (/^$/) {next; }
   /(.+)_(.*)/; 

   $c1 = $1; $c2 = $2;
   if ($_ eq "timestamp") {$c1= "timestamp"; $c2 = $timestamp; }
   $cookie{$c1} = $c2;

#
# special argument "HILITE_char-string" will not set a cookie per se.
# instead, a word will be marked for subsequent highligting.
#

   if ($c1 eq "HILITE") {
      $hilite = $c2;
      $hilite =~ s/-/\\s/;
      next;
   }

}

if ($cookie{'sick'} eq 'yes') {$cookie{'back'} = 'white'; }

if ($cookie{'fontname'}) {$fontname = $cookie{'fontname'}; } # again!

$titlebullet = "·";  # ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿×Ø÷ø
$bodybullet = "•";
if ($lynx) {$titlebullet = "--"; $bodybullet = "*"; }

$prepage = "<html><head>
<LINK REL=\"SHORTCUT ICON\" HREF=\"http://www.panix.com/~jens/favicon.ico\">
<meta http-equiv=\"imagetoolbar\" content=\"no\" />
<title>&#9835;Jens Johansson " . $titlebullet . " " .
$title . "</title></head>
";

#
# formatting
#

if ($varnish_cookie) {
   $font_spec = "<font size=-1 face=\"$fontname\">";
} else {
   $font_spec = "<font face=\"$fontname\">";
}

$font_spec_end = "</font>";

$fn_glob_open = "<font color=#004000>";

if ($in_file) { $bkg_switch_file = $in_file; }
else {$bkg_switch_file = "about.par"; }

if (! $lynx) {
   if ($cookie{'marg'} eq 'wide') {
      $bkg_switch = "<a href=\"/parse.cgi/$bkg_switch_file?marg_normal\">[ margins ]</a>\n";
   } else {
      $bkg_switch = "<a href=\"/parse.cgi/$bkg_switch_file?marg_wide\">[ no margins ]</a>\n";
   } 
}

if ( 
   $variable_body_tag_content !~ /=#[0-9a-fA-F]{6,6}/ && $cookie{'sick'} ne 'yes'
   && ( ! $lynx )
) {
   if ($cookie{'back'} eq 'white') {
      $body_attr = " text=#000000 bgcolor=#FFFFFF";
      $bkg_switch .= "<a href=\"/parse.cgi/$bkg_switch_file?back_normal\">[ view as white text on black backgound ]</a>";
   } else {
      $body_attr = " bgcolor=#444444 text=#FFFFFF link=#AAAAFF alink=#FFAA00 vlink=#FFAAAA";
      $fn_glob_open = "<font color=#30FF30>";
      $bkg_switch .= "<a href=\"/parse.cgi/$bkg_switch_file?back_white\">[ view as black text on white background ]</a>";
   }
}

$bkg_switch = "<br><font size=-2>" . $bkg_switch . "</font>";

if ($cookie{'sick'} eq 'yes') {
   $body_attr = " text=#000000 background=\"/stuff/sick_bkb.gif\"";
   $font_spec = "<font size=+3>";
   $font_spec_end = "</font>";
}

$fn_glob_close = "</font>";

$body_attr_fn = $body_attr;

$body_attr .= " " . $variable_body_tag_content;

unless ($cookie{'marg'} eq 'wide') {
   if ($user_agent =~ /MSIE/i) {
      $body_attr1 .= " \nbottommargin=60 topmargin=60 leftmargin=60 rightmargin=60";
   } else {
      $body_attr1 .= " \nmarginwidth=60 marginheight=60";
   }
}

unless ($varnish_cookie) {
   $toppage_header = "<center><a href=\"/parse.cgi/index.par\">[ home ]</a>
   <a href=\"/parse.cgi/sect-index-brief.par\">[ sections ]</a>
   <a href=\"/parse.cgi/word-index.par\">[ references ]</a>
   <a href=\"/parse.cgi/records.par\">[ cd:s ]</a>
   <a href=\"/parse.cgi/about.par\">[ about ]</a>
   <a href=\"/parse.cgi/links.par\">[ links ]</a>
   <a href=\"http://www.heptagon.se\">[ heptagon ]</a>
   " . $bkg_switch . "
   </center>
   <br><br>";
} else {
   $toppage_header = "";
}

$toppage = "<body" . $body_attr . $body_attr1 . ">\n" . $font_spec . "
<!-- pages generated with utmost pride by a suite of custom perl 
scripts written by the 'great' Jens himself..  -->\n" .
$toppage_header . $toppage;

if ($cookie{'sick'} eq 'yes') {
   $toppage .= "<font size=+4>
<center>THE WHOLE SITE IS BEING DISPLAYED IN UGLY MODE!!<br>
<blink><A href=\"about.par?sick_no#sick\">CLICK HERE TO RETURN TO NORMAL MODE</a><br></blink>
<A href=\"about.par?sick_no#sick\">
<img width=150 height=150 align=middle src=\"/stuff/sick.gif\" alt=\"\"></a></font></center>";
}

unless ($varnish_cookie) {
   $postpage = "<p><!--endnotes--><hr><p><center> ";
   unless ($stdin_infile) {
      $postpage .= "Page updated " . $in_modtime . " " . $bodybullet . " ";
   }

   open (IN, "footer.par"); while (<IN>) {$footer .= $_; } close(IN);

   $postpage .= "Email: <a href=\"mailto:jens\@panix.com\">jens\@panix.com</a></center>" .
   $footer . $font_spec_end . "</body></html>";
} else {
   $postpage = "<p><!--endnotes-->" . $font_spec_end . "</body></html>";
}

#
# add subheading links (only for quite long pages, maybe??)
#

# length($page) > xxxxxxx

if ((! $lynx) 
    && (    ($cookie{'headings'} eq 'on')
         || ( ($cookie{'fakeheadings'} ne '') && $cookie{'headings'} ne 'off') )
        ) { 
   for ($h=0; $h < $nheadings; $h++) { 
      $subheadings .= "\n<a href=\"$in_file#HED_TAG_$h\">[ $headings[$h] ]</a>\n";
   }
   $fontsize=-1; if ($nheadings > 8) {$fontsize=-2; }
   $page = "<font size=$fontsize>" . $subheadings . "</font><p>" . $page;
}

$page = $toppage . $page . $postpage;

#
# do comment clobbering first! 
# in case some [] tags etc have been commented out
#

# '\#\#' comments
$page =~ s/^\#\#(.*)$//g;
$page =~ s/\#\#(.*)\#\#//g;

# '#' comments
$page =~ s/^\#(.*)$/<!-- $1 -->/g;

#
# now do repetitive mangling
#

#
#    <!--#include "filename"-->               like <!--#include -->
#

while (
   ($page =~ s/<!--#include "([^>]*)"-->/{PLACEHOLDER}/)
) { 
   $include = "";
   open (IN, $1); while (<IN>) {$include .= $_; } close(IN);
   $page =~ s/{PLACEHOLDER}/$include/;
}

#
#     do freaky dynamic-page stuff:
#
#     {a|b|c|d}          output string a,b,c or d with equal probability
#     {?0.1a|?0.3b|c|d}  p(a)=0.1, p(b)=0.3, p(c)=p(d)=(1-(0.1+0.3))/2
#     {@string a}        output a only if string is contained in remote_host 
#     {@string a|b}         same -- b if NOT
#     {=cookie value a}  output a only if cookie has value value
#         the value 'normal' implies; no cookie present means 'yes'
#     
#     {=cookie value dummyarg} output VALUE of cookie if exists.
#              ^^^^^ that is, use the *actual* string 'v a l u e' 
#
#     {=cookie value a|b}   same -- b if NOT
#
#     {=timestamp number a|b}
#     output a if cookie 'timestamp' is lower than 'number'
#     {=gettimestamp dummy_arg dummy_arg}
#               output value of cookie 'timestamp' as text (GMT)
#

#Sat Nov 06 09:26:33 1999
#while ($page =~ s/{([^}]*)}/{PLACEHOLDER}/) {
#   @curly_args = split(/\|/, $1);

while (
   ($status, @result) = &excise($page, "{", "}"), $status
) {
#   @curly_args = split(/\|/, $result[2]);
   @curly_args = split_outer($result[2], "|", "[{", "]}");

#Sat Nov 06 09:26:33 1999
   
   foreach (@curly_args) { 
      s/^\\\\\t/<br><img src="\/stuff\/2x2.gif" alt=\"\" hspace=$hspace>/g ;
      s/^\\\\/<br>/g ;
      s/^\\\t/<p><img src="\/stuff\/2x2.gif" alt=\"\" hspace=$hspace>/g ;
      s/^\\/<p>/g ;
      s/\s/ /g; 
   }
   if ($curly_args[0] =~ /^@(\S*)\s+(.*)/) { # remote_host replacement
      $repl = $2;
      if ($curly_args[1] ne "") {$alt  = $curly_args[1]; } else {$alt = ""; }
      if ($ENV{"REMOTE_HOST"} =~ /$1/) 
         {} else
         {$repl = $alt; }
   } elsif ($curly_args[0] =~ /^\=(\S+)\s+(\S+)\s+(.*)/) { # cookie stuff

      $c1 = $1; $c2 = $2; $c3 = $3;

      $repl = $c3;

      if (($c2 eq 'normal') && ($cookie{$c1} eq "")) {
         $cookie{$c1} = 'normal';
      }

      if ($curly_args[1] ne "") {$alt  = $curly_args[1]; } else {$alt = ""; }

      if (($c2 eq 'value') && ($cookie{$c1} ne "") && ($cookie{$c1} ne "normal")) {

         $repl = $cookie{$c1};

      } elsif ($c1 eq "gettimestamp") { # output the timestamp cookie!

          if ($cookie{'lastvisit'} eq 'on') 
            {$repl = gmtime($cookie{'timestamp'}) . " GMT"; } 
            else {$repl = ""; }

      } elsif ($c1 eq "timestamp") { # special case! timestamp dependent!!

         if($cookie{'lastvisit'} eq 'on' && $cookie{'timestamp'} < $c2)
            {} else
            {$repl = $alt; }

      } else {
         if ($cookie{$c1} eq $c2)
            {} else
            {$repl = $alt; }
      }
      

   } else

#     ok, probability stuff
   {
      undef %curly_prob; $t = 0; $n = 0; $m = 0; 
      foreach $arg (@curly_args) {
         $m++;
         if ($arg =~ /^\?([\d.]+)(.*)/) {
            $arg = $2; $curly_prob{$arg} = $1;
            $t += $1; 
         } else {
            $curly_prob{$arg} = 0; $n++; 
         }
      }      
      $limit = 0;
      if ($t > 1) {                      # wrongly specified probabilities!
         foreach $arg (@curly_args) {    # set all equal..
            $limit += 1 / $m;
            $curly_prob{$arg} = $limit;
         }
      } else {
         foreach $arg (@curly_args) {
            if ($curly_prob{$arg} == 0)
               { $limit += (1-$t) / $n; } else
               { $limit += $curly_prob{$arg}; }
            $curly_prob{$arg} = $limit;
         }
      }
      $random = rand;

      $repl = "";
      foreach $arg (@curly_args) {
         if ($random < $curly_prob{$arg} ) {
            $repl = $arg; last;
         }
      }
  }   

#Sat Nov 06 09:26:33 1999
#   $page =~ s/{PLACEHOLDER}/${repl}/;
   $page = $result[0] . $repl . $result[1];
#Sat Nov 06 09:26:33 1999

}

#
# handle [* ] footnotes
#

while ($page =~ s/\[\*([^]]*)\]/{PLACEHOLDER}/) {

   if ($cookie{'foot'} eq 'off') {
      $repl = "";
      goto done_footnote;
   }

   $footnote_text = $1;
   $footnote_text =~ s/\s*([\x00-\xFF]*)\s*/$1/;
   if ($footnote_text =~ /([^\|]+)\|(.*)/) {
      $footnote_link = $footnote_inline = $1; $footnote_text = $2;
      $fn_open = "";
      $fn_close = "";
   } else {
      $footnote_inline = "";
      if ($cookie{'foot'} eq 'bottom') {
         $footnote_link = ++$footnote_count;
      } else {
         $footnote_link = "*";
      }
      $fn_open = "<font size=-1><sup>";
      $fn_close = "</sup></font>";
   }

   $footnote_text =~ s/\s+/ /g;

# escape special chars into into $footnote_text_w

   $footnote_text_w = $footnote_text;
   $footnote_text_w =~ s/(.)/sprintf("%02X", ord($&))/eg;
   
   if ($cookie{'foot'} eq 'bottom') {
      $fn_open = "<font size=-1><sup>";
      $fn_close = "</sup></font>";
      $repl = $fn_open . "<a href=\"$in_file#FOOT_$footnote_count\"\n";
      $repl .= ">" . $fn_glob_open . $footnote_link . $fn_glob_close . "</a>" . $fn_close;
      $footnotes_at_end .= "<br><a name=\"FOOT_$footnote_count\"></a>$footnote_count: $footnote_text\n";
   } else {
      if ($cookie{'foot'} eq 'inline') {
         if (length($footnote_inline != 0))
             {$footnote_inline .= " "; }
         $repl = " <font size=-1>$footnote_inline($footnote_text) </font>";
      } else {
         $repl = $fn_open . "<a href=\"/fn0.cgi?${footnote_text_w}\"\n";
         unless ($cookie{'foot'} eq 'href') { # .. test onClick, maybe? return false?
            $repl .= "onMouseOver=\"footnote('${footnote_text_w}', event)\" 
onMouseOut=\"footnoteout()\"";
            $footnotes_used = 1;
         }

      $repl .= ">" . $fn_glob_open . $footnote_link . $fn_glob_close . "</a>" . $fn_close;
      }
   }
 
done_footnote:

   $page =~ s/{PLACEHOLDER}/${repl}/;
}

#
# handle [] special indexed link names
#

#while ($page =~ s/([^\\])\[([\S][^]]*)\]/$1{PLACEHOLDER}/) {  
while ($page =~ s/\[([\S][^]]*)\]/{PLACEHOLDER}/) {  

# [ xxxxxx ] not transformed, for text mode button etc

 $link_text = $1;
 if ($link_text =~ /^\\(.*)$/) { # [\blabla] => [\ is escape seq
   $repl = "$1"; # just lose the []'s
 } elsif ($link_text =~ /\?(.*)/) { # [?] search engine page quick reference
   $spec = $spec0 = $1;
   $spec0 =~ s/\s*(\w+)\s.*/$1/;
   $spec =~ s/\s*(.*)/$1/;
   $spec =~ s/([^\w\. ])/sprintf("%%%02X", ord($&))/eg;
   $spec =~ tr/ /+/;
   $repl = "<a href=\"/generate-web-search.cgi/$spec\" VARNISH>internet search links for '$spec0'</a>"; 
 } else { # normal [blabla] link

# webify it into link_text_w

   $link_text_w = $link_text;
   $link_text_w =~ s/([^\w\. ])/sprintf("%%%02X", ord($&))/eg;
   $link_text_w  =~ tr/ /+/;
   $repl = "<a href=\"/generate-word-index.cgi/${link_text_w}\" name=\"SEQ_TAG_$seq_tag\" VARNISH>$link_text</a>";
   $seq_tag++;
   }
 $page =~ s/{PLACEHOLDER}/${repl}/;
}

# fix escaped [xxxxxxx xxxx ] 

#$page =~ s/([\\])(\[[\S][^]]*\])/$2/g; 

#
# finally do rest of one-time manglings
#

if (length($footnotes_at_end) != 0) {
  $footnotes_at_end = "<font size=-3>" . $footnotes_at_end . "</font>";
  $page =~ s/<!--endnotes-->/$footnotes_at_end/;
}

# records.par jpegs or gifs.. 
if ($cookie{'recimg'} eq 'jpg' && $in_file =~ /records\.par/i) {
   $page =~ s#src="/stuff/cov/(.+)\.gif"#src="/stuff/cov/$1\.jpg"#ig;
}

# blink
if ($cookie{'blink'} eq 'off') {
   $page =~ s/<\s*\/?blink\s*>//ig;
}

# not sure how lynx handles &nbsp; .. checked, seems OK!
if (1 || $user_agent =~ /(mozilla|msie|opera|arachne)/i) {

   while($page =~ s/\[( .* )\]/{PLACEHOLDER}/) {
#   while($page =~ s/\[ (.*) \]/{PLACEHOLDER}/) {

      $repl = $1;
      $repl =~ s/ /\&nbsp\;/g;
      $page =~ s/{PLACEHOLDER}/${repl}/;
    }
} else {
}

# breaks, para marks, etc.
# \\tab => <br>+hspace, \tab => <p>+hspace
# \\ => <br>, \ => <p>

$page =~ s/^\\\\(\t+)/"<br>" . ($spacerzzz x length($1)) /eg ;
$page =~ s/^\\\\/<br>/g ;
$page =~ s/^\\(\t+)/"<p>" . ($spacerzzz x length($1)) /eg ;
$page =~ s/^\\/<p>/g ;

# word emphasis
   $page =~ s/\*([a-zA-Z][-0-9A-Za-z!?,.:;"'_ ]{1,50})\*/<i>$1<\/i>/g ;

# smileys

if ($cookie{'smiley'} eq 'on') {
   $page =~ s/(;\)|:\)|<g>)/${smiley}/g;
} else {
   $page =~ s/(;\)|:\)|<g>)//g;
}

# hey, let's change any stupid <B></B> tags to <i> !

$page =~ s/<([^b]?)b>/<$1i>/gi ;

# and as an antidote to that stupidity, let's specify <bb> to mean
# bold _really_ .. sigh

$page =~ s/<(.?)bb>/<$1b>/gi ;

# -- => typographical character

unless ($cookie{'dash'} eq 'off') {
   $page =~ s/([^!])--([^>])/$1&#151;$2/g;
}

#
# iso -> entityname translation
#
%entitymap = (
   'Å', '&Aring;', 'Ä', '&Auml;', 'Æ', '&AElig;', 'É', '&Eacute;',
   'Ñ', '&Ntilde;', 'Ö', '&Ouml;', 'Ø', '&Oslash;', 'Ü', '&Uuml;',
   'ß', '&szlig;', 'å', '&aring;', 'ä', '&auml;', 'æ', '&aelig;',
   'é', '&eacute;', 'ñ', '&ntilde;', 'ö', '&ouml;', 'ø', '&oslash;',
   'ü', '&uuml;', '®', '&reg;', '©', '&copy;', 
#  '™', '&trade;'
   '™', '(tm)'
);

if ($lynx) {$entitymap{'©'} = '(c)'; }

$entity_charclass = '[' . join('', keys %entitymap) . ']';
$page =~ s/($entity_charclass)/$entitymap{$1}/eg;

#
# hey, let's waste billions of processor cycles and megabits of 'net bandwidth! 
#
if ($cookie{'sick'} eq 'yes') {
   $page2 = ""; $in_literal = 0; $in_tag = 0;
   for ($i = 0; $i < length($page); $page2 .= $str, $i++) {
      $str = substr($page, $i, 1);   
      if ($in_literal && $str eq ";") {$in_literal = 0; next; }
      if ($in_literal) {next; }
      if ($str eq "&") { $in_literal = 1; next; }
      if ($in_tag && $str eq ">") {$in_tag = 0; next; }
      if ($in_tag) {next; }
      if ($str eq "<") { $in_tag = 1; next; }
      $str = sprintf("<font color=#%2x%2x%2x size=%d>%s<\/font>", rand(255), rand(128), rand(128), rand(4)+3, $str);
   }
   $page = $page2;
}

# this is even more useless... :)

if ($cookie{'atarifont'} eq 'yes' && ! $lynx) {
   if ($cookie{'back'} eq 'normal') {$atarifontdir = '/stuff/atari0/';} 
   else {$atarifontdir = '/stuff/atari1/';} 
   
   $page2 = ""; $in_literal = 0; $in_tag = 0;
   for ($i = 0; $i < length($page); $page2 .= $str, $i++) {
      $str = substr($page, $i, 1);   
      if ($in_literal && $str eq ";") {$in_literal = 0; next;
}
      if ($in_literal) {next; }
      if ($str eq "&") { $in_literal = 1; next; }
      if ($in_tag && $str eq ">") {$in_tag = 0; next; }
      if ($in_tag) {next; }
      if ($str eq "<") { $in_tag = 1; next; }
      $str =~ s/\s/ /;
      $str = sprintf(
         "<img width=8 height=16 src=\"%schar-%02x.gif\" alt=\"%s\" hspace=0 vspace=0>", 
         $atarifontdir, ord($str), $str);
   }
   $page = $page2;
}

# "hilite"
if ($hilite) {

#   $page =~ s/([\s>;()])($hilite)([\s'!.?,<&()])/$1<b><blink><font color=red>$2<\/font><\/blink><\/b>$3/ig;
   $page =~ s/([\s>;()])($hilite)([\s'!.?,<&()])/$1<b><font color=red>$2<\/font><\/b>$3/ig;

}

# short-hand links <http_blabla> => <a href="http_blabla">http_blabla</a>
# Sat Aug 21 14:00:51 1999 hacked to also allow <link_text|url>

while ($page =~ s/<(http:|ftp:|www\d*\.)([^>]+)>/{PLACEHOLDER}/) {
#while ($page =~ s/<(((http:|ftp:|www\d*\.)([^>]+))|(([^>]+)\.par($|\|)))>/{PLACEHOLDER}/) {
   $r_url = $1 . $2;
#   $r_url = $1;
   $r_url =~ s/^www/http:\/\/www/;
   if ($r_url =~ /(.+)\|(.*)/) {
      $replace = "<a href=\"$1\">$2<\/a>";
   } else {
      $replace = "<a href=\"$r_url\">$r_url<\/a>";
   }
   $page =~ s/{PLACEHOLDER}/$replace/;
}

# bare .par links transformed to specify parse.cgi
$page =~ s/href="([#\w.-]+\.par[^"]*)"/href="\/parse.cgi\/$1"/ig;

# here we go, window madness. needs to be tidied up..
# sdfsdf. . 3.4 .23.4 23.4 2 135 34 !!!!QHTUQWYRUWEYT!!!!
# <  VARNISH> -- no controls -- floating text window
# <  TARNISH> -- no controls, read image data for target win size if present
# 

if ($cookie{'img'} ne 'same' && (! $lynx )) {
   while ($page =~ s/<a\shref=([^>]*)>/{PLACEHOLDER}/i) {
      $repl = $1;

# of the form '"/stuff/img.gif"', '"mailto:jens@panix.com"' or '"http://w.a.q/~ww/" tarnish'

      unless ($repl =~ /mailto/) {

         if ($repl =~ /\"([^"]*)\.(gif|jpg|jpeg)\"/i) {
            $tarnish_fn = $1 . "." . $2;
         } else {
            $tarnish_fn = 0;
         }

         if ($repl =~ / ([TV])ARNISH$/i

# this is not quite done so disable it...
&& 0

) {
            $varnish = $1; if ($varnish =~ /v/i) {$varnish = 1; } else {$varnish = 2; }
         } else {
            $varnish = 0;
         }

         if ($tarnish_fn || $varnish) {
   
            $tarnish_h = 0;
            $tarnish_w = 0;
            $tarnish_ctl = 1;
         $repl =~ s/$/ target=\"newjj\" onClick=\"tarnish($tarnish_h, $tarnish_w, $tarnish_ctl)\" /i;
         $imgnew_used = 1;
         if ($tarnish_ctl != 0) {$tarnish_ctl_used = 1; }
         }
      }
      $page =~ s/{PLACEHOLDER}//;
   }

# oh my what a fucking kludge..

   $page =~ s/</</g; $page =~ s/>/>/g;

} # else replace TARNISH with '' but who cares

# moved heavy files..
$page =~ s#src="/stuff/([^"]+)"#src="http://jens.org/stuff/$1"#ig;

$page =~ s#href="/stuff/([^"]+\.(mp3|wav|mid|gif|jpg|txt|troff|ps|zip|exe|c))"#href="http://jens.org/stuff/$1"#ig;

# Lastly f we're on panix, transform '="/' into '="/~jens/'
if ($unix) {$page =~ s/="\//="\/~jens\//g;}

#
# add whatever JavaScript that is necessary
#

if ($cookie{'img'} ne 'same') {
   $javascript = "if (window.name != \"newjj\") {window.name = \"jj\";}

";
}

# stripped comment
# // open a window

if ($imgnew_used) { # JavaScript routine required
   $javascript .= "var wo = new Object();
function tarnish(width, height, controls) {
 var attributes, attributes1;
 attributes = 
    'resizable=yes,scrollbars=no,location=no,menubar=no,toolbar=no,status=no';" .
($tarnish_controls_used ? "
 if (controls) {attributes = 
   'resizable=yes,scrollbars=yes,location=yes,menubar=yes,toolbar=yes,status=yes';}" 
: "" ) . "
 if (width == 0) {width = screen.width*2/3; }
 if (height == 0) {height = screen.height*2/3; }
 attributes = attributes + ',width=' + width + ',height=' + height;
 wo = window.open('', 'newjj', attributes)
" .
# // wo.document.write('<html><head><title>newjj</title></head><body 
# ${body_attr_fn}>wait..</body></html>');
# // wo.document.close();
" wo.window.focus();
}

";
}

# 
# stripped comments..
# // Jens J's footnote handler v 1.0. (from http://www.panix.com/~jens)
# // Free use if you leave these lines intact! What a deal!
# // winobj.moveTo(event.screenX + 10, event.screenY + 10);
# // here we decode footnote encoded by perl cgi source like:
# // s/\\s/ /g; s/(.)/sprintf('%02X', ord(&#36;1))/eg;
# // (simply set text_dec to text instead, if it's plain text)
# // user could close it

if ($footnotes_used) { # JavaScript routine required
   unless ($cookie{'foot'} eq 'inline' && $cookie{'foot'} eq 'href') { 
      $javascript .= "defaultStatus = 'Move cursor over green link to see footnote.';
"
   }

   if ($msie) {
     $fn_winattr_calc = "fnx = event.screenX+10; fny = event.screenY+10;";
     $fn_winattr = "',left=' + fnx + ',top=' + fny";
   } else {
     $fn_winattr_calc = "fnx = event.screenX-window.pageXOffset+5; fny = event.screenY-window.pageYOffset+5; // NS BUG!!!?";
     $fn_winattr = "',screenX=' + fnx + ',screenY=' + fny";
   }

   $javascript .= "var winobj = new Object();
var fn_open;

function footnote (text, event) {
 var text_dec = ''; 
 var fnx, fny;
 if (fn_open) {return true; } 
 fn_open = 1;
 window.status = '***  PLEASE WAIT, GENERATING FOOTNOTE  ***';

 for(i = 0; i < text.length; i+=2) {
  text_dec += String.fromCharCode(parseInt(text.substr(i, 2), 16));
 }

 var dimension = 30 + 10 * Math.sqrt((text_dec.length + 50));
 var attributes = 'resizable=yes';
 hdimension = (dimension); wdimension = (dimension * 1.15);
 $fn_winattr_calc
 attributes = attributes + ',width=' + wdimension + ',height=' + hdimension;
 attributes = attributes + ${fn_winattr};

 winobj = window.open('', 'Fn', attributes);
 winobj.document.write(
 '<HTML><HEAD><TITLE>Fn</TITLE></HEAD><BODY${body_attr_fn}>\\n' +
 '$font_spec' +
 '<font size=-1><P><center>' + text_dec + '</P></font>\\n' +
 '<font size=-2>(move the mouse to remove footnote)</font></center>' +
 '$font_spec_end');
 winobj.document.close();
 window.status = '';
 return true;
}

function footnoteout () {
 fn_open = 0;
 if (!winobj.closed)
  winobj.close();
 return true;
} 

";
}

if ($javascript && (! $lynx )) {
   $prepage .= "
<script language='JavaScript'><!--

" . $javascript .
   "//--></script>

";
}

$page =~ s/(href|action)="(\/~jens)?\/parse.cgi\/([#\w.-]+\.par)\^([^"]*)"/href="$2\/$3?$4"/ig;
$page =~ s/(href|action)="(\/~jens)?\/parse.cgi\/([#\w.-]+\.par[^"]*)"/href="$2\/$3"/ig;

#$page =~ s#/parse.cgi/#/#g;

#
# Output the processed page
#

$page = $prepage . $page;

if (1) { 
   if ($unix) {
      $html_head .= "Content-length: " . length($page) . "\n\n";
   } else {
      # arrrgghh fucking braindead DOS newlines use 2 bytes once it's piped!
      $nlcount = length($page);
      for ($i=0; $i<length($page); $i++) {if (substr($page, $i, 1) eq "\n") {$nlcount++;}}
      $html_head .= "Content-length: " . $nlcount . "\n\n";
   }
   print $html_head;
}

print $page;

# better efficiency?? instead of regex-matching which also doesn't allow nesting..
#
#  &excise($string, "{opendelim", "closedelim}") -- 
#     returns status and $string split in 3, while tracking nested open/closedelims 
#
# eg.
#  &excise("aaaaaaaaaa{bla blurk plurrk {{-}} alb}zzz", "{", "}") returns
#    (1, "aaaaaaaaaa",
#    "zzz",
#    "bla blurk plurrk {{-}} alb")
#
#
# it seems to work!?.. !
#
# but how deal with | or other delimiters in matched string?
#

sub excise() {
   local($search_in, $opendelim, $closedelim) = @_;
   local($i, $rlen, $llen, $opendelim_count);
   $lmatch = index($search_in, $opendelim);
   if ($lmatch == -1) { return (0, '', '', ''); }
   $rlen = length($closedelim); $llen = length($opendelim);
   $opendelim_count = 1;
   for ($i=$lmatch+$llen; $i<=length($search_in)-$rlen && ($opendelim_count); ) {
      if (substr($search_in, $i, $llen) eq $opendelim) 
         {$opendelim_count++; $i+=$llen; next; }
      if (substr($search_in, $i, $rlen) eq $closedelim) 
         {$opendelim_count--; $rmatch = $i; $i+=$rlen; next; }
      $i++;
   }

   if ($opendelim_count) {
#      die "matching ".$opendelim."/".$closedelim." error";
      return (0, "matching ".$opendelim."/".$closedelim." error", '' ,''); 
   }

   ( 1, 
     substr($search_in, 0, $lmatch),
     substr($search_in, $rmatch + $rlen, length($search_in)-1),
     substr($search_in, $lmatch + $llen, $rmatch-$lmatch-$llen)
   )
}

# how to deal with | or other delimiters in matched string
#
#  &split_outer($string, "split_char", "{opendelim", "closedelim}") -- 
#     returns $string split at split_char, while tracking nested open/closedelims 
#
#  &split_outer("aaaaaaaaaa{bla blurk |plurrk [-lds|dd] {{-|-}} alb}zzz", "|", "{[", "}]") 
#  returns:
#    "aaaaaaaaaa{bla blurk" 
#    "plurrk [-lds|dd] {{-|-}} alb}zzz"
#

# @resulu = 
# &split_outer("abcdef|aaa{bla blurk |plurrk [-lds|dd] {{-|-}} alb}zzz", "|", "{[", "}]");
# print "(" . join(")<br>(", @resulu) . ")";

sub split_outer() {
   local($search_in, $split_char, $opendelim, $closedelim) = @_;
   local(@result, $len, $opendelim_count, $curchar, $j);
   local($i) = 0;
   $result[$i] = $search_in;

reloop:
   $len = length($result[$i]);

   for($j = 0; $j < $len; $j++) {
      $curchar = substr($result[$i], $j, 1);

      if ($curchar eq $split_char && !$opendelim_count) {
         $result[$i+1] = substr($result[$i], $j+1);
         $result[$i] = substr($result[$i], 0, $j);
         $i++;
         goto reloop;
      }
      if (index($opendelim, $curchar) != -1) {$opendelim_count++; }
      if (index($closedelim, $curchar) != -1) {$opendelim_count--;}
   }

#print "<br>(". join(")<br>(", @result) . ")";
   @result;
}

__END__


Email: jens@panix.com

All content copyright © Jens Johansson 2017. No unathorized duplication, copying, mirroring, archival, or redistribution/retransmission allowed! Any offensively categorical statements passed off as facts herein should only be construed as my very opinionated opinions.