/[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.2 - (hide annotations) (download)
Sun Oct 26 08:44:19 2008 UTC (17 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +20 -12 lines
File MIME type: text/plain
Dynamic update implemented

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24