Perl Script 1: A simple text to html converter


The following script converts text paragraphs to an html Web page via
interactive user queries about the attributes of the Web page format.

#!/usr/bin/perl -w
$USAGE = "Usage: webbuild.pl outputfile inputfile";

# Author: Laura LeMay
# Date: March 1999
# Updated by R. Katz Jan 2000 to provide concurrent file (an argument) output
#
# webbuild:  simple text-file conversion to HTML
# *very* simple.  Assumes no funky characters, embedded
# links or boldface, etc.  Blank spaces == paragraph
# breaks.

$title = '';                    # <TITLE>
$bgcolor = '';                  # BGCOLOR
$text = '';                     # TEXT
$head = '';                     # main heading
@text = ();                     # body text;
$mail = '';                     # email address

$out = shift; 
open (FILE, ">$out") || die "Couldn't create $out: $! ";

print "Enter the title to use for your web page: ";
chomp($title = <STDIN>);

foreach $color ('background', 'text') { # run twice, once for each color
    $in = '';                   # temporary input
    while () {
        print "Enter the $color color (? for options): ";
        chomp($in = <STDIN>);
        $in = lc $in;
        
        if ($in eq '?') {       # print help
            print "One of: \nwhite, black, red, green, blue,\n";
            print "orange, purple, yellow, aqua, gray,\n";
            print "silver, fuchsia, lime, maroon, navy,\n";
            print "olive, or Return for none\n";
            next;
        } elsif ($in eq '' or
                 $in eq 'white' or
                 $in eq 'black' or 
                 $in eq 'red' or
                 $in eq 'blue' or
                 $in eq 'green' or
                 $in eq 'orange' or
                 $in eq 'purple' or
                 $in eq 'yellow' or
                 $in eq 'aqua' or
                 $in eq 'gray' or 
                 $in eq 'silver' or
                 $in eq 'fuchsia' or
                 $in eq 'lime' or
                 $in eq 'maroon' or
                 $in eq 'navy' or
                 $in eq 'olive') { last; }
        else { 
            print "that's not a color.\n";
        }
    }
    
    if ($color eq 'background') {
        $bgcolor = $in;
    } else {
        $text = $in;
    }
}

print "Enter a heading: ";
chomp($head = <STDIN>);

print "Enter your email address: ";
chomp($mail = <STDIN>);

print '*' x 30;

print "\n<HTML>\n<HEAD>\n<TITLE>$title</TITLE>\n";
print FILE  "\n<HTML>\n<HEAD>\n<TITLE>$title</TITLE>\n";

print "</HEAD>\n<BODY";
print FILE  "</HEAD>\n<BODY";
if ($bgcolor ne '') { print qq( BGCOLOR="$bgcolor"); print FILE qq( BGCOLOR="$bgcolor"); }
if ($text ne '') { print qq( TEXT="$text"); print FILE qq( TEXT="$text"); }

print ">\n";
print FILE ">\n";

print "<H1>$head</H1>\n<P>";
print FILE "<H1>$head</H1>\n<P>";

while (<>) {
    if ($_ eq "\n") {
        print "<P>\n";
        print FILE "<P>\n"
    } else {
        print $_;
        print FILE $_;
    }
}

print qq(<HR>\n<ADDRESS><A HREF="mailto:$mail">$mail</A></ADDRESS>\n);
print FILE qq(<HR>\n<ADDRESS><A HREF="mailto:$mail">$mail</A></ADDRESS>\n);

print "</BODY>\n</HTML>\n";
print FILE  "</BODY>\n</HTML>\n";

close (FILE)

# END OF webbuild.pl

Questions about the questions? Send mail to Robert Katz: katz@cis.highline.ctc.edu
Last Update January 10, 2000