#!/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 &main(); 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|(.*)|si) { $title = $1; } else{ $title = "Upper St. Clair High Schoool"; } print "\t\tParsing Document - "; if($content=~m|(.*?)|si) { $content = $header_html . $1 . $footer_html; $content =~ s|%title%|$title|; &save_file("$fullpath",$content); 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 copy("$fullpath","$dir"); } } #Returns the contents of a filename specified... sub open_file{ my($file) = @_; my($file_contents) = ""; open(DATA,"$file") || die "Not Completed\n"; while() { $file_contents .= $_; } close(DATA); return($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 replaces it with new one my($dir) = $file; if($dir =~ /(.*)\/.*/) {$dir = $1;} if(!(-e "$dir/")) { mkdir("$dir/",0755); } open(DATA,">$file") || die "Cannot saved parse document\n"; print DATA "$file_contents"; close(DATA); } sub main{ #Retrieves HTML for template print "Opening Template - "; open(FILE,"$html_file") || die "Cannot open HTML because $!"; my $data = join('',); close(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"; }