1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
#!/usr/bin/perl
## dwt - perl script to support dreamweaver templates
## Copyright (C) 2003 Benjamin Stocker <bstocker@4s-hosting.ch>
##
## 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=<HF>; $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=<HF>; $templatesource=join("",@v);
$templatesource=&Dos2Unix($templatesource);
close HF;
# Correct links in template
$templatesource=&CorrectLinks($templatesource,$PROJECTPATH,$filename);
# Rebuild file using template
$p1=index($templatesource,"<!-- #BeginEditable ");
while ($p1>=0) {
$p1=index($templatesource,"\"",$p1);
if ($p1>=0) {
$p1=$p1+1;
$p2=index($templatesource,"\"",$p1);
$region=substr($templatesource,$p1,$p2-$p1);
&DebugMsg("Region: $region");
# Get region from file and paste into template
$rbegin="<!-- #BeginEditable \"$region\" -->";
$rend="<!-- #EndEditable -->"; # 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,"<!-- #BeginEditable ",$p1);
} # while
# Editable blocks inserted, now get library elements
$templatesource=&InsertLibs($templatesource, $projectpath, $filename);
# Finally, build frame
$templatesource=&RemoveHtmlTags($templatesource);
$templatesource=&AddHtmlTags($templatesource,$source);
# Save new file or write it to STDOUT
if (!$SCREEN) {
print "Updating $filename\n";
open HF,">$filename" or die "FATAL: Cannot create $filename";
print HF $templatesource;
close HF;
} else {
print $templatesource;
}
}
##
## Extract template filename
##
sub GetTemplateName() {
my ($p1,$p2,$v);
my $source=$_[0];
my $templatename="";
$p1=index($source, "<!-- #BeginTemplate ");
if ($p1>=0) {
$p1=index($source,"\"",$p1);
if ($p1>=0) {
$p1=$p1+1;
$p2=index($source,"\"",$p1);
$templatename=substr($source,$p1,$p2-$p1);
&DebugMsg("Template to be used: $templatename");
}
}
return $templatename;
}
##
## Add path specification to template filename, so it ca be opened
##
sub BuildPathName() {
my $templatename=$_[0];
my $prp=$_[1];
$templatename="$prp/$templatename";
# Remove unneeded slashes
$templatename=~s/\/\//\//g;
return $templatename;
}
##
## Remove header and footer from Template
##
sub RemoveHtmlTags() {
my $p1;
my $templatesource=$_[0];
$p1=index($templatesource,"<head>"); # 6 chars
if ($p1>=0) {
$p1=$p1+6;
$templatesource=substr($templatesource,$p1);
} else {
print "WARNING: Could not find <head> tag in template $templatename";
}
$p1=index($templatesource,"</body>");
if ($p1>=0) {
$templatesource=substr($templatesource,0,$p1);
} else {
print "WARNING: Could not find </body> tag in template $templatename";
}
return $templatesource;
}
##
## Add header and footer tags from source file to template
##
sub AddHtmlTags() {
my ($p1,$hstart,$hend);
my $templatesource=$_[0];
my $source=$_[1];
$p1=index($source,"<head>"); # 6 chars
if ($p1>=0) {
$p1=$p1+6;
$hstart=substr($source,0,$p1);
} else {
print "WARNING: Could not find <head> tag in $filename";
}
$p1=index($source,"</body>");
if ($p1>=0) {
$hend=substr($source,$p1);
} else {
print "WARNING: Could not find </body> tag in $filename";
}
return "$hstart$templatesource$hend";
}
##
## Remove '\r' (DOS CRLF Format -> UNIX)
##
sub Dos2Unix() {
my $v=$_[0];
$v=~s/\r//gm;
return $v;
}
##
## Correct template links in new created buffer
##
sub CorrectLinks() {
my $ts=$_[0];
my $prp=$_[1];
my $filename=$_[2];
my $l;
# Remove filename
$l=rindex($filename,'/');
$filename=substr($filename,0,$l);
# Get remaining path specification
$l=length($prp); $filename=substr($filename,$l);
$filename.='/'; $filename=~s/\/\//\//;
$ts=~s/\.\.$filename//g;
return $ts;
}
##
## Insert library items (if any)
##
sub InsertLibs() {
my $source=$_[0];
my $projectpath=$_[1];
my $sourcepath=$_[2];
my ($p1, $p2, $p3, $lib, $libpath);
my (@alb, $lb);
# Scan for library items
$p1=index($source,"<!-- #BeginLibraryItem ");
while ($p1>=0) {
# Found, get lib name
$p1=index($source,"\"",$p1);
if ($p1>=0) {
$p1=$p1+1;
$p2=index($source,"\"",$p1);
$lib=substr($source,$p1,$p2-$p1);
&DebugMsg("Library item: $lib");
# Read library from disk
$libpath="$projectpath$lib";
open LHF, $libpath or die "FATAL: Cannot read library file $libpath";
@alb=<LHF>;
$lb=join("", @alb);
$lb=&Dos2Unix($lb);
$lb=&CorrectLinks($lb, $projectpath, $sourcepath);
close LHF;
# Replace item
$p2=index($source, "-->", $p2);
if ($p2>=0) {
$p2=$p2+3;
$p3=index($source, "<!-- #EndLibraryItem -->", $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,"<!-- #BeginLibraryItem ", $p1);
}
return $source;
}
sub DebugMsg($) {
print "# $_[0]\n" if ($DEBUG);
}
##
## Show a small help screen
##
sub Usage() {
print "Usage: dwt.pl [-dov -e extensions] projectpath\n";
print " -d Show debug messages\n";
print " -o Show result but do not save it to file\n";
print " -v Show version\n";
print " -e Set extensions to search for (default: $SEXT)\n";
exit;
}
##
## Show version
##
sub Version() {
print "this is dwt $VERSION\n";
exit;
}
##
## Check command line arguments
##
sub CheckArguments() {
my (%args, $s);
&getopt('e',\%args);
foreach $s (keys(%args)) {
if ($s eq 'o') {
$SCREEN=1;
} elsif ($s eq 'd') {
$DEBUG=1;
} elsif ($s eq 'v') {
&Version();
} elsif ($s eq 'e') {
@AEXT=split(':',$args{$s});
} else {
print "Unknown option: -$s\n";
&Usage();
}
}
}
##
## Convert relative to absolute path
##
sub ExpandPath($) {
my $filename=$_[0];
my $pwd=$ENV{PWD};
# Convert filename to full pathname. Maybe there is a better way to do that?
# Could not find answer how to expand filename to full path at comp.lang.perl.*
if (substr($filename,0,1) ne '/') {
$filename="$pwd/$filename";
$filename=~s/\/[a-zA-Z0-9]+?\/\.\.\//\//; # Remove ' xxx/../ '
$filename=~s/\/\//\//g; # Remove ' // '
$filename=~s/\/\.//g; # Remove ' ./ '
}
$filename=~s/\/$//;
return $filename;
}
# EOF
|