-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg-css-compiler.pl
77 lines (58 loc) · 1.35 KB
/
svg-css-compiler.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
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
#!/usr/bin/perl
use warnings;
use strict;
use constant SVG_DIR => 'input';
use constant OUTPUT_FILE => 'output/compiled.css';
# Don't include extension
my @file_names = qw(
ac ca fi lk pl tf ad cc fj im mg pm tg ae cd fk
in mh pn th af cf fm io mk pr tj ag cg fo iq ml
);
# Two variables will go into this template:
#
# [% id %] the name of the SVG file being written as a string
# [% svg %] url() with the SVG as a data: URI
use constant CSS_TEMPLATE => <<TEMPLATE_END;
.flag-[% id %] {
background-image: [% svg %]
background-repeat: no-repeat;
width: 320px;
height: 240px;
}
TEMPLATE_END
sub css_class {
my ($id, $svg_lines) = @_;
my $template = CSS_TEMPLATE;
$template =~ s/\[% id %\]/$id/;
$template =~ s{\[% svg %\]} {
'url("data:image/svg+xml,' .
join('', @{$svg_lines}) .
'");'
}em;
return $template;
}
sub uri_encode {
my $line = shift;
my %encoded = (
'<' => '%3C',
'>' => '%3E',
'#' => '%23',
'"' => '\''
);
$line =~ s/\s{2,}//g;
foreach my $char (keys %encoded) {
$line =~ s/$char/$encoded{$char}/g;
}
return $line;
}
open (my $OUT, '>', OUTPUT_FILE) or die $!;
for my $file (@file_names) {
open (my $SVG_FILE, '<', SVG_DIR . "/$file.svg") or die $!;
my $svg_lines;
while (<$SVG_FILE>) {
chomp;
next if $_ eq '\n';
push $svg_lines->@*, uri_encode($_);
}
print $OUT css_class($file, $svg_lines);
}