#! /usr/local/bin/perl ############################################################################# # # NOTE: This file under revision control using RCS # Any changes made without RCS will be lost # # $Source: U:\\mhansen\\sites\\marchansen\\bin\\RCS\\view.cgi,v $ # $Revision: 1.4 $ # $Date: 2002-11-09 11:13:45-06 $ # $Author: mhansen $ # $Locker: mhansen $ # $State: Exp $ # # Purpose: CGI to display tech note files as HTML. # # Description: Takes ASCII text files formatted in the "tech note" # format and outputs HTML. Prepends standard header. # Appends standard footer. Generates and prepends index. # Converts titles and section headings to different # display Format. Converts embedded URL's and other tech # note references to links. # # Directions: Usage: view.cgi/[tech_note] # If tech_note argument is supplied it must be in the # default tech note directory. Argument is passed as # extra path information, NOT with ? delimiter # # If no argument is supplied then it is assumed that # the tech note is "index.txt" in the current directory. # Presumably, there is an index.cgi in the same directory # which is a link to view.cgi. This feature permits # simpler paths for tech note URLs. # # Default Location: http:/www.marchansen.com/bin/view.cgi # # Invoked by: httpd, typically as UID nobody # # Copyright (C) 1997 Marc Hansen # # This program is free software; you can redistribute it and/or # modify it under the terms of version 2 of the GNU General Public # License as published by the Free Software Foundation available at: # http://http://www.gnu.org/copyleft/gpl.html # # 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. # # ############################################################################# # # # REVISION HISTORY: # # $Log: view.cgi,v $ # Revision 1.4 2002-11-09 11:13:45-06 mhansen # Fixed bug in index table # # Revision 1.3 2000-03-04 00:52:35-06 mhansen # Added link conversion for ftp:// # # Revision 1.2 2000-02-19 16:16:33-06 mhansen # Added index # # Revision 1.1 1998-05-09 13:22:43 mhansen # Change references to other tech notes to links # # Revision 1.0 1997-09-13 16:27:36 mhansen # Initial revision # # # # Constants # $BASE ="/htdocs/userdirs/mhansen"; # Base path to httpd docroot from # perl interpreters point of view. $DEFAULTNOTE="index.txt"; # Default file name for tech note $DIR="/tech"; # Default directory for tech note # # Get file to display if ($ENV{PATH_INFO} =~ /^\/(.*)/) { # If we have extra path info $file = $1; # we assume it is path to tech } # note and relative to $DIR. else { $file = "$DEFAULTNOTE"; # If we have no arguments then we $DIR = "."; # assume file name is "index.txt" } # and directory is current dir. if ($DIR eq "."){ # Set path to file $path = "$file" } else { $path = "$BASE/$DIR/$file" } # # Print HTTP header so we can see error messages print "Content-type: text/html\n\n"; # # Check for file # Get tech note number from file name $file =~ /^(\d+)_/; $number = $1; ## # Open tech note and read through it to build index, # reformat, and insert links. # Push note body on to array @text # Push note index on to array @index # # Default format for body is
push(@text,"
");                       

# Open file
open(NOTE, "< $path") ||                    
fatal("Cannot open file '$DIR/$file'");

# Begin loop through tech note
foreach $line () {

    # Look for comment lines with name value pairs
    # which look like '#       key: ...'
    if ($line =~ /^#\s+([^:]+):\s*(.*)\s*$/){
        ($key,$value) = ($1,$2);
        $value =~ s/^\s+|\s+$//g;
        $value =~ s/\$$//g;

        if ($key =~ /Title/ ) {$title=$value}
        if ($key =~ /Number/ ) {
            $tnum=$value;
            $tnum =~ /^TN(\d+)/;
            $number = $1;
        }
    }


    # Look for section headings
    # lines begin with nnn.n
    elsif ($line =~ /^$number\.(\d+)(\s+)(.*)\s*$/){
    
        # Push on to index array
        push(@index, "$number.$1  $3\n");
        
        # Push on to body array
        push(@text, "

$number.$1    $3

\n");
    }


    # If the line is not a comment line, then push it on to body array
    elsif ($line !~ /^#/) {
    
        # Translate characters that are not allowed in HTML 
        # to entity equivilants, must do & to & first 
        $line =~ s/&/&/g;

        #Translate any < or > symbols to < or >
        $line =~ s//>/g; 

        # Change references to email addresses to 'mailto:' links
        $line =~ s/([\w\.\/-]+\@\w+\.[\w\.\/-]+)/a("mailto:$1",$1)/eg;

        # Change http/s URL references to links
        $line =~ s/(https?:[#@~\w\.\/:\+\?=-]+[\/\w])/a($1,$1)/eg;

        # Change ftp URL references to links
        $line =~ s/(ftp:[#@~\w\.\/:\+\?=-]+[\/\w])/a($1,$1)/eg;

        # Change references to other tech notes to links
        $line =~ s/TN(\d\d\d)\.(\d+)/a("\/tn$1\/#$2","TN$1.$2")/eg;
        $line =~ s/TN(\d\d\d)([^\.])/a("\/tn$1","TN$1")/eg;

        # Push rewritten $line on to body array
        push(@text,$line);
        
    }
    
} # End of loop to read through tech note

# Close tech note file
close(NOTE);

# Close format tag for body of tech note
push(@text,"
"); ## # Output tech note as HTML # # # Open HTML document print "\n"; print ''; print "\n\n"; print "\n"; print " \n"; print " $title\n"; print " \n"; print " \n"; print " \n"; print " \n"; print "\n"; print "\n"; # # Prepend standard html header for this site &include("/include/header.html"); # # Put whole page in a table to contain width print "
\n"; # # Document title print "

$tnum - $title

\n"; # # Tech note index # goes in its own table inside whole page table print "\n"; print join('',@index); print "
\n"; # # Tech note body print join('',@text); # # Close out "whole page" table print "
\n"; # # Append standard html footer for this site &include("/include/footer.html"); # # Close HTML document print "\n"; print "\n"; # # Done exit (0); ################### # # Function: include ($file) # Purpose: copies file to stdout # Arguments: $file - path to file, relative to httpd docroot # Output: to stdout # Depends on: Expects global variable $BASE set to base path # to prepend to httpd docroot. # sub include { my($file) = @_; open(INCLUDE, "< $BASE/$file") || fatal("Cannot include file '$file'"); while () {print $_} close(INCLUDE); return 0; } ################### # # Function: a ($href, $text) # Purpose: rewrite args as HTML anchor to simplify sytax in regex # Arguments: $href - href path # $text - text of link # Output: returns string $text # sub a { my($href, $text) = @_; return ''.$text.''; } ################### # # Function: fatal ($msg) # Purpose: Print error message to stdout, close HTML page and exit # Arguments: $msg - Error message to print # Output: to stdout # sub fatal { my($msg) = @_; print($msg); print "\n\n\n"; exit 1; }