line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Command::Generate::Lexicon; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
2939
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
132
|
|
4
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
97
|
|
5
|
3
|
|
|
3
|
|
3225
|
use utf8; |
|
3
|
|
|
|
|
35
|
|
|
3
|
|
|
|
|
20
|
|
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
127
|
use base 'Mojo::Command'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
2328
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = 0.992_2; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use File::Find; |
12
|
|
|
|
|
|
|
use Getopt::Long; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use MojoX::I18N::Lexemes; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
__PACKAGE__->attr(description => <<'EOF'); |
17
|
|
|
|
|
|
|
Generate lexicon file from templates. |
18
|
|
|
|
|
|
|
EOF |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
__PACKAGE__->attr(usage => <<"EOF"); |
21
|
|
|
|
|
|
|
usage: $0 generate lexicon [language] [--behavior=save||reset] [templates] |
22
|
|
|
|
|
|
|
Options: |
23
|
|
|
|
|
|
|
-b, --behavior=BEHAVIOR |
24
|
|
|
|
|
|
|
Determine how to work with existent lexems, possible values: |
25
|
|
|
|
|
|
|
save save old lexicon values; |
26
|
|
|
|
|
|
|
reset delete old lexicon. |
27
|
|
|
|
|
|
|
EOF |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub run { |
30
|
|
|
|
|
|
|
my $self = shift; |
31
|
|
|
|
|
|
|
my $language = shift; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my @templates; |
34
|
|
|
|
|
|
|
my $s = Mojo::Server->new; |
35
|
|
|
|
|
|
|
my $app = $s->app; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $verbose; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $app_class = $s->app_class; |
40
|
|
|
|
|
|
|
$app_class =~ s{::}{/}g; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$language ||= 'Skeleton'; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $behavior = ''; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
local @ARGV = @_ if @_; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $result = GetOptions( |
49
|
|
|
|
|
|
|
"behavior|b:s{1,1}" => \$behavior, |
50
|
|
|
|
|
|
|
'verbose|v:1' => \$verbose, |
51
|
|
|
|
|
|
|
'<>' => sub { push @templates, $_[0] if $_[0] } |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $handler = $app->renderer->default_handler; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Find all templates of project |
57
|
|
|
|
|
|
|
unless (@templates) { |
58
|
|
|
|
|
|
|
find( |
59
|
|
|
|
|
|
|
sub { |
60
|
|
|
|
|
|
|
push @templates, $File::Find::name if (/\.$handler/); |
61
|
|
|
|
|
|
|
}, |
62
|
|
|
|
|
|
|
$app->renderer->root |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $lexem_file = $app->home->rel_file("lib/$app_class/I18N/$language.pm"); |
67
|
|
|
|
|
|
|
my %oldlex = (); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
if ($language ne 'Skeleton' && -e $lexem_file) { |
70
|
|
|
|
|
|
|
if (lc $behavior eq 'save') { |
71
|
|
|
|
|
|
|
%oldlex = eval { |
72
|
|
|
|
|
|
|
require "$app_class/I18N/$language.pm"; |
73
|
|
|
|
|
|
|
no strict 'refs'; |
74
|
|
|
|
|
|
|
%{*{"${app_class}::I18N::${language}::Lexicon"}}; |
75
|
|
|
|
|
|
|
}; |
76
|
|
|
|
|
|
|
%oldlex = () if ($@); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
elsif (lc $behavior eq 'reset') { |
79
|
|
|
|
|
|
|
# Just proceed |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
else { |
82
|
|
|
|
|
|
|
print <
|
83
|
|
|
|
|
|
|
Lexemes already exists. |
84
|
|
|
|
|
|
|
You must set `--behavior' to one of "reset" or "save". |
85
|
|
|
|
|
|
|
USAGE |
86
|
|
|
|
|
|
|
return; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my $l = MojoX::I18N::Lexemes->new(renderer => $self->renderer); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my %lexicon = %oldlex; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
foreach my $file (@templates) { |
95
|
|
|
|
|
|
|
open F, $file or die "Unable to open $file: $!"; |
96
|
|
|
|
|
|
|
my $t = do { local $/; }; |
97
|
|
|
|
|
|
|
close F; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# get lexemes |
100
|
|
|
|
|
|
|
print "Parsing $file \n" if $verbose; |
101
|
|
|
|
|
|
|
my $parsed_lexemes = $l->parse($t); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# add to all lexemes |
104
|
|
|
|
|
|
|
foreach (grep { !exists $lexicon{$_} } @$parsed_lexemes) { |
105
|
|
|
|
|
|
|
$lexicon{$_} = ''; |
106
|
|
|
|
|
|
|
print "New lexeme found => $_\n" if $verbose; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Output lexem |
111
|
|
|
|
|
|
|
$self->render_to_file('package', $lexem_file, $app_class, $language, |
112
|
|
|
|
|
|
|
\%lexicon); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__DATA__ |