Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Template HTML

by thealienz1 (Pilgrim)
on Feb 13, 2001 at 21:00 UTC ( #58154=sourcecode: print w/replies, xml ) Need Help??
Category: HTML Utility
Author/Contact Info Justin Archie

Takes a directory, and all sub directories, of files, and copys and parses them to a template HTML file.

Used for a site I made where the people were to lazy to jsut insert the template into each page, but this make it easier to change if you change the template again.

Yes I understand that SSI can be used, but I still lazy to do that too... ENJOY!

#!/usr/bin/perl -w
# opens up original HTML and inserts
# into template html. and mirrors the other
# files that it doesn't parse....

#Define variables
    #Directory to parse (with trailing slash)
    $open_dir = 'C:/winnt/profiles/jt/desktop/html/';
    #Directory to save parsed documents to (with trailing slash)
    $save_dir = 'C:/winnt/profiles/jt/desktop/blah/';
    #Location of template HTML file
    $html_file = 'C:/winnt/profiles/jt/desktop/template.html';
    #Extensions allowed to parse
    @exts = ('html','htm','shtml','shtm');

#Used to find html files to replace
use File::Find;
#Used to copy files to new location
use File::Copy;
#Assists in copying path directory structure
use File::Path;

#Starts the actual code

exit; #Just in case of any accidents

sub eachFile {
    my $filename = $_;
    my $fullpath = $File::Find::name;
    #remember that File::Find changes your CWD, 
    #so you can call open with just $_
    my $found = 0;
    foreach $ext (@exts) {
        if($filename=~/\.$ext$/) {
            print "\tOpening file $filename - ";
            my $content = &open_file("$filename");
            print "Completed\n";
            if($content=~m|<TITLE>(.*)</TITLE>|si) {
                $title = $1;
            } else{
                $title = "Upper St. Clair High Schoool";
            print "\t\tParsing Document - ";
            if($content=~m|<BODY.*?>(.*?)</BODY>|si) {
                $content = $header_html . $1 . $footer_html;
                $content =~ s|%title%|$title|;
                print "Completed\n";
            } else{
                print "Couldn't parse\n";
            $found = 1;
            last; #So it doesn't reopen it with similar extension
    if($found==0) {
        my $dir = $fullpath;
        $dir=~s/\Q$open_dir\E/$save_dir/i; #Removes current root dir, 
+and replaces it with new one

#Returns the contents of a filename specified...
sub open_file{
    my($file) = @_;
    my($file_contents) = "";
    open(DATA,"$file") || die "Not Completed\n";
        while(<DATA>) {
            $file_contents .= $_;

#Saves the new file to its new location
sub save_file{
    my($file,$file_contents) = @_;
    $file=~s/$open_dir/$save_dir/i; #Removes current root dir, and rep
+laces it with new one
    my($dir) = $file;
    if($dir =~ /(.*)\/.*/) {$dir = $1;}
    if(!(-e "$dir/")) {
    open(DATA,">$file") || die "Cannot saved parse document\n";
        print DATA "$file_contents";

sub main{
    #Retrieves HTML for template
    print "Opening Template - ";
    open(FILE,"$html_file") || die "Cannot open HTML because $!";
        my $data = join('',<FILE>);
    #Please let them be global variables
    ($header_html,$footer_html) = split(/\%content\%/,$data);
    print "Successful\n";

    #Copys directory structure of old to new one...
    print "Copying Directory Path - ";
    mkpath([$open_dir, $save_dir], 1, 0711); 
    print "Successful\n";

    #Starts the actual File Search & Replace
    print "Starting Search and Replace - \n";
    find (\&eachFile, "$open_dir");
    print "Successful\n";
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://58154]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2020-09-19 10:30 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (114 votes). Check out past polls.