-
Notifications
You must be signed in to change notification settings - Fork 1
/
embed.pl
executable file
·46 lines (39 loc) · 941 Bytes
/
embed.pl
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
#!/usr/bin/env perl
# The following script replaces all images references by URL() directive with
# the relevant Data URI string.
use File::Spec;
use File::Basename;
use File::MimeInfo;
use MIME::Base64;
my $inputCSS = shift(@ARGV);
if (!$inputCSS) {
die ("USAGE: $0 <CSS file>\n");
}
my $dir = dirname($inputCSS);
open(F, $inputCSS) or die($!);
my $line;
while ($line = <F>) {
$line =~ s/([ \:])url\(([^\)]+)\)/"$1url(".encodeURL($2).")"/gie;
print $line;
}
close(F);
sub encodeURL {
my $url = shift;
my $original = $url;
$url =~ s/^['"](.*)['"]$/$1/;
$url = File::Spec->file_name_is_absolute($url) ? $url : $dir."/".$url;
my $mimeType = mimetype($url);
if ($mimeType =~ /^image\//) {
my $image;
{
local $/ = undef;
open (FILE, $url) or die($!);
binmode FILE;
$image = <FILE>;
close FILE;
}
my $encoded = encode_base64($image, "");
return "'data:$mimeType;base64,$encoded'";
}
return $original;
}