#!/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|<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|; &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(<DATA>) { $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 rep +laces 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('',<FILE>); 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"; }

In reply to Template HTML by thealienz1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.