#!/usr/bin/env perl
#This is a simple cgi program that does not accept any user input.
#When accessed from a web browser,
#it reads text from calender files at http://www.ou.edu/calendar/,
#modifies the text slightly, and sends it a the browser.
#The modifications consist of html tags that colorize certain
#text matched in regular expressions.
#A good reference for perl regular expressions is:
#http://www.troubleshooters.com/codecorn/littperl/perlreg.htm
#Students are required to change some of those regular expressions,
#see TASK below.
#
use LWP::Simple; #provides ability to access files via http
@months=('january','february','march','april','may','june',
'july','august','september','ocotober','november','december');
$calurl="http://www.ou.edu/calendar/";
#this filter is "demo-ed" with this practice file:
$practice='http://it.metr.ou.edu/regex/filt/practice.htm';
#following needed to construct file names of the current files
@a=localtime();
$month=$a[4]+1; #current month number
$nextmonth=($month +1)%12; #next month number (reverts to 1 if 13)
@files=($practice); #use this practice file for development
#use these files for your useful app:
#@files=($calurl.$months[$month-1].'.htm', $calurl.$months[$nextmonth-1].'.htm');
#this MUST be the first line spit out by a cgi script:
print "Content-type: text/html\n\n";
#next write out some html to send to a browser:
print "
My OU events";
print "my enhancement of:
\n";
$content=""; #this single string will hold all the text from the input file(s)
foreach $file (@files) {
print ''.$file."
\n"; #print link to original files
$content.=get($file) or die("no $file, die"); # this "gets" the file at the URL
}
print "
\n";
#Here is somethine easy: color the word "Free" red. Note the use of %
#as the delimiter, rather than the conventional /
#Also the appended g is for "global", for all matches in the
#very big string $content
$content=~s%Free%Free%g;
#------------------------------------------------------------
# A bit more fancy: colorize 'ticket' or 'tickets'.
#In the following, $& is the text of the entire match. Thus, html
#colorizing tags are being inserted before and after the match.
#The appended i is for "case insensitive"
$content=~s%ticket[s]?%$&%gi;
#------------------------------------------------------------
#Even more fancy, look for calls to a phone number. Use parenthesis
#to make subgroups 1 and 2. Subgroup 1 matches the end of a sentence,
#subgroup 2 matches from the end of that sentence through a phone
#number. Subgoup 2 is colorized. Note \. means a real "period",
#and not the metacharacter.
#The [^\.] means "anything but a 'period'".
$content=~s%(\.\s+)([^\.]*?\s+\d\d\d-\d\d\d\d)%$1$2%g;
#------------------------------------------------------------
#TASK:
# 1) highlight in color ff00ff all instances of "woman" and "women",
# using one regex
# hint: this is easy
#------------------------------------------------------------
# 2) highlight in color red all "Lecture" and subsequent title in quotes.
# look at the source html: see quotes are strings “ and ”
# see http://www.natural-innovations.com/wa/doc-charset.html
#------------------------------------------------------------
# 3) Find ALL things that look like URLs. e.g. www.snomnh.ou.edu,
# and make an href, e.g. www.snomnh.ou.edu
# hint: \w and \. may be useful in your regex
print $content;