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
|
#!/usr/bin/perl -w
# Scan templates and append checksum of each file to the JavaScript file URL.
# Author: Elan Ruusamäe <glen@delfi.ee>
use strict;
use File::Find ();
my %cache;
sub checksum {
my ($file) = @_;
return $cache{$file} if exists $cache{$file};
open(my $fh, '<', 'htdocs/'.$file) or die $!;
my $checksum = do {
local $/; # slurp!
unpack('%32C*', <$fh>) % 65535;
};
close($fh);
return $cache{$file} = sprintf '%x', $checksum;
}
sub put {
my ($file, $contents) = @_;
open(my $fh, '>', $file) or die $!;
print $fh $contents;
close($fh);
}
sub process_file {
my ($file) = @_;
my @lines;
open(my $fh, '<', $file) or die $!;
while (<$fh>) {
if (my ($tag, $script) = $_ =~ /(<(?:script.+src|link.+rel="stylesheet".+href)="{\$rel_url})([^"]+)/i) {
my ($pre, $post) = ($`, $');
if ($script !~ /\?/) {
$_ = $pre. $tag. $script .'?c='.checksum($script). $post;
}
}
push(@lines, $_);
}
close($fh);
put($file, join('', @lines));
}
my @files;
File::Find::find({
wanted => sub {
/^.*\.tpl\.html\z/s && push(@files, $File::Find::name);
}
}, 'templates');
foreach (@files) {
process_file($_);
}
|