#!/usr/bin/perl -w #News Goat 1.9 # #This script loads data entries into a web page. In other words, it's a weblog. #Now using HTML::Template use strict; use CGI qw(:standard); use HTML::Template; sub getLongMonthName { my $month = shift(@_); #yuck, a big if else if ($month == 1) {return "January";} if ($month == 2) {return "February";} if ($month == 3) {return "March";} if ($month == 4) {return "April";} if ($month == 5) {return "May";} if ($month == 6) {return "June";} if ($month == 7) {return "July";} if ($month == 8) {return "August";} if ($month == 9) {return "September";} if ($month == 10) {return "October";} if ($month == 11) {return "November";} if ($month == 12) {return "December";} } sub getHour { my $hour = shift(@_); if ($hour > 0 && $hour <= 12) { return $hour; } else { return abs($hour - 12); } } sub getMeridian { my $hour = shift(@_); my $meridian = " a.m."; if ($hour > 11) { $meridian = " p.m."; } return $meridian; } sub printEntries { my $entry = shift; my $tenEntries = shift; my $entriesDir = shift; my @directoryList = @_; my $count = 0; my $file = ""; my @descfile = (); my @entries_data = (); foreach $file (@directoryList) { my %item_data; if (($file =~ /$entry/) || ($tenEntries && ($count < 10))) { my $year = substr($file, 0, 4); my $iMonth = substr($file, 4, 2); my $month = getLongMonthName($iMonth); my $day = substr($file, 6, 2); my $meridian = getMeridian(substr($file, 8, 2)); my $iHour = substr($file, 8, 2); my $hour = getHour($iHour); my $minute = substr($file, 10, 2); my $fullpath = $entriesDir.$file; open (ENTRY, $fullpath); @descfile = ; close (ENTRY); my $entry_title = $descfile[0]; chomp($entry_title); $item_data{MAIN_TITLE} = $entry_title; $item_data{MAIN_PERMALINK} = "/".$year."/".$iMonth."/".$day."/".$iHour.$minute."/"; $item_data{MAIN_TIMESTAMP} = $month." ".$day.", ".$year." -- ".$hour.":".$minute.$meridian; shift @descfile; my $line; my @item_lines = (); foreach $line (@descfile) { my %entry_line; $entry_line{ENTRY_LINE} = $line; push(@item_lines, \%entry_line); } $item_data{MAIN_ENTRY} = \@item_lines; push(@entries_data, \%item_data); $count++; } } return ($count, @entries_data); } my @descfile = (); my $entriesDir = "$ENV{'DOCUMENT_ROOT'}/entries/"; my $storiesDir = "$ENV{'DOCUMENT_ROOT'}/stories/"; my @directoryList = (); my $places = "$ENV{'DOCUMENT_ROOT'}/places.inc"; my $menu = "$ENV{'DOCUMENT_ROOT'}/menu.inc"; my $currentYear = 0; my $currentMonth = 0; my @currentTime = (); my $script = "$ENV{'SCRIPT_NAME'}"; my $defaultStory = "notfound"; my $template = HTML::Template->new(filename => 'newsgoat.tmpl'); @currentTime = localtime(time); $currentYear = 1900 + $currentTime[5]; $currentMonth = $currentTime[4] + 1; print header(); $template->param(SCRIPT => $script); #menu my @menu_data = (); open (MENU, $menu) or die "Can't open places file: $!"; while () { my %item_data; my $alt = ""; my $image = ""; my $URL = ""; ($alt, $image, $URL) = split(','); chomp($URL); $item_data{MENU_ALT} = $alt; $item_data{MENU_IMAGE} = $image; $item_data{MENU_LINK} = $URL; push(@menu_data, \%item_data); } close (MENU); $template->param(MENU => \@menu_data); #find all entries opendir(DIR, $entriesDir); @directoryList = grep(!/^\.{1,2}/, readdir(DIR)); closedir(DIR); @directoryList = sort {$b cmp $a} @directoryList; my $file = ""; my $currArch = ""; #history my @history_data = (); foreach $file (@directoryList) { my %item_data; my $moYear = substr($file, 0, 6); if ($moYear ne $currArch) { my $archMonth = getLongMonthName(substr($moYear, 4, 2)); my $archYear = substr($moYear, 0, 4); my $archMonthi = substr($moYear, 4, 2); $item_data{HISTORY_LINK} = "/".$archYear."/".$archMonthi."/"; $item_data{HISTORY_DATE} = $archMonth." ".$archYear; push(@history_data, \%item_data); } $currArch = $moYear; } $template->param(HISTORY => \@history_data); #main text my $entry = '^'; my $count = 0; my $tenEntries = 1; my $story = ""; my @results = (); if (!param('story')) { $template->param(STORY => 0); my $defaultEntry = ""; if ($currentMonth < 10) { $currentMonth = "0" . $currentMonth; } $defaultEntry .= $currentYear . $currentMonth; $defaultEntry .= '.*$'; if (!param('entry')) { @results = printEntries($defaultEntry, $tenEntries, $entriesDir, @directoryList); my $EntryCount = shift(@results); $template->param(ENTRIES => \@results); } else { $entry .= param('entry'); $entry .= '.*$'; $tenEntries = 0; @results = printEntries($entry, $tenEntries, $entriesDir, @directoryList); my $EntriesPrinted = shift(@results); if (! $EntriesPrinted) { print "

(Could not find any matching entries. Sorry. Hope the latest entries are good enough...)

\n"; $EntriesPrinted = printEntries($defaultEntry, $tenEntries, $entriesDir, @directoryList); } $template->param(ENTRIES => \@results); } } else { $template->param(STORY => 1); my @story_text = (); $story = param('story'); $story = $storiesDir . $story; if (! open(INF, $story)) { $story = $storiesDir . $defaultStory; open (INF, $story); } @descfile = ; my $text_line; foreach $text_line (@descfile) { my %story_line; $story_line{STORY_LINE} = $text_line; push(@story_text, \%story_line); } $template->param(STORY_TEXT => \@story_text); close(INF); } #Links. my @links_data = (); open (PLACES, $places) or die "Can't open places file: $!"; while () { my %item_data; my $title = ""; my $URL = ""; ($title, $URL) = split(','); chomp($URL); $item_data{PLACES_TITLE} = $title; $item_data{PLACES_LINK} = $URL; push(@links_data, \%item_data); } close (PLACES); $template->param(LINKS => \@links_data); print $template->output;