]>
Commit | Line | Data |
---|---|---|
8798626b MG |
1 | #!/usr/bin/perl |
2 | use v5.14; | |
3 | use warnings; | |
4 | ||
5eae1f77 | 5 | use CSS::Minifier::XS qw/minify/; |
7e8f9a5c | 6 | use Digest::SHA qw/sha256_base64/; |
3c9bcb47 | 7 | use IO::Compress::Gzip qw/gzip/; |
7e8f9a5c | 8 | use File::Slurp qw/read_file write_file edit_file_lines/; |
cf39f49f | 9 | use SVG::SpriteMaker; |
fa8b7f99 | 10 | use File::Which; |
5eae1f77 | 11 | use List::Util qw/first/; |
8798626b MG |
12 | |
13 | mkdir 'static'; | |
14 | mkdir 'static/css'; | |
f57a9178 | 15 | mkdir 'static/js'; |
cf39f49f | 16 | mkdir 'static/logos'; |
c9dee865 | 17 | |
3c9bcb47 MG |
18 | sub gzip_file { |
19 | my ($file) = @_; | |
fa8b7f99 MG |
20 | my $zopfli = which 'zopfli'; |
21 | system $zopfli => $file if $zopfli; | |
22 | gzip $file => "$file.gz", -Level => 9, Minimal => 1 unless $zopfli; | |
3c9bcb47 MG |
23 | } |
24 | ||
cf39f49f MG |
25 | sub write_gzfile { |
26 | my ($file, @content) = @_; | |
27 | write_file $file, @content; | |
28 | gzip_file $file | |
29 | } | |
30 | ||
31 | sub sprite_name { | |
32 | my ($name) = $_[0] =~ m,/(.*)\.svg,; | |
33 | "logo-$name" | |
34 | } | |
35 | ||
36 | sub make_logos { | |
37 | my @logos = <logos/*>; | |
38 | my $logos = make_sprite \&sprite_name, @logos; | |
39 | my @logos_light; | |
40 | for (<logos/*>) { | |
41 | my $light = s/logos/logos-light/r; | |
42 | push @logos_light, -f $light ? $light : $_; | |
43 | } | |
44 | my $logos_light = make_sprite \&sprite_name, @logos_light; | |
45 | ||
46 | write_gzfile 'static/logos/dark.svg', $logos->render; | |
47 | write_gzfile 'static/logos/light.svg', $logos_light->render; | |
48 | } | |
49 | ||
5eae1f77 MG |
50 | sub read_css_into_blocks { |
51 | my ($file) = @_; | |
52 | my (@blocks, $block); | |
53 | for (read_file $file) { | |
54 | $block .= $_; | |
55 | if (/^}/) { | |
56 | push @blocks, $block; | |
57 | $block = ''; | |
58 | } | |
59 | } | |
60 | \@blocks | |
61 | } | |
62 | ||
c13e110a | 63 | my $default_theme = 'slate'; |
db73dc0c MG |
64 | |
65 | sub theme_prefix { | |
66 | my ($theme, $decl, $default) = @_; | |
67 | return $decl if $theme eq $default_theme || !$decl; | |
68 | return '' if $decl eq $default; | |
69 | ||
70 | $default =~ s/[^{]*{\n//; | |
71 | $default =~ s/\n}[^}]*//; | |
72 | $decl =~ s/^$_$//m for split "\n", $default; | |
73 | $decl =~ s/\n+/\n/g; | |
74 | ||
75 | my $prefix = "html.$theme"; | |
76 | my ($first_line) = $decl =~ /([^{]*){/; | |
77 | $first_line =~ s/(,\s+)/$1 $prefix /g; | |
78 | $first_line = "$prefix $first_line"; | |
79 | $decl =~ s/([^{]*){/$first_line\{/; | |
80 | $decl | |
81 | } | |
82 | ||
cf2b0139 | 83 | sub make_css { |
db73dc0c | 84 | my $css = join '', map { read_file $_ } <css/*.css>; |
5eae1f77 | 85 | |
db73dc0c | 86 | my (%themes); |
cf2b0139 | 87 | for (<css/themes/*>) { |
db73dc0c MG |
88 | my ($theme) = m,themes/(.*)\.css,; |
89 | $themes{$theme} = read_css_into_blocks $_; | |
5eae1f77 | 90 | } |
db73dc0c | 91 | my @themes = sort grep { $_ ne $default_theme } keys %themes; |
5eae1f77 MG |
92 | |
93 | while (grep { scalar @$_ } values %themes) { | |
94 | my %blocks = map { $_ => (shift @{$themes{$_}}) // '' } keys %themes; | |
db73dc0c MG |
95 | $css .= $blocks{$default_theme}; |
96 | $css .= theme_prefix $_, $blocks{$_}, $blocks{$default_theme} for @themes | |
5eae1f77 MG |
97 | } |
98 | ||
db73dc0c | 99 | write_gzfile "static/css/all.css", minify $css |
8798626b MG |
100 | } |
101 | ||
cf2b0139 | 102 | sub make_js { |
8345760a | 103 | system java => -jar => 'compiler.jar', qw,-O SIMPLE --create_source_map static/js/js.map --js_output_file static/js/all.js --language_in ECMASCRIPT6_STRICT --language_out ECMASCRIPT5_STRICT --source_map_location_mapping js/|/static/js/,, <js/*>; |
f57a9178 | 104 | my $js = read_file 'static/js/all.js'; |
cf39f49f | 105 | write_gzfile 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js; |
f57a9178 | 106 | system 'cp', '-rp', 'js', 'static/'; |
cf2b0139 MG |
107 | } |
108 | ||
cf39f49f MG |
109 | my $sprite_mtime = -M 'static/logos/dark.svg' // 0; |
110 | for (<logos/*>, <logos-light/*>) { | |
7fd2e6a5 | 111 | if (!$sprite_mtime || $sprite_mtime > -M) { |
cf39f49f | 112 | make_logos; |
7fd2e6a5 MG |
113 | last |
114 | } | |
115 | } | |
116 | ||
db73dc0c | 117 | my $css_mtime = -M 'static/css/all.css' // 0; |
cf2b0139 MG |
118 | for (<css/*>, <css/themes/*>) { |
119 | if (!$css_mtime || $css_mtime > -M) { | |
120 | make_css; | |
121 | last | |
122 | } | |
123 | } | |
124 | ||
b5d06694 | 125 | my $js_mtime = -M 'static/js/all.js' // 0; |
cf2b0139 MG |
126 | for (<js/*>) { |
127 | if (!$js_mtime || $js_mtime > -M) { | |
128 | make_js; | |
129 | last | |
130 | } | |
9cad7bdd | 131 | } |
69d77267 | 132 | |
7e8f9a5c MG |
133 | edit_file_lines { |
134 | my ($file) = m,(static.*\.(?:css|js)),; | |
135 | return unless $file; | |
136 | my $hash = sha256_base64 scalar read_file $file; | |
137 | s/integrity=".*"/integrity="sha256-$hash="/; | |
65ef5465 | 138 | } 'tmpl/skel.en'; |