/
postprocess.pl
executable file
·329 lines (204 loc) · 7.97 KB
/
postprocess.pl
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
#!/usr/bin/env perl
# Copyright 2011-2012 by Enrique Nell
#
# Requires Pod::Simple::HTML
use 5.012;
use warnings;
use File::Copy;
use File::Basename;
use Readonly;
use Pod::Tidy qw( tidy_files );
use Text::WordDiff;
use Pod::Checker;
use Getopt::Long;
#use utf8;
$|++;
my (@names, $nohtml, $diff);
my $result = GetOptions(
"pod=s" => \@names,
"nohtml" => \$nohtml,
"diff" => \$diff,
);
die "Usage: perl postprocess.pl --pod <pod_name1> <pod_name2> ... [--nohtml] [--diff]\n"
unless $names[0];
# Hard-coded paths relative to /perldoc-es/tools
# OmegaT project
Readonly my $SOURCE_PATH => "../../omegat_516/516/source";
Readonly my $TARGET_PATH => "../../omegat_516/516/target";
Readonly my $MEM_PATH => "../../omegat_516/516/omegat/project_save.tmx";
# Clean OmegaT project (only reviewed segments)
Readonly my $CLEAN_PATH => "../../omegat_clean_prj/source";
Readonly my $CLEANM_PATH => "../../omegat_clean_prj/omegat/project_save.tmx";
# git repository
Readonly my $TRANSPOD_PATH => "../pod/translated";
Readonly my $REVPOD_PATH => "../pod/reviewed";
Readonly my $DIFF_PATH => "../pod/diff";
Readonly my $DISTR_PATH => "../POD2-ES/lib/POD2/ES";
Readonly my $WORK_PATH => "../memory/work/perlspanish-omegat.zipf.tmx";
# Diff file boilerplate
Readonly my $DIFF_HEADER => <<"END_HEADER";
<html>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=UTF-8'>
<style>
.file .fileheader { color: #888; }
.file .hunk ins { color: #060; font-weight: bold; }
.file .hunk del { color: #b22; font-weight: bold; }
</style>
</head>
<title>Word-oriented POD comparison</title>
<body>
END_HEADER
# read team from __DATA__ section
my (%team, %files);
while ( <DATA> ) {
chomp;
next if '';
my ($alias, @details) = split /,/;
$team{$alias} = $details[0]; # Name
if ( @details > 2 ) { # files translated by this team member
$files{$_} = $alias foreach @details[2 .. $#details];
}
}
close DATA;
# copy work memory to clean project => clean memory
copy($MEM_PATH, $CLEANM_PATH);
# copy work memory to /memory/work in repository
# and rename it perlspanish-omegat.zipf.tmx
copy($MEM_PATH, $WORK_PATH);
foreach my $pod_name (@names) {
my $source = "$SOURCE_PATH/$pod_name"; # src file in work OmegaT project
unless ( -f $source ) {
say "File $pod_name not found.";
next;
}
my $target = "$TARGET_PATH/$pod_name"; # translated file generated by OmegaT
my $trans_pod = "$TRANSPOD_PATH/$pod_name"; # file delivered by translator
my $rev_pod = "$REVPOD_PATH/$pod_name"; # file delivered by reviewer
my $clean = "$CLEAN_PATH/$pod_name"; # src file in clean OmegaT project
# Get path components
my ($name, $path, $suffix) = fileparse($target, qr{\.pod|\.pm|\..*});
my ( $ext ) = $suffix =~ /\.(.+)$/;
my ( $readme, $final_name );
if ( $name eq "README" ) {
$readme++;
say "Readme file" if $readme;
$final_name = "perl$ext.pod"; # new name convention for READMEs in 5.16
} else {
$final_name = $pod_name;
}
my $distr = "$DISTR_PATH/$final_name";
# copy source file to clean project => clean memory
copy($source, $clean);
# copy generated file to git archive (won't go through postprocessing)
copy($target, $rev_pod);
# copy generated file to distribution
copy($target, $distr);
# Replace double-spaces after full-stop with single space
open my $dirty, '<:encoding(UTF-8)', $distr; # OmegaT generates UTF-8 files
my $text = do { local $/; <$dirty> };
close $dirty;
$text =~ s/(?<=\.) (?=[A-Z])/ /g; # two white spaces after full stop
# TO DO: add more checks
# Check if there is a =encoding command
my $encoding;
if ( $text =~ /^=encoding (\S+)/m ) {
$encoding = $1;
if ( $encoding eq 'utf8' ) {
say "Found UTF-8 encoding command.";
} else {
say "Found alternative encoding command in POD. Changing to UTF-8...";
$text =~ s/^=encoding\s+$encoding/^=encoding utf8/;
}
} else {
say "No encoding command found. Adding '=encoding utf8'...";
$text = "=encoding utf8\n\n$text";
}
open my $fixed, ">:encoding(UTF-8)", $distr;
if ( $readme ) {
# Add pod formatting to the first paragraph, to help Pod::Tidy
print $fixed "=head1 FOO\n\n$text";
} else {
print $fixed $text;
}
close $fixed;
# Wrap lines (OmegaT removes some line breaks) using Pod::Tidy
my $processed = Pod::Tidy::tidy_files(
files => [ $distr ],
inplace => 1,
columns => 80,
);
if ( $readme ) {
# Remove added pod formatting from README files
open my $dirty, "<:encoding(UTF-8)", $distr;
my $text = do { local $/; <$dirty> };
close $dirty;
$text =~ s/^=head1 FOO\n\n//;
open my $fixed, ">:encoding(UTF-8)", $distr;
print $fixed $text;
close $fixed;
}
# Add TRANSLATORS section to distribution file
open my $out, ">>:encoding(UTF-8)", $distr;
my $translators_section = "\n=head1 TRADUCTORES\n\n=over\n\n";
my @file_team = ("explorer", "zipf"); # default team
unshift(@file_team, $files{$name}) if $files{$name};
$translators_section .= "=item * $team{$_}\n\n" foreach @file_team;
$translators_section .= "=back\n\n";
print $out $translators_section;
close $out;
# Check POD sintax/formatting
say "Checking POD syntax...";
podchecker($distr);
# Generate word-oriented diff file
if ( $diff ) {
say "Generating diff file...";
diff_file(
trans => $trans_pod,
rev => $rev_pod,
path => $DIFF_PATH,
name => $name,
extension => $ext,
header => $DIFF_HEADER,
);
}
# Generate HTML file for proofreading;
unless ( $nohtml ) {
say "Generating HTML version of POD file...";
my $html = "$REVPOD_PATH/$name$suffix.html";
system("perl -MPod::Simple::HTML -e Pod::Simple::HTML::go $distr > $html");
}
unlink "$distr~";
}
sub diff_file {
my %params = @_;
my (@trans, @rev);
open my $trans, "<:encoding(UTF-8)", $params{trans};
chomp(@trans = <$trans>);
close $trans;
open my $rev, "<:encoding(UTF-8)", $params{rev};
chomp(@rev = <$rev>);
close $rev;
my $target = "$params{path}/$params{name}_diff.html";
open my $out, ">:encoding(UTF-8)", $target;
say $out $DIFF_HEADER;
say $out "<h1>Comparison results for $params{name}.$params{extension}</h1>\n</br>";
for (my $i=0; $i < $#trans; $i++) {
$trans[$i] =~ s/</\(/g;
$trans[$i] =~ s/>/\)/g;
$rev[$i] =~ s/</\(/g;
$rev[$i] =~ s/>/\)/g;
if ( $rev[$i] ne $trans[$i] ) {
my $diff = word_diff \$trans[$i], \$rev[$i], { STYLE => 'HTML' };
say $out "<span style='color:blue'><b>TRANSLATOR:</b></span><br />$trans[$i]<br />";
say $out "<span style='color:red'><b>REVIEWER:</b></span><br />$rev[$i]<br />";
say $out "<span style='color:blueviolet'><b>CHANGES:</b></span><br />$diff<br />";
}
}
close $out;
}
__DATA__
j3nnn1,Jennifer Maldonado,C< jcmm986 + POD2ES at gmail.com >
mgomez,Manuel Gómez Olmedo,C< mgomez + POD2ES at decsai.ugr.es >,perlootut,perlobj,perlmod
explorer,Joaquín Ferrero (Tech Lead),C< explorer + POD2ES at joaquinferrero.com >
zipf,Enrique Nell (Language Lead),C< blas.gordon + POD2ES at gmail.com >