#!/bin/perl # $Id: merge,v 1.1 2005-06-26 18:39:02 andy_seaborne Exp $ # Take a template file and a file with content and merge them. # Content is defined as the stuff between certain
markers # NB The id=, not class= $template = "template.html" ; $target = undef ; $prog = $0 ; $extract = 0 ; $usage = "$prog [--extract] [--template FILE] FILE" ; if ( $#ARGV == -1 ) { print STDERR $usage, "\n" ; exit 0 ; } if ( $#ARGV == 0 && ( ( $ARGV[0] eq '-h' ) || ( $ARGV[0] eq '--help' ) ) ) { print STDERR $usage, "\n" ; exit 0 ; } if ( $#ARGV >= 0 && ( $ARGV[0] eq '--extract' ) ) { $extract = 1 ; shift @ARGV ; } if ( $#ARGV == 0 ) { $target = $ARGV[0] ; } elsif ( ( $#ARGV == 2 ) && ( $ARGV[0] eq '--template' ) ) { $template = $ARGV[1] ; $target = $ARGV[2] ; } else { print STDERR $usage, "\n" ; exit 1 ; } # Find directory of target (for relative links) if ( $target =~ m!/$! ) { print STDERR "'$target': Filename end in a /\n" ; exit 2 ; } $dir = undef ; if ( $target =~ m!(.*)/! ) { $dir = $1 ; @path = split('/', $dir) ; } ## Will be used so we can process file not in the current directory ## $unpath='' ; ## for $p (@path) ## { ## $unpath="$unpath".'../' ; ## } ## print "Unpath: '$unpath'\n" ; ## print "Dir = '$dir'\n" ; ## print $#path , "\n" ; ## exit ; # Read whole files. undef $/ ; open(TARGET, "<$target") || die "Open $target: $!"; binmode TARGET ; $_ = ; close(TARGET) ; # Remove comment ## s/\<\!--.*?--\>//g ; ## ---- Extract the content @sections = ("header", "footer", "trail", "content") ; %sections = () ; for $s (@sections) { $t = &doSectionNamed($_,$s) ; $sections{$s} = $t if ( $t ne '' ) ; } # And the HTML title. $title='' ; if ( m!(.*?)! ) { $title = $& ; } # And meta data @meta = m!\s*]*>\s*!sg ; if ( $extract ) { for $m (@meta) { print "Meta: $m\n" } # Debug: for $s (@sections) { print "-----------------------------------------------------------\n" ; print "Section:: $s\n" ; print $sections{$s} ; print "\n" if ( $sections{$s} !~ /\n$/s ) ; print "-----------------------------------------------------------\n" ; } print "Title:\n$title\n" ; print "\n" ; print "Meta data:\n" ; print @meta,"\n" ; exit ; } ## ---- Substitute open(TEMPLATE,"<$template") || die ; binmode TEMPLATE ; $_ =