#!/usr/bin/perl ## dwt - perl script to support dreamweaver templates ## Copyright (C) 2003 Benjamin Stocker ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## DESCRIPTION: ## This is a perl script to apply a modified Dreamweaver (DW) ## template to a file using this template. Macromedia ## Dreamweaver manages this automatically whenever you ## change a tepmplate. See DW Documentation for more infos. ## When you have a DW-Project with templates but no DW, you ## cann change templates, but this changes will not be added ## to files using the template. ## This script can solve this problem. It takes the name of a ## file as argument, scans this file for a template and adds ## all template changes to the file. ## ## Check http://sys.4s-hosting.ch/bstocker/dwt/ ## for more informations and updates. ## ## Please, let me know it this script works for you, your questions ## and comments are welcome! use Getopt::Std; use IO::Dir; # Write debug informations to STDOUT (-d) $DEBUG=0; # Version $VERSION="1.11 (06/9/2003)"; # Display result on screen (-o); $SCREEN=0; # Extensions to search $SEXT="php:php3:php4:htm:html:phtml"; @AEXT=split(':',$SEXT); &CheckArguments(); ## Check command line arguments if ($ARGV[0] eq '') { &Usage; } $PROJECTPATH=$ARGV[0]; $PROJECTPATH=&ExpandPath($PROJECTPATH); unless (-d $PROJECTPATH) { print "FATAL: Cannot stat '$PROJECTPATH'. argument must be a directory\n"; exit; } &ScanDir($PROJECTPATH); # All done! exit(0); ## ## Scan all directories and files in specified path ## sub ScanDir($) { my $path=$_[0]; my ($dirname,$newpath,%dhf); tie %dhf, IO::Dir, $path; if (defined %dhf) { foreach $dirname(keys(%dhf)) { if ($dirname ne '.' and $dirname ne '..') { $newpath="$path/$dirname"; &CheckExtension($newpath); if (-d $newpath) { &ScanDir($newpath); } } } } else { die "FATAL: Cannot list $path"; } undef $dhf; } ## ## Check if extension matches ## sub CheckExtension($) { my $filename=$_[0]; my $s; foreach $s (@AEXT) { if ($filename=~/\.$s$/i) { &DebugMsg($filename); &DoDWT($filename); } } } ## ## Merge template and document ## sub DoDWT($) { my $filename=$_[0]; my (@v,$source,$templatesource,$templatename); my ($p1,$p2,$region,$rbegin,$rend,$rbuf,$q1,$q2); # Read file into buffer open HF, $filename or die "FATAL: Cannot open: $filename"; @v=; $source=join("",@v); $source=&Dos2Unix($source); close HF; # Extract template name from buffer $templatename=&GetTemplateName($source); if ($templatename eq '') { print "NOTE: Could not extract template name from $filename\n"; return; # function execution ends here! NW would not like that :) } # Open template file $templatename=&BuildPathName($templatename,$PROJECTPATH); open HF, $templatename or die "FATAL: Cannot open template file: $templatename"; @v=; $templatesource=join("",@v); $templatesource=&Dos2Unix($templatesource); close HF; # Correct links in template $templatesource=&CorrectLinks($templatesource,$PROJECTPATH,$filename); # Rebuild file using template $p1=index($templatesource,""; $rend=""; # 21 characters, see below! $q1=index($source,$rbegin); $q2=index($source,$rend,$q1); if ($q1>=0 and $q2>=0) { # Extract from file $rbuf=substr($source,$q1,$q2-$q1+21); # Paste into template $q1=index($templatesource,$rbegin); $q2=index($templatesource,$rend,$q1); if ($q1>=0 and $q2>=0) { $q2=$q2+21; $templatesource=substr($templatesource,0,$q1).$rbuf.substr($templatesource,$q2); } else { print "WARNING: Could not find region '$region' in template $templatename"; } } else { print "WARNING: Could not find region '$region' in $filename"; } } $p1=index($templatesource,"", $p2); if ($p2>=0) { $p2=$p2+3; $p3=index($source, "", $p2); if ($p3>=0) { # Build the updated document $source=substr($source, 0, $p2) . $lb . substr($source, $p3); } else { print "WARNING: End tag for library $lib not found "; } } else { print "WARNING: Error in library tag $lib. Please correcet markup"; } } $p1=index($source,"