#!/usr/bin/perl -w # Copyright (C) 2003 Anthony de Boer # # 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. # # 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. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA use strict; my $tpl = 'template'; my $tempfile = ".temp$$"; my $section = 'NULL'; my %sects; my @pagid; my @pagnm; my %subs; for my $s (qw/pages template buttons currpage endbuttons body/) { $sects{$s} = ''; } open(TPL, "template") || die "Cannot read $tpl: $!\n"; while() { next if /^$/; if (/^\>\>(\w+)$/) { $section = $1; die "Illegal section $section\n" unless defined($sects{$section}); } elsif ($section eq 'pages') { chomp; my ($pg, $name) = split(/\s+/, $_, 2); push (@pagid, $pg); push (@pagnm, $name); } else { die "Illegal section $section\n" unless defined($sects{$section}); $sects{$section} .= $_; } } close(TPL); undef($/); $subs{'date'} = 'TODAY'; sub outsec { my ($se) = @_; my $o = $sects{$se}; $o =~ s/\&\&(\w+)/$subs{$1}/ge; $o =~ s/(HREF\=\")index\.html(\")/$1\.\/$2/g; die "$tempfile write failed: $!\n" unless print TMP $o; } for my $thisi (0..$#pagid) { my $thispg = $pagid[$thisi]; my $thisname = $pagnm[$thisi]; $subs{'thisid'} = $thispg; $subs{'thisname'} = $thisname; my $file = $thispg . '.html'; my $content = ''; my $canary0 = "\<\!-- STARTPAGEBODY --\>"; my $canary9 = "\<\!-- ENDPAGEBODY --\>"; if (open(F, $file)) { $content = ; close(F); $content ="$canary0\n$canary9" if $content =~ /^\S*$/s; $content =~ s/^.*$canary0//s || die "No $canary0 in $file!\n"; $content =~ s/$canary9.*$//s || die "No $canary9 in $file!\n"; } else { my $nopen = $!; die "Cannot read $file: $nopen\n" if -f $file; $content = "\n"; } unlink($tempfile); open(TMP, ">$tempfile") || die "Can't write $tempfile: $!\n"; &outsec('template'); for my $linki (0..$#pagid) { my $linkpg = $pagid[$linki]; $subs{'linkid'} = $linkpg; $subs{'linkname'} = $pagnm[$linki]; &outsec(($linkpg eq $thispg) ? 'currpage' : 'buttons'); } delete($subs{'linkid'}); delete($subs{'linkname'}); &outsec('endbuttons'); die "Failure printing to $tempfile: $!\n" unless print TMP $canary0, $content, $canary9, "\n"; &outsec('body'); die "Failure closing $tempfile: $!\n" unless close(TMP); rename($tempfile, $file) || die "Cannot rename $tempfile to $file: $!\n"; } unlink($tempfile);