summaryrefslogtreecommitdiff
path: root/util/jsgen.pl
blob: 1ad8fb530cd5f694771a1909074a35005f4a006b (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/perl

package VNDB;

use strict;
use warnings;
use Encode 'encode_utf8';
use Cwd 'abs_path';
eval { require JavaScript::Minifier::XS; };

our($ROOT, %S);
BEGIN { ($ROOT = abs_path $0) =~ s{/util/jsgen\.pl$}{}; }
require $ROOT.'/data/global.pl';

use lib "$ROOT/lib";
use lib "$ROOT/yawf/lib";
use LangFile;

# The VNDB::L10N module is not really suited to be used outside the VNDB::*
# framework, but it's the central location that defines which languages we have
# and in what order to display them.
use VNDB::L10N;


my $jskeys_lang = join '|', VNDB::L10N::languages();
my $jskeys = qr{^(?:
    _lang_(?:$jskeys_lang)|
    _js_.+|
    _menu_emptysearch|
    _vnpage_uopt_(?:10?vote|rel.+)|
    _rlst_[vr]stat_.+|
    _vnedit_rel_(?:isa|of|addbut|del|none|findformat|novn|double)|
    _redit_form_med_.+|
    _vnedit_scr_.+|
    _tagv_(?:add|spoil\d|notfound|nometa|double)|
    _redit_form_vn_(?:addbut|remove|none|vnformat|notfound|double)|
    _redit_form_prod_(?:addbut|remove|none|pformat|notfound|double)|
    _pedit_rel_(?:addbut|del|none|findformat|notfound|double)
  )$}x;

sub l10n {
  # Using JSON::XS or something may be shorter and less error prone,
  #  although I would have less power over the output (mostly the quoting of the keys)

  my $lang = LangFile->new(read => "$ROOT/data/lang.txt");
  my @r;
  push @r, 'L10N_STR = {';
  my $cur; # undef = none/excluded, 1 = awaiting first TL line, 2 = after first TL line
  my %lang;
  while((my $l = $lang->read())) {
    my $type = shift @$l;
    if($type eq 'key') {
      my $key = shift @$l;
      push @r, '  }' if $cur;
      $cur = $key =~ $jskeys ? 1 : undef;
      if($cur) {
        $r[$#r] .= ',' if $r[$#r] =~ /}$/;
        # let's assume key names don't trigger a reserved word in JS
        $key = qq{"$key"} if $key !~ /^[a-z_][a-z0-9_]*$/i;
        push @r, qq|  $key: {|;
      }
    }
    $lang{$l->[0]} = 1 if $type eq 'tl';
    if($type eq 'tl' && $cur) {
      my($lang, $sync, $val) = @$l;
      next if !$val;
      $val =~ s/"/\\"/g;
      $val =~ s/\n/\\n/g;
      $r[$#r] .= ',' if $cur == 2;
      $lang = qq{"$l->[0]"} if $lang =~ /^(?:as|do|if|in|is)$/; # reserved two-char words
      push @r, qq|    $lang: "$val"|;
      $cur = 2;
    }
  }
  push @r, '  }' if $cur;
  push @r, '};';
  push @r, 'L10N_LANG = [ '.join(', ', map qq{"$_"}, VNDB::L10N::languages()).' ];';
  return join "\n", @r;
}


sub jsgen {
  # JavaScript::Minifier::XS doesn't correctly handle perl's unicode,
  #  so just do everything in raw bytes instead.
  my $js = encode_utf8(l10n()) . "\n";
  $js .= sprintf "rlst_rstat = [ %s ];\n", join ', ', map qq{"$_"}, @{$S{rlst_rstat}};
  $js .= sprintf "rlst_vstat = [ %s ];\n", join ', ', map qq{"$_"}, @{$S{rlst_vstat}};
  open my $JS, '<', "$ROOT/data/script.js" or die $!;
  $js .= join '', <$JS>;
  close $JS;
  open my $NEWJS, '>', "$ROOT/static/f/script.js" or die $!;
  print $NEWJS $JavaScript::Minifier::XS::VERSION ? JavaScript::Minifier::XS::minify($js) : $js;
  close $NEWJS;
}

jsgen;