/[suikacvs]/markup/html/html5/spec-ja/make2.pl
Suika

Contents of /markup/html/html5/spec-ja/make2.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download)
Sun Oct 26 15:24:43 2008 UTC (17 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +1 -1 lines
File MIME type: text/plain
make

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
13 my $all = get_all_entries ();
14 for my $key (keys %$all) {
15 for (keys %{$all->{$key}->{exact} or {}}) {
16 my $entry = $all->{$key}->{exact}->{$_};
17 $data{normalize ($entry->{en})} = [$entry->{ja}, $_];
18 }
19
20 for (keys %{$all->{$key}->{pattern} or {}}) {
21 my $entry = $all->{$key}->{pattern}->{$_};
22 $pattern{create_pattern1 (normalize ($entry->{en}))}
23 = [$entry->{ja}, $_];
24 }
25 }
26
27 my @pattern = sort {length $b <=> length $a} keys %pattern;
28
29 my $source_text;
30 {
31 warn "$source_file_name...\n";
32 open my $source_file, '<:utf8', $source_file_name
33 or die "$0: $source_file_name: $!";
34 local $/ = undef;
35 $source_text = <$source_file>;
36 }
37
38 my $part = 'cover';
39
40 my $status = {};
41 my $all_status = {};
42 my $tbt_added = {};
43
44 warn "Generating...\n";
45 $source_text =~ s{(<(?>p(?>re)?|li|d[td]|t[dh]|h[1-6]|caption)(?>\s[^>]*)?>)((?>(?!</?(?>p(?>re)?|li|d(?>[tdl]|iv)|t(?>[dr]|h(?>ead)?|able|body|foot)|h[1-6r]|ul|ol|caption)(?>\s[^>]*)?>).)+)}
46 {
47 my ($tag, $text) = ($1, $2);
48 my $prefix = '';
49 if ($text =~ s#^(<span class=secno>[^<>]+</span>)##) {
50 $prefix = $1;
51 }
52 my $n_text = normalize ($text);
53
54 if ($tag =~ /^<h2 id=(\w+)/) {
55 $part = $1;
56 }
57
58 if (length $n_text) {
59 my $ja_text = $data{$n_text};
60
61 $status->{all}++;
62 $all_status->{$part}->{all}++;
63
64 if (defined $ja_text) {
65 $status->{ja}++;
66 $all_status->{$part}->{ja}++;
67 add_class ($tag, 'has-ja-translation', $ja_text->[1]) .
68 q[<span class=en-original lang=en>] .
69 $prefix .
70 escape_id ($text, 'en-') . q[</span>] .
71 q[<span class=ja-translation lang=ja>] .
72 $prefix .
73 $ja_text->[0] . q[</span>];
74 } else {
75 my $v;
76 for my $pattern (@pattern) {
77 if ($n_text =~ /^$pattern$/) {
78 $status->{ja}++;
79 $all_status->{$part}->{ja}++;
80 my $real_hash = get_hash ($n_text);
81 $v = add_class ($tag, 'has-ja-translation',
82 $pattern{$pattern}->[1], $real_hash) .
83 q[<span class=en-original lang=en>] .
84 $prefix .
85 escape_id ($text, 'en-') . q[</span>] .
86 q[<span class=ja-translation lang=ja>] .
87 $prefix .
88 replace_pattern2 ($pattern{$pattern}->[0], $1, $2, $3, $4, $5) .
89 q[</span>];
90
91 unless ($tbt_added->{$n_text}) {
92 set_fallback_entry ($real_hash => {en => $text});
93 $tbt_added->{$n_text} = 1;
94 }
95 last;
96 }
97 }
98
99 unless (defined $v) {
100 my $hash = get_hash ($n_text);
101 $v = add_class ($tag, 'no-ja-translation', $hash) .
102 '<span class=en-original lang=en>' .
103 $prefix . $text .
104 '</span>';
105
106 unless ($tbt_added->{$n_text}) {
107 set_fallback_entry ($hash => {en => $text});
108 $tbt_added->{$n_text} = 1;
109 }
110 }
111
112 $v;
113 }
114 } else {
115 $1 . $2;
116 }
117 }ges;
118 $source_text =~ s{(<(?>link|img|script)\s[^>]+>)}{
119 my $tag = $1;
120 my $n_text = normalize ($tag);
121 my $ja_text = $data{$n_text};
122 if (defined $ja_text) {
123 $ja_text->[0];
124 } else {
125 $tag;
126 }
127 }ges;
128
129 $source_text =~ s{\[\[([A-Z ]+):([^]]+)\]\]}
130 {<em class=rfc2119 title="$1">$2</em>}gs;
131
132 #$source_text =~ s[<title>][<base href="http://www.whatwg.org/specs/web-apps/current-work/"><title>];
133
134 {
135 warn "$result_file_name...\n";
136 open my $result_file, '>:utf8', $result_file_name
137 or die "$0: $result_file_name: $!";
138 print $result_file $source_text;
139 }
140
141 {
142 my $time = time;
143 my @item = ($time, $status->{ja}, $status->{all});
144 for my $part (qw(cover introduction infrastructure dom semantics browsers
145 editing comms syntax rendering no)) {
146 push @item, $all_status->{$part}->{ja};
147 push @item, $all_status->{$part}->{all};
148 }
149
150 open my $status_file, '>>', $status_file_name
151 or die "$0: $status_file_name: $!";
152 print $status_file join "\t", @item;
153 print $status_file "\n";
154 close $status_file;
155 }
156
157 save_fallback_entries ();
158
159 sub add_class ($$$;$) {
160 my $tag = shift;
161 my $new_class = shift; # should not contain bare & and bare "
162 my $hash = shift;
163 my $real_hash = shift;
164 $real_hash = qq[ data-ja-real-hash="$real_hash"] if defined $real_hash;
165
166 if ($tag =~ /^<li\b/) {
167 ## NOTE: This |p| wrapper is necessary, otherwise, if |li| element
168 ## is set to |display: table|, then no list marker is shown.
169 $tag .= qq[<p class="$new_class ja-translation-inserted" data-ja-hash="$hash"$real_hash>];
170 } elsif ($tag =~ /\bclass="/) {
171 $tag =~ s/\bclass="([^"]*)"/class="$1 $new_class" data-ja-hash="$hash"$real_hash/;
172 } elsif ($tag =~ /\bclass=/) {
173 $tag =~ s/\bclass=([^\s>]+)/class="$1 $new_class" data-ja-hash="$hash"$real_hash/g;
174 } else {
175 $tag =~ s/>/ class="$new_class" data-ja-hash="$hash"$real_hash>/;
176 }
177
178 return $tag;
179 } # add_class
180
181 sub escape_id ($$) {
182 my $content = shift;
183 my $id_prefix = shift; # should not contain bare & and bare "
184
185 $content =~ s{<([a-zA-Z0-9-][^<>]+)>}{
186 my $tag_content = $1;
187
188 if ($tag_content =~ /\bid="/) {
189 $tag_content =~ s/\bid="([^"]*)"/id="$id_prefix$1"/;
190 } elsif ($tag_content =~ /\bid=/) {
191 $tag_content =~ s/\bid=(\S+)/id="$id_prefix$1"/;
192 }
193
194 # if ($tag_content =~ /\bhref=#/) {
195 # $tag_content =~ s/\bhref=#(\S+)/href=#$id_prefix$1/;
196 # }
197
198 '<' . $tag_content . '>';
199 }ge;
200
201 return $content;
202 } # escape_id

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24