| 1 | #!/usr/bin/perl |
| 2 | use v5.14; |
| 3 | use warnings; |
| 4 | |
| 5 | use CSS::Minifier::XS qw/minify/; |
| 6 | use Digest::SHA qw/sha256_base64/; |
| 7 | use IO::Compress::Gzip qw/gzip/; |
| 8 | use File::Slurp qw/read_file write_file edit_file_lines/; |
| 9 | use SVG::SpriteMaker; |
| 10 | use File::Which; |
| 11 | use List::Util qw/first/; |
| 12 | |
| 13 | mkdir 'static'; |
| 14 | mkdir 'static/css'; |
| 15 | mkdir 'static/js'; |
| 16 | mkdir 'static/logos'; |
| 17 | |
| 18 | sub gzip_file { |
| 19 | my ($file) = @_; |
| 20 | my $brotli = which 'brotli'; |
| 21 | my $zopfli = which 'zopfli'; |
| 22 | system $brotli => '--input', $file, '--output', "$file.br" if $brotli; |
| 23 | system $zopfli => $file if $zopfli; |
| 24 | gzip $file => "$file.gz", -Level => 9, Minimal => 1 unless $zopfli; |
| 25 | } |
| 26 | |
| 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 | |
| 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 | |
| 65 | my $default_theme = 'cyborg'; |
| 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 | |
| 85 | sub make_css { |
| 86 | my $css = join '', map { read_file $_ } <css/*.css>; |
| 87 | |
| 88 | my (%themes); |
| 89 | for (<css/themes/*>) { |
| 90 | my ($theme) = m,themes/(.*)\.css,; |
| 91 | $themes{$theme} = read_css_into_blocks $_; |
| 92 | } |
| 93 | my @themes = sort grep { $_ ne $default_theme } keys %themes; |
| 94 | |
| 95 | while (grep { scalar @$_ } values %themes) { |
| 96 | my %blocks = map { $_ => (shift @{$themes{$_}}) // '' } keys %themes; |
| 97 | $css .= $blocks{$default_theme}; |
| 98 | $css .= theme_prefix $_, $blocks{$_}, $blocks{$default_theme} for @themes |
| 99 | } |
| 100 | |
| 101 | write_gzfile "static/css/all.css", minify $css |
| 102 | } |
| 103 | |
| 104 | sub make_js { |
| 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/*>; |
| 106 | my $js = read_file 'static/js/all.js'; |
| 107 | write_gzfile 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js; |
| 108 | system 'cp', '-rp', 'js', 'static/'; |
| 109 | } |
| 110 | |
| 111 | my $sprite_mtime = -M 'static/logos/dark.svg' // 0; |
| 112 | for (<logos/*>, <logos-light/*>) { |
| 113 | if (!$sprite_mtime || $sprite_mtime > -M) { |
| 114 | make_logos; |
| 115 | last |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | my $css_mtime = -M 'static/css/all.css' // 0; |
| 120 | for (<css/*>, <css/themes/*>) { |
| 121 | if (!$css_mtime || $css_mtime > -M) { |
| 122 | make_css; |
| 123 | last |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | my $js_mtime = -M 'static/js/all.js' // 0; |
| 128 | for (<js/*>) { |
| 129 | if (!$js_mtime || $js_mtime > -M) { |
| 130 | make_js; |
| 131 | last |
| 132 | } |
| 133 | } |
| 134 | |
| 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="/; |
| 140 | } 'tmpl/skel.en'; |