All pastes #178224 Raw Edit

Someone

public text v1 · immutable
#178224 ·published 2006-09-21 03:04 UTC
rendered paste body
#!/usr/bin/perl -w
# Templatize v0.0
# Mabus

use strict;
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);
my ($titlemark, $bodymark) = 0;	# these are to make sure the correct lines
				# go in the correct variables
while(<TEMPLATE>){
	if (/<!--body-->/){
		$bodymark = 1;
		next;
	}
	if (/<!--title-->/){
		$titlemark = 1;
		next;
	}
	# Now that we know this line is part of a template section
	# Put it in the appropriate one
	push @toptemp, $_ if ($titlemark == 0);
	push @midtemp, $_ if (($titlemark == 1) and ($bodymark == 0));
	push @bottemp, $_ if (($titlemark == 1) and ($bodymark == 1));
}


foreach(@ARGV){
	my (@title, @body);
	my $curfile = $_;
	open CURFILE, "<", $curfile or die "Cannot open $curfile : $!\n";
	while (<CURFILE>){
		my $line = $_;
		# 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, $line if ($titleover == 0);
		push @body, $line if ($bodyreached == 1);
	}
	# Close the file for reading, open it again to write
	close CURFILE;
#	open CURFILE, ">", $curfile;
#	select CURFILE;

	# Add the markers that templatize looks for
	# for future runs of the script
	unshift @title, "<!--title start-->\n";
	push @title, "<!--title end-->\n";
	unshift @body, "<!--body start-->\n";
	push @body, "<!--body end-->\n";

	# Write all those arrays to your file, done!
	print "@toptemp";
	print "@title";
	print "@midtemp";
	print "@body";
#	print "@bottemp";
}