]>
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) = @_; | |
1b5b61ae | 20 | my $brotli = which 'brotli'; |
fa8b7f99 | 21 | my $zopfli = which 'zopfli'; |
1b5b61ae | 22 | system $brotli => '--input', $file, '--output', "$file.br" if $brotli; |
fa8b7f99 MG |
23 | system $zopfli => $file if $zopfli; |
24 | gzip $file => "$file.gz", -Level => 9, Minimal => 1 unless $zopfli; | |
3c9bcb47 MG |
25 | } |
26 | ||
cf39f49f MG |
27 | sub write_gzfile { |
28 | my ($file, @content) = @_; | |
29 | write_file $file, @content; | |
30 | gzip_file $file | |
31 | } | |
32 | ||
33 | sub sprite_name { | |
34 | my ($name) = $_[0] =~ m,/(.*)\.svg,; | |
35 | "logo-$name" | |
36 | } | |
37 | ||
38 | sub make_logos { | |
39 | my @logos = <logos/*>; | |
40 | my $logos = make_sprite \&sprite_name, @logos; | |
41 | my @logos_light; | |
42 | for (<logos/*>) { | |
43 | my $light = s/logos/logos-light/r; | |
44 | push @logos_light, -f $light ? $light : $_; | |
45 | } | |
46 | my $logos_light = make_sprite \&sprite_name, @logos_light; | |
47 | ||
48 | write_gzfile 'static/logos/dark.svg', $logos->render; | |
49 | write_gzfile 'static/logos/light.svg', $logos_light->render; | |
50 | } | |
51 | ||
5eae1f77 MG |
52 | sub read_css_into_blocks { |
53 | my ($file) = @_; | |
54 | my (@blocks, $block); | |
55 | for (read_file $file) { | |
56 | $block .= $_; | |
57 | if (/^}/) { | |
58 | push @blocks, $block; | |
59 | $block = ''; | |
60 | } | |
61 | } | |
62 | \@blocks | |
63 | } | |
64 | ||
385eb223 | 65 | my $default_theme = 'cyborg'; |
db73dc0c MG |
66 | |
67 | sub theme_prefix { | |
68 | my ($theme, $decl, $default) = @_; | |
69 | return $decl if $theme eq $default_theme || !$decl; | |
70 | return '' if $decl eq $default; | |
71 | ||
72 | $default =~ s/[^{]*{\n//; | |
73 | $default =~ s/\n}[^}]*//; | |
74 | $decl =~ s/^$_$//m for split "\n", $default; | |
75 | $decl =~ s/\n+/\n/g; | |
76 | ||
77 | my $prefix = "html.$theme"; | |
78 | my ($first_line) = $decl =~ /([^{]*){/; | |
79 | $first_line =~ s/(,\s+)/$1 $prefix /g; | |
80 | $first_line = "$prefix $first_line"; | |
81 | $decl =~ s/([^{]*){/$first_line\{/; | |
82 | $decl | |
83 | } | |
84 | ||
cf2b0139 | 85 | sub make_css { |
db73dc0c | 86 | my $css = join '', map { read_file $_ } <css/*.css>; |
5eae1f77 | 87 | |
db73dc0c | 88 | my (%themes); |
cf2b0139 | 89 | for (<css/themes/*>) { |
db73dc0c MG |
90 | my ($theme) = m,themes/(.*)\.css,; |
91 | $themes{$theme} = read_css_into_blocks $_; | |
5eae1f77 | 92 | } |
db73dc0c | 93 | my @themes = sort grep { $_ ne $default_theme } keys %themes; |
5eae1f77 MG |
94 | |
95 | while (grep { scalar @$_ } values %themes) { | |
96 | my %blocks = map { $_ => (shift @{$themes{$_}}) // '' } keys %themes; | |
db73dc0c MG |
97 | $css .= $blocks{$default_theme}; |
98 | $css .= theme_prefix $_, $blocks{$_}, $blocks{$default_theme} for @themes | |
5eae1f77 MG |
99 | } |
100 | ||
db73dc0c | 101 | write_gzfile "static/css/all.css", minify $css |
8798626b MG |
102 | } |
103 | ||
cf2b0139 | 104 | sub make_js { |
8345760a | 105 | 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 | 106 | my $js = read_file 'static/js/all.js'; |
cf39f49f | 107 | write_gzfile 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js; |
f57a9178 | 108 | system 'cp', '-rp', 'js', 'static/'; |
cf2b0139 MG |
109 | } |
110 | ||
cf39f49f MG |
111 | my $sprite_mtime = -M 'static/logos/dark.svg' // 0; |
112 | for (<logos/*>, <logos-light/*>) { | |
7fd2e6a5 | 113 | if (!$sprite_mtime || $sprite_mtime > -M) { |
cf39f49f | 114 | make_logos; |
7fd2e6a5 MG |
115 | last |
116 | } | |
117 | } | |
118 | ||
db73dc0c | 119 | my $css_mtime = -M 'static/css/all.css' // 0; |
cf2b0139 MG |
120 | for (<css/*>, <css/themes/*>) { |
121 | if (!$css_mtime || $css_mtime > -M) { | |
122 | make_css; | |
123 | last | |
124 | } | |
125 | } | |
126 | ||
b5d06694 | 127 | my $js_mtime = -M 'static/js/all.js' // 0; |
cf2b0139 MG |
128 | for (<js/*>) { |
129 | if (!$js_mtime || $js_mtime > -M) { | |
130 | make_js; | |
131 | last | |
132 | } | |
9cad7bdd | 133 | } |
69d77267 | 134 | |
7e8f9a5c MG |
135 | edit_file_lines { |
136 | my ($file) = m,(static.*\.(?:css|js)),; | |
137 | return unless $file; | |
138 | my $hash = sha256_base64 scalar read_file $file; | |
139 | s/integrity=".*"/integrity="sha256-$hash="/; | |
65ef5465 | 140 | } 'tmpl/skel.en'; |