#!/usr/bin/perl -w
# Templatize v0.0
# Mabus
use diagnostics;
my $usage = "Usage: $0 [files to be templated]\n";
die "$usage" if (@ARGV==0);
# This is the location of the template file
my $template = "/var/www/users/mabus/template.tpl";
open TEMPLATE, "<", $template;
#Now, let's put the template to memory sections
my (@toptemp, @midtemp, @bottemp);
while(<TEMPLATE>){
my ($title, $body) = 0; # these are to make sure the correct lines
# go in the correct variables
if (/<!--body-->/){
$body = 1;
next;
}
if (/<!--title-->/){
$title = 1;
next;
}
# Now that we know this line is part of a template section
# Put it in the appropriate one
push @toptemp, $_ if (($title == 0) and ($body == 0));
push @midtemp, $_ if (($title == 1) and ($body == 0));
push @bottemp, $_ if (($title == 1) and ($body == 1));
}
foreach(@ARGV){
my (@title, @body);
$curfile = $_;
open CURFILE, "<", $curfile or die "Cannot open $curfile : $!\n";
while (<CURFILE>){
# These are markers to know whether to put stuff in the title or body
# Or to just ignore it as part of a previous template
my ($titlereached, $titleover, $bodyreached) = 0;
# Now, to set markers when found.
if (/^<!--title start-->$/){
$titlereached++;
next;
}
if (/^<!--title end-->$/){
$titleover++;
next;
}
if (/^<!--body start-->$/){
$bodyreached++;
next;
}
last if (/^<!--body end-->$/);
# Discard the lines if they're in the template section
next if ($titlereached == 0);
next if (($titleover == 1) and ($bodyreached == 0));
# Now to actually put the contents to memory
push @title if ($titleover == 0);
push @body if ($bodyreached == 1);
}
# Close the file for reading, open it again to write
close CURFILE;
open CURFILE, ">", $curfile;
select CURFILE;
print "@toptemp";
print "<!--title start-->";
print "@title";
print "<!--title end-->";
print "@midtemp";
print "<!--body start-->";
print "@body";
print "<!--body end-->";
print "@bottemp";
}