Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 330 lines (204 sloc) 8.16 kb
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
1 #!/usr/bin/env perl
2
0773491 @zipf Changes for v5.16
authored
3 # Copyright 2011-2012 by Enrique Nell
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
4 #
5 # Requires Pod::Simple::HTML
6
7 use 5.012;
56b1246 @zipf Removed redundant use strict;
authored
8 use warnings;
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
9 use File::Copy;
10 use File::Basename;
11 use Readonly;
12 use Pod::Tidy qw( tidy_files );
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
13 use Text::WordDiff;
4498c32 @zipf Added support for podchecker
authored
14 use Pod::Checker;
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
15 use Getopt::Long;
16 #use utf8;
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
17
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
18 $|++;
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
19
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
20 my (@names, $nohtml, $diff);
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
21
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
22 my $result = GetOptions(
23 "pod=s" => \@names,
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
24 "nohtml" => \$nohtml,
25 "diff" => \$diff,
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
26 );
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
27
28
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
29 die "Usage: perl postprocess.pl --pod <pod_name1> <pod_name2> ... [--nohtml] [--diff]\n"
0f3a1fa @zipf Now copies reviewer work memory to /memory/work and renames it. Other fi...
authored
30 unless $names[0];
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
31
32
33 # Hard-coded paths relative to /perldoc-es/tools
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
34 # OmegaT project
35 Readonly my $SOURCE_PATH => "../../omegat_516/516/source";
36 Readonly my $TARGET_PATH => "../../omegat_516/516/target";
37 Readonly my $MEM_PATH => "../../omegat_516/516/omegat/project_save.tmx";
38 # Clean OmegaT project (only reviewed segments)
39 Readonly my $CLEAN_PATH => "../../omegat_clean_prj/source";
40 Readonly my $CLEANM_PATH => "../../omegat_clean_prj/omegat/project_save.tmx";
41 # git repository
42 Readonly my $TRANSPOD_PATH => "../pod/translated";
43 Readonly my $REVPOD_PATH => "../pod/reviewed";
6e54daf @zipf Changed the path of generated diff files
authored
44 Readonly my $DIFF_PATH => "../pod/diff";
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
45 Readonly my $DISTR_PATH => "../POD2-ES/lib/POD2/ES";
46 Readonly my $WORK_PATH => "../memory/work/perlspanish-omegat.zipf.tmx";
47
48 # Diff file boilerplate
49 Readonly my $DIFF_HEADER => <<"END_HEADER";
50 <html>
51 <head>
52 <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=UTF-8'>
cb2541b @zipf Added css code to the header boilerplate, to avoid using an external fil...
authored
53 <style>
54 .file .fileheader { color: #888; }
55 .file .hunk ins { color: #060; font-weight: bold; }
56 .file .hunk del { color: #b22; font-weight: bold; }
57 </style>
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
58 </head>
59 <title>Word-oriented POD comparison</title>
60 <body>
61 END_HEADER
62
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
63
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
64 # read team from __DATA__ section
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
65 my (%team, %files);
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
66
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
67 while ( <DATA> ) {
0f3a1fa @zipf Now copies reviewer work memory to /memory/work and renames it. Other fi...
authored
68
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
69 chomp;
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
70
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
71 next if '';
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
72
0f3a1fa @zipf Now copies reviewer work memory to /memory/work and renames it. Other fi...
authored
73 my ($alias, @details) = split /,/;
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
74
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
75
0f3a1fa @zipf Now copies reviewer work memory to /memory/work and renames it. Other fi...
authored
76 $team{$alias} = $details[0]; # Name
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
77
78 if ( @details > 2 ) { # files translated by this team member
79
80 $files{$_} = $alias foreach @details[2 .. $#details];
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
81
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
82 }
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
83 }
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
84
4498c32 @zipf Added support for podchecker
authored
85 close DATA;
86
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
87
78f00f1 @zipf Removed copy of translation memories from the loop
authored
88
89 # copy work memory to clean project => clean memory
90 copy($MEM_PATH, $CLEANM_PATH);
91
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
92 # copy work memory to /memory/work in repository
93 # and rename it perlspanish-omegat.zipf.tmx
78f00f1 @zipf Removed copy of translation memories from the loop
authored
94 copy($MEM_PATH, $WORK_PATH);
95
96
97
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
98 foreach my $pod_name (@names) {
99
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
100 my $source = "$SOURCE_PATH/$pod_name"; # src file in work OmegaT project
f8c1bac @zipf Added a file check
authored
101
102 unless ( -f $source ) {
103 say "File $pod_name not found.";
104 next;
105 }
106
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
107 my $target = "$TARGET_PATH/$pod_name"; # translated file generated by OmegaT
108 my $trans_pod = "$TRANSPOD_PATH/$pod_name"; # file delivered by translator
109 my $rev_pod = "$REVPOD_PATH/$pod_name"; # file delivered by reviewer
110 my $clean = "$CLEAN_PATH/$pod_name"; # src file in clean OmegaT project
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
111
0773491 @zipf Changes for v5.16
authored
112 # Get path components
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
113 my ($name, $path, $suffix) = fileparse($target, qr{\.pod|\.pm|\..*});
0773491 @zipf Changes for v5.16
authored
114
115 my ( $ext ) = $suffix =~ /\.(.+)$/;
116
117 my ( $readme, $final_name );
118 if ( $name eq "README" ) {
119
120 $readme++;
121 say "Readme file" if $readme;
122
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
123 $final_name = "perl$ext.pod"; # new name convention for READMEs in 5.16
0773491 @zipf Changes for v5.16
authored
124
125 } else {
126
127 $final_name = $pod_name;
128
129 }
130
131 my $distr = "$DISTR_PATH/$final_name";
132
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
133 # copy source file to clean project => clean memory
134 copy($source, $clean);
135
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
136 # copy generated file to git archive (won't go through postprocessing)
137 copy($target, $rev_pod);
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
138
139 # copy generated file to distribution
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
140 copy($target, $distr);
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
141
142
143
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
144 # Replace double-spaces after full-stop with single space
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
145 open my $dirty, '<:encoding(UTF-8)', $distr; # OmegaT generates UTF-8 files
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
146
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
147 my $text = do { local $/; <$dirty> };
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
148
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
149 close $dirty;
150
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
151 $text =~ s/(?<=\.) (?=[A-Z])/ /g; # two white spaces after full stop
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
152 # TO DO: add more checks
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
153
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
154 # Check if there is a =encoding command
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
155 my $encoding;
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
156 if ( $text =~ /^=encoding (\S+)/m ) {
157
158 $encoding = $1;
159 if ( $encoding eq 'utf8' ) {
160
161 say "Found UTF-8 encoding command.";
162
163 } else {
164
165 say "Found alternative encoding command in POD. Changing to UTF-8...";
166 $text =~ s/^=encoding\s+$encoding/^=encoding utf8/;
167
168 }
169
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
170 } else {
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
171
172 say "No encoding command found. Adding '=encoding utf8'...";
173 $text = "=encoding utf8\n\n$text";
174
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
175 }
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
176
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
177
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
178 open my $fixed, ">:encoding(UTF-8)", $distr;
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
179
0f3a1fa @zipf Now copies reviewer work memory to /memory/work and renames it. Other fi...
authored
180 if ( $readme ) {
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
181
182 # Add pod formatting to the first paragraph, to help Pod::Tidy
183 print $fixed "=head1 FOO\n\n$text";
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
184
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
185 } else {
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
186
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
187 print $fixed $text;
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
188
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
189 }
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
190
191 close $fixed;
192
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
193
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
194 # Wrap lines (OmegaT removes some line breaks) using Pod::Tidy
195 my $processed = Pod::Tidy::tidy_files(
196 files => [ $distr ],
197 inplace => 1,
198 columns => 80,
199 );
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
200
201
0f3a1fa @zipf Now copies reviewer work memory to /memory/work and renames it. Other fi...
authored
202 if ( $readme ) {
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
203
204 # Remove added pod formatting from README files
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
205 open my $dirty, "<:encoding(UTF-8)", $distr;
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
206
207 my $text = do { local $/; <$dirty> };
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
208
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
209 close $dirty;
210
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
211
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
212 $text =~ s/^=head1 FOO\n\n//;
213
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
214
215 open my $fixed, ">:encoding(UTF-8)", $distr;
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
216
217 print $fixed $text;
218
219 close $fixed;
220 }
221
222
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
223 # Add TRANSLATORS section to distribution file
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
224 open my $out, ">>:encoding(UTF-8)", $distr;
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
225
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
226 my $translators_section = "\n=head1 TRADUCTORES\n\n=over\n\n";
227
228 my @file_team = ("explorer", "zipf"); # default team
229
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
230 unshift(@file_team, $files{$name}) if $files{$name};
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
231
232 $translators_section .= "=item * $team{$_}\n\n" foreach @file_team;
233 $translators_section .= "=back\n\n";
234
235 print $out $translators_section;
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
236
237 close $out;
4498c32 @zipf Added support for podchecker
authored
238
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
239
4498c32 @zipf Added support for podchecker
authored
240 # Check POD sintax/formatting
a513d48 @zipf Fixed a couple of messages
authored
241 say "Checking POD syntax...";
4498c32 @zipf Added support for podchecker
authored
242 podchecker($distr);
243
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
244
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
245 # Generate word-oriented diff file
246 if ( $diff ) {
247
bb7ad07 @zipf Improved the STDOUT messages
authored
248 say "Generating diff file...";
249
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
250 diff_file(
251 trans => $trans_pod,
252 rev => $rev_pod,
6e54daf @zipf Changed the path of generated diff files
authored
253 path => $DIFF_PATH,
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
254 name => $name,
255 extension => $ext,
256 header => $DIFF_HEADER,
257 );
258
259 }
260
261
dd10cbb @zipf Bug fixes: Pod::Tidy didn't cut lines of the first paragraph in README f...
authored
262 # Generate HTML file for proofreading;
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
263 unless ( $nohtml ) {
264
bb7ad07 @zipf Improved the STDOUT messages
authored
265 say "Generating HTML version of POD file...";
266
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
267 my $html = "$REVPOD_PATH/$name$suffix.html";
268 system("perl -MPod::Simple::HTML -e Pod::Simple::HTML::go $distr > $html");
269
270 }
3933a67 @zipf New approach to postprocessing; merged preprocessing.pl
authored
271
272 unlink "$distr~";
273
274 }
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
275
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
276
277 sub diff_file {
278
279 my %params = @_;
280
281 my (@trans, @rev);
282 open my $trans, "<:encoding(UTF-8)", $params{trans};
283 chomp(@trans = <$trans>);
284 close $trans;
285
286 open my $rev, "<:encoding(UTF-8)", $params{rev};
287 chomp(@rev = <$rev>);
288 close $rev;
289
290 my $target = "$params{path}/$params{name}_diff.html";
291
292 open my $out, ">:encoding(UTF-8)", $target;
293
294 say $out $DIFF_HEADER;
295 say $out "<h1>Comparison results for $params{name}.$params{extension}</h1>\n</br>";
296
297 for (my $i=0; $i < $#trans; $i++) {
94d0a2c @zipf Improved diff_file()
authored
298
299 $trans[$i] =~ s/</\(/g;
300 $trans[$i] =~ s/>/\)/g;
301
302 $rev[$i] =~ s/</\(/g;
303 $rev[$i] =~ s/>/\)/g;
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
304
305 if ( $rev[$i] ne $trans[$i] ) {
306
307 my $diff = word_diff \$trans[$i], \$rev[$i], { STYLE => 'HTML' };
308
3e96fde @zipf Fixed tag issue
authored
309 say $out "<span style='color:blue'><b>TRANSLATOR:</b></span><br />$trans[$i]<br />";
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
310
3e96fde @zipf Fixed tag issue
authored
311 say $out "<span style='color:red'><b>REVIEWER:</b></span><br />$rev[$i]<br />";
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
312
3e96fde @zipf Fixed tag issue
authored
313 say $out "<span style='color:blueviolet'><b>CHANGES:</b></span><br />$diff<br />";
1bec3e9 @zipf Added word-oriented diffs; code cleanup
authored
314
315 }
316
317 }
318
319
320 close $out;
321
322 }
323
324
1ab2a03 @zipf Added features: 1) handling utf-8-encoded pods 2) Specifying the transla...
authored
325 __DATA__
0f3a1fa @zipf Now copies reviewer work memory to /memory/work and renames it. Other fi...
authored
326 j3nnn1,Jennifer Maldonado,C< jcmm986 + POD2ES at gmail.com >
6e54daf @zipf Changed the path of generated diff files
authored
327 mgomez,Manuel Gómez Olmedo,C< mgomez + POD2ES at decsai.ugr.es >,perlootut,perlobj,perlmod
0f3a1fa @zipf Now copies reviewer work memory to /memory/work and renames it. Other fi...
authored
328 explorer,Joaquín Ferrero (Tech Lead),C< explorer + POD2ES at joaquinferrero.com >
329 zipf,Enrique Nell (Language Lead),C< blas.gordon + POD2ES at gmail.com >
Something went wrong with that request. Please try again.