/[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.1 - (hide annotations) (download)
Sun Oct 26 06:50:10 2008 UTC (17 years, 3 months ago) by wakaba
Branch: MAIN
File MIME type: text/plain
New version of editing interface/database, first revision

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     $v = add_class ($tag, 'has-ja-translation',
82     $pattern{$pattern}->[1]) .
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     last;
91     }
92     }
93    
94     unless (defined $v) {
95     my $hash = get_hash ($n_text);
96     $v = add_class ($tag, 'no-ja-translation', $hash) . $prefix . $text;
97    
98     $text =~ s/^\s+//;
99     $text =~ s/\s+\z//;
100     $text =~ s/\x0D?\x0A(?:\x0D?\x0A)+/\n/g;
101     unless ($tbt_added->{$text}) {
102     set_fallback_entry ($hash => {en => $text});
103     $tbt_added->{$text} = 1;
104     }
105     }
106    
107     $v;
108     }
109     } else {
110     $1 . $2;
111     }
112     }ges;
113     $source_text =~ s{(<(?>link|img|script)\s[^>]+>)}{
114     my $tag = $1;
115     my $n_text = normalize ($tag);
116     my $ja_text = $data{$n_text};
117     if (defined $ja_text) {
118     $ja_text->[0];
119     } else {
120     $tag;
121     }
122     }ges;
123    
124     $source_text =~ s{\[\[([A-Z ]+):([^]]+)\]\]}
125     {<em class=rfc2119 title="$1">$2</em>}gs;
126    
127     #$source_text =~ s[<title>][<base href="http://www.whatwg.org/specs/web-apps/current-work/"><title>];
128    
129     {
130     warn "$result_file_name...\n";
131     open my $result_file, '>:utf8', $result_file_name
132     or die "$0: $result_file_name: $!";
133     print $result_file $source_text;
134     }
135    
136     {
137     my $time = time;
138     my @item = ($time, $status->{ja}, $status->{all});
139     for my $part (qw(cover introduction infrastructure dom semantics browsers
140     editing comms syntax rendering no)) {
141     push @item, $all_status->{$part}->{ja};
142     push @item, $all_status->{$part}->{all};
143     }
144    
145     open my $status_file, '>>', $status_file_name
146     or die "$0: $status_file_name: $!";
147     print $status_file join "\t", @item;
148     print $status_file "\n";
149     close $status_file;
150     }
151    
152     save_fallback_entries ();
153    
154     sub add_class ($$$) {
155     my $tag = shift;
156     my $new_class = shift; # should not contain bare & and bare "
157     my $hash = shift;
158    
159     if ($tag =~ /^<li\b/) {
160     ## NOTE: This |p| wrapper is necessary, otherwise, if |li| element
161     ## is set to |display: table|, then no list marker is shown.
162     $tag .= qq[<p class="$new_class ja-translation-inserted" data-ja-hash="$hash">];
163     } elsif ($tag =~ /\bclass="/) {
164     $tag =~ s/\bclass="([^"]*)"/class="$1 $new_class" data-ja-hash="$hash"/;
165     } elsif ($tag =~ /\bclass=/) {
166     $tag =~ s/\bclass=([^\s>]+)/class="$1 $new_class" data-ja-hash="$hash"/g;
167     } else {
168     $tag =~ s/>/ class="$new_class" data-ja-hash="$hash">/;
169     }
170    
171     return $tag;
172     } # add_class
173    
174     sub escape_id ($$) {
175     my $content = shift;
176     my $id_prefix = shift; # should not contain bare & and bare "
177    
178     $content =~ s{<([a-zA-Z0-9-][^<>]+)>}{
179     my $tag_content = $1;
180    
181     if ($tag_content =~ /\bid="/) {
182     $tag_content =~ s/\bid="([^"]*)"/id="$id_prefix$1"/;
183     } elsif ($tag_content =~ /\bid=/) {
184     $tag_content =~ s/\bid=(\S+)/id="$id_prefix$1"/;
185     }
186    
187     # if ($tag_content =~ /\bhref=#/) {
188     # $tag_content =~ s/\bhref=#(\S+)/href=#$id_prefix$1/;
189     # }
190    
191     '<' . $tag_content . '>';
192     }ge;
193    
194     return $content;
195     } # escape_id

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24