This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir2lang
executable file
·435 lines (374 loc) · 12 KB
/
dir2lang
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/local/bin/perl
=head1 NAME
dir2lang - Convert SIWECOS translations to be used by the application
=head1 SYNOPSIS
B<dir2lang> [B<-v>] [B<--dryrun>] [SCANNER…]
B<dir2wiki> B<--help>|B<--man>
=head1 Description
dir2lang will take all SIWECOS translations found and copy them into PHP files.
If scanner names are given on the commandline, just the texts from those are taken.
=cut
use strict;
use warnings;
use FindBin qw($Bin $Script);
use Getopt::Long qw(:config no_ignore_case);
use Pod::Usage;
use lib "$Bin/lib";
use MediaWiki::API;
use Config::Simplest;
use File::Basename;
use File::Path qw(make_path);
use SIWECOS::Text::Scanner;
my $WARNCOLOR="\e[\$7;31;48m";
my $COLOROFF="\e[0m";
my $EXT='.wiki';
$|= 1;
my $settings= get_config("$Bin/wiki.conf", qw(WIKI API));
my $config= get_config("$Bin/siwecos.conf");
=head1 options
=over 4
=cut
my $dryrun = 0;
=item B<--dryrun>
=item B<-d>
Just test the translations.
=cut
my $test = '';
=item B<--test> DIRNAME
=item B<-t> DIRNAME
Like B<--dryrun> but create the files in DIRNAME.
=cut
my $verbose= 0;
=item B<-v>
Verbose output.
=cut
help() unless GetOptions(
'v+' => \$verbose,
'dryrun' => \$dryrun,
'test=s' => \$test,
'h|help' => \&help,
'm|man' => \&man,
);
=item B<--help>
short help page
=item B<--man>
show man page
=back
=cut
sub help { pod2usage(-verbose=>1); }
sub man { pod2usage(-verbose=>2); }
# Location and names of some directories / files
# $Bin = absolute path of this script
# $Script = filename of this script
my $TRANSLATIONS= "$Bin/texts";
my $LANGUAGE_DIR= $config->{LANGUAGE_DIR} || "$Bin/lang";
my $WIKI_LINK= $config->{TARGET_WIKI_LINK} || "siwecos_wiki";
my $WIKI_EXTERN= $config->{TARGET_EXTERN_LINK} || "siwecos_extern";
my $TEMPLATEFILE= "$Bin/$Script.txt";
my $SCANNER_NAME= 'SCANNER_NAME';
# Expected purposes
my $DESCRIPTION = 'Description';
my $HEADLINE = 'Headline';
my $LINK = 'Link';
my $ERROR_TEXT = 'Negative';
my $SUCCESS_TEXT = 'Positive';
my $TIPP = 'Solution_Tips';
my %PREPARE = (
$DESCRIPTION => \&parse_wiki,
$HEADLINE => \&oneline_wiki,
$LINK => \&wikilink,
$ERROR_TEXT => \&oneline_wiki,
$SUCCESS_TEXT => \&oneline_wiki,
$TIPP => \&parse_wiki,
);
my @PURPOSES= sort keys %PREPARE;
# Prepare some output stuff
# Sequence of textnames in output
my @SEQUENCE = (
$HEADLINE,
$SUCCESS_TEXT,
$ERROR_TEXT,
$DESCRIPTION,
$TIPP,
$LINK,
);
my $fill= maxlen( @SEQUENCE); # number of spaces to use to align values vertically
# First prepare the template
open my $template, '<', $TEMPLATEFILE
or die "Cannot read template $TEMPLATEFILE: $!\n";
my @template= <$template>;
close $template;
die "Cannot access directory $TRANSLATIONS\n"
unless -x $TRANSLATIONS and -r $TRANSLATIONS;
my $lang_dir= $test || $LANGUAGE_DIR;
if ( not -d $lang_dir ) {
make_path($lang_dir)
or die "Can't create $lang_dir: $!\n";
}
die "Can't write to $lang_dir.\n"
unless -w $lang_dir;
my $specific= '*';
if (@ARGV) {
$specific= '{' . join(",", splice(@ARGV)) . '}';
}
my @scanners;
my $all_okay= 1;
my %languages;
foreach my $dir (glob("$TRANSLATIONS/$specific")) {
print "$dir\n";
my $scanner= SIWECOS::Text::Scanner->read($dir);
if ($scanner) {
$all_okay&&= $scanner->validate == 0;
push @scanners, $scanner;
++$languages{$_} foreach ($scanner->languages)
} else {
warn "${WARNCOLOR}Issues with $dir$COLOROFF\n";
$all_okay= undef;
}
}
die "Please fix issues first.\n" unless $all_okay;
my $mw = MediaWiki::API->new({
api_url => $settings->{API},
});
my $standard_prepare= $PREPARE{$HEADLINE};
my $warning= 0;
foreach my $scanner (@scanners) {
my %missing_language;
@missing_language{keys %languages}= ();
delete @missing_language{ $scanner->languages};
my $scanner_name= $scanner->id;
foreach my $lang (keys %languages) {
if (exists $missing_language{$lang}) {
print "\rScanner $scanner_name: No translation for $lang\n";
next;
}
print "\rScanner $scanner_name, $lang\n" if $verbose;
my $output= '';
# go through the template, line by line
foreach (@template) {
# Until the placeholder /* DATA HERE */ is found
if( m#^(\s*)/\*\s*DATA\s*HERE\s*\*/\s*#) {
my $err= 0;
# Remember the indent
my $indent= $1;
# SCANNER_NAME COMES FIRST!
my $textname= $scanner_name;
$output.= "$indent// These are the $lang texts for $scanner_name\n";
my $translations= $scanner->name;
if (defined $translations) {
my $text= $translations->text($lang);
if( $standard_prepare ){
($text, $err)= $standard_prepare->($mw, $text, "Name of $scanner_name", $lang);
}
$output.= qq:${indent}'${SCANNER_NAME}' => '$text',\n\n:;
$warning+= $err;
}
# Texts for Tests
if (defined $scanner->tests) {
foreach my $testname (sort keys %{$scanner->tests}) {
my $test= $scanner->test($testname);
$output.= "$indent// Test: $testname\n";
foreach my $topic (@SEQUENCE) {
my $text= $test->topic($topic)->text($lang);
my $prepare= $PREPARE{$topic};
if( $prepare ){
($text, $err)= $prepare->($mw, $text, "$scanner_name: $testname: $topic", $lang);
}
my $suffix= uc $topic;
my $fillstring= ' ' x ($fill - length $suffix);
$output.= qq:$indent'${testname}_$suffix'$fillstring => '$text',\n:;
$warning+= $err;
}
$output.= "\n";
}
}
# Texts for results
my $all_results= $scanner->results;
if (defined $all_results) {
$output.= "$indent// Result texts follow\n";
my @resultnames= sort keys %{$all_results->results};
my $maxlen= maxlen (@resultnames);
foreach my $textname (@resultnames) {
my $text= $all_results->result($textname)->text($lang);
if( $standard_prepare ){
($text, $err)= $standard_prepare->($mw, $text, "$scanner_name: Resulttext $textname", $lang);
}
my $fillstring= ' ' x ($maxlen - length $textname);
$output.= qq:$indent'$textname'$fillstring => '$text',\n:;
$warning+= $err;
}
}
} else {
# Print an unmodified template line
$output.= $_;
}
}
if (not $dryrun) {
if (not -d "$lang_dir/$lang") {
make_path("$lang_dir/$lang") or
die "Cannot create $lang_dir/$lang: $!\n";
}
my $outfilename= "$lang_dir/$lang/$scanner_name.php";
print "Writing $outfilename\n" if $verbose;
open my $out, '>:utf8', $outfilename
or die "Failed to create $outfilename: $!\n";
print $out $output;
close $out unless $dryrun;
}
}
}
$mw->logout();
print $/ unless $verbose;
# done
exit $warning;
sub maxlen {
my $fill= 0;
foreach ( @_ ) {
my $l= length $_;
$fill= $l if $l > $fill;
}
return $fill;
}
sub parse_wiki {
my($mw, $text, $location, $lang)= @_;
my $warning= 0;
local($_)= $text;
if (defined $_) {
if (/\[|''/) {
s#^(\s*):(?=\w)#$1<nowiki>:</nowiki>#mg;
print "Parsing $location\n" if $verbose;
my $result= $mw->api({
action => 'parse',
format => 'json',
title => 'none',
text => $_,
prop => 'text|links|externallinks',
disablelimitreport => 'true',
});
if( not $result ) {
die $mw->{error}->{details}. ' ('. $mw->{error}->{code} . ')';
}
$_= $result->{parse}->{text}->{'*'};
foreach my $link (@{$result->{parse}->{links}}) {
my $printable_link= $link->{'*'};
my $linktext= quotemeta($printable_link);
if (not exists $link->{exists}) {
warn "${WARNCOLOR}Missing link: $printable_link in $location ($lang).$COLOROFF\n";
++$warning;
}
s#(<a\b[^>]+\b)(href=")/wiki(/[^>"]+"[^>]+\btitle="$linktext")#"$1target=\x22$WIKI_LINK\x22 $2".$settings->{WIKI}.$3#ge;
}
foreach my $link (@{$result->{parse}->{externallinks}}) {
my $linktext= quotemeta $link;
s#(<a\b[^>]*href="$linktext"[^>]*>)#replace_target($1, $WIKI_EXTERN)#ge;
}
} else {
print "Noparse $location\n" if $verbose;
s/\s+$//s;
s#\s\s+# </p><p>#sg;
$_= "<p>$_ </p>";
}
s/\\/\\\\/sg;
s/\n/ /sg;
s/\r//sg;
s/'/\\'/sg;
s/\s+$//s;
} else {
warn "${WARNCOLOR}No text in $location.$COLOROFF\n";
++$warning;
}
return ($_, $warning);
}
sub oneline_wiki {
local($_);
my($warning);
($_, $warning)= parse_wiki(@_);
if (defined $_) {
s#</?p>##sg;
s/ / /sg;
s/\s+$//s;
}
return ($_, $warning);
}
sub oneline {
my($mw, $text, $location, $lang)= @_;
local($_)= $text;
s/[\r\n]+/ /sg;
s/ / /sg;
s/\s+$//s;
return ($_, 0);
}
sub wikilink {
my($mw, $text, $location, $lang)= @_;
my $warning= 0;
for ($text) {
s/^[\s\012\015]+|[\s\012\015]+$//sg;
if (/[\015\012]/) {
die "Links must be single lines.\n"
. " Check $location\n";
}
if (/[\x00-\x1f]/) {
die "Illegal characters in link.\n"
. " Check $location\n";
}
}
my $pagename= $text."/".uc($lang);
print "Lookup $pagename\n" if $verbose;
my $page= my $ref = eval {
$mw->get_page( { title => $pagename } );
};
if ($@) {
warn "${WARNCOLOR}Error $@$COLOROFF\n";
warn "${WARNCOLOR}Page $pagename$COLOROFF\n" unless $verbose;
warn "${WARNCOLOR}Check $location$COLOROFF\n";
++$warning;
} elsif (exists $page->{missing}) {
warn "${WARNCOLOR}New $pagename$COLOROFF\n";
++$warning;
}
return ($settings->{WIKI}."/".$pagename, $warning);
}
sub replace_target {
my($a, $target)= @_;
$a=~ s/(?<=\btarget=)(["'']).*?\1/$1$target$1/g;
return $a;
}
__DATA__
# We need to use the wiki now
my $done= 0;
my $standard_prepare= $PREPARE{$HEADLINE};
foreach my $scanner (@scanners) {
sub update_percent {
my($done, $max)= @_;
++$$done;
printf "\r%5.1f%% ", $$done*100/$max;
}
sub maxlen {
my $fill= 0;
foreach ( @_ ) {
my $l= length $_;
$fill= $l if $l > $fill;
}
return $fill;
}
sub read_text {
my($path)= @_;
return "Missing $path" unless -d $path;
return "Can't read $path" unless -r $path;
my $result;
foreach my $file (glob("$path/??$EXT")) {
my $lang= lc basename $file, $EXT;
++$LANGUAGES{$lang};
$result||= {};
$result->{$lang}{location}= $file;
open my $in, '<:utf8', $file
or return "Can't read $file";
$result->{$lang}{text}= do { local $/; <$in>; };
close $in;
for ($result->{$lang}) {
s/[\012\015]+$//s;
s/[ \t]+$//gm;
}
}
return $result || "No translations in $path";
}