| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
|
| 4 |
BEGIN { require 'common.pl' } |
| 5 |
|
| 6 |
my $source_file_name = shift; |
| 7 |
my $result_file_name = shift; |
| 8 |
my $status_file_name = shift; |
| 9 |
|
| 10 |
my %data; |
| 11 |
my %pattern; |
| 12 |
for_each_data_file (sub ($) { |
| 13 |
my $data_file_name = shift; |
| 14 |
warn "$data_file_name...\n"; |
| 15 |
load_data_file ($data_file_name, \%data, \%pattern); |
| 16 |
}); |
| 17 |
my @pattern = sort {length $b <=> length $a} keys %pattern; |
| 18 |
|
| 19 |
my $source_text; |
| 20 |
{ |
| 21 |
warn "$source_file_name...\n"; |
| 22 |
open my $source_file, '<:utf8', $source_file_name |
| 23 |
or die "$0: $source_file_name: $!"; |
| 24 |
local $/ = undef; |
| 25 |
$source_text = <$source_file>; |
| 26 |
} |
| 27 |
|
| 28 |
my $status = {}; |
| 29 |
|
| 30 |
warn "Generating...\n"; |
| 31 |
$source_text =~ s{(<(?>p(?>re)?|li|d[td]|t[dh]|h[1-6])(?>\s[^>]*)?>)((?>(?!</?(?>p(?>re)?|li|d(?>[tdl]|iv)|t[dh]|h[1-6r]|ul|ol)(?>\s[^>]*)?>).)+)} |
| 32 |
{ |
| 33 |
my ($tag, $text) = ($1, $2); |
| 34 |
my $n_text = normalize ($text); |
| 35 |
|
| 36 |
if (length $n_text) { |
| 37 |
my $ja_text = $data{$n_text}; |
| 38 |
|
| 39 |
$status->{all}++; |
| 40 |
|
| 41 |
if (defined $ja_text) { |
| 42 |
$status->{ja}++; |
| 43 |
$tag . q[<span class=ja-translation lang=ja>] . $ja_text . q[</span>]; |
| 44 |
} else { |
| 45 |
my $v = $tag . $text; |
| 46 |
for my $pattern (@pattern) { |
| 47 |
if ($n_text =~ /^$pattern$/) { |
| 48 |
$status->{ja}++; |
| 49 |
$v = $tag . q[<span class=ja-translation lang=ja>] . |
| 50 |
replace_pattern2 ($pattern{$pattern}, $1, $2, $3, $4, $5) . |
| 51 |
q[</span>]; |
| 52 |
last; |
| 53 |
} |
| 54 |
} |
| 55 |
$v; |
| 56 |
} |
| 57 |
} else { |
| 58 |
$1 . $2; |
| 59 |
} |
| 60 |
}ges; |
| 61 |
$source_text =~ s{(<(?>link|img|script)\s[^>]+>)}{ |
| 62 |
my $tag = $1; |
| 63 |
my $n_text = normalize ($tag); |
| 64 |
my $ja_text = $data{$n_text}; |
| 65 |
if (defined $ja_text) { |
| 66 |
$ja_text; |
| 67 |
} else { |
| 68 |
$tag; |
| 69 |
} |
| 70 |
}ges; |
| 71 |
|
| 72 |
$source_text =~ s{\[\[([A-Z ]+):([^]]+)\]\]} |
| 73 |
{<em class=rfc2119 title="$1">$2</em>}gs; |
| 74 |
|
| 75 |
#$source_text =~ s[<title>][<base href="http://www.whatwg.org/specs/web-apps/current-work/"><title>]; |
| 76 |
|
| 77 |
{ |
| 78 |
warn "$result_file_name...\n"; |
| 79 |
open my $result_file, '>:utf8', $result_file_name |
| 80 |
or die "$0: $result_file_name: $!"; |
| 81 |
print $result_file $source_text; |
| 82 |
} |
| 83 |
|
| 84 |
{ |
| 85 |
my $time = time; |
| 86 |
open my $status_file, '>>', $status_file_name |
| 87 |
or die "$0: $status_file_name: $!"; |
| 88 |
print $status_file "$time\t$status->{ja}\t$status->{all}\n"; |
| 89 |
} |