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

Diff of /markup/html/html5/spec-ja/common.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9 by apache, Sun Oct 26 07:03:10 2008 UTC revision 1.19 by wakaba, Wed Nov 5 01:29:08 2008 UTC
# Line 1  Line 1 
1  use strict;  use strict;
2    use utf8;
3    
 my $data_suffix = q[.dat];  
4  my $data_dir_name = q[data/];  my $data_dir_name = q[data/];
5  my $data2_dir_name = q[data2/];  my $data2_dir_name = q[data2/];
6  my $data2_suffix = q[.dat];  my $data2_suffix = q[.dat];
7  my $lock_suffix = q[.lock];  my $lock_suffix = q[.lock];
8    
9    ## SEE ALSO: |Makefile|.
10  my $fallback_file_name = $data2_dir_name . 'fallback' . $data2_suffix;  my $fallback_file_name = $data2_dir_name . 'fallback' . $data2_suffix;
11    
12    ## SEE ALSO: |Makefile|.
13    my $patch_file_name = $data2_dir_name . 'modified.txt';
14    
15  our $UseCVS //= 1;  our $UseCVS //= 1;
16    
17  sub normalize ($) {  sub normalize ($) {
# Line 19  sub normalize ($) { Line 24  sub normalize ($) {
24    
25  sub get_hash ($) {  sub get_hash ($) {
26    require Digest::MD5;    require Digest::MD5;
27    return Digest::MD5::md5_hex (normalize ($_[0]));    require Encode;
28      return Digest::MD5::md5_hex (Encode::encode ('utf8', normalize ($_[0])));
29  } # get_hash  } # get_hash
30    
31  sub create_pattern1 ($) {  sub create_pattern1 ($) {
# Line 35  sub replace_pattern2 ($@) { Line 41  sub replace_pattern2 ($@) {
41    return $s;    return $s;
42  } # replace_pattern2  } # replace_pattern2
43    
 sub load_data_file ($$$) {  
   my ($data_file_name, $exact_data, $pattern_data) = @_;  
   
   open my $data_file, '<:utf8', $data_file_name  
       or die "$0: $data_file_name: $!";  
   local $/ = undef;  
   my $data = <$data_file>;  
   $data =~ s/\x0D?\x0A/\n/g;  
   for (split /\n\n+(?=#)/, $data) {  
     my ($en, $ja) = split /\n#ja\n/, $_;  
     if ($en =~ s/^#en\n//) {  
       $exact_data->{normalize ($en)} = $ja;  
     } elsif ($en =~ s/^#pattern\n//) {  
       $pattern_data->{create_pattern1 (normalize ($en))} = $ja;  
     }  
   }  
 } # load_data_file  
   
 sub for_each_data_file ($) {  
   my ($code) = @_;  
   
   opendir my $data_dir, $data_dir_name or die "$0: $data_dir_name: $!";  
   for (sort {$a cmp $b} readdir $data_dir) {  
     next if /^\./; # hidden files  
     next if /^_/; # editable but not-used files  
     my $data_file_name = qq[$data_dir_name$_];  
     next unless $data_file_name =~ /\Q$data_suffix\E$/;  
     $code->($data_file_name, $_);  
   }  
 } # for_each_data_file  
   
44  sub read_data_file ($) {  sub read_data_file ($) {
45    my $file_name = shift;    my $file_name = shift;
46    if (-f $file_name) {    if (-f $file_name) {
# Line 82  sub write_data_file ($$) { Line 57  sub write_data_file ($$) {
57    
58    require Data::Dumper;    require Data::Dumper;
59    local $Data::Dumper::Sortkeys = 1;    local $Data::Dumper::Sortkeys = 1;
60      local $Data::Dumper::Useqq = 1;
61      local *Data::Dumper::qquote = sub {
62        my $s = shift;
63        $s =~ s/([\x24\x27\x40\x5C])/sprintf '\x%02X', ord $1/ge;
64        return q<qq'> . $s . q<'>;
65      }; # Data::Dumper::qquote
66      
67    my $had_file = -f $file_name;    my $had_file = -f $file_name;
68    open my $file, '>:encoding(utf8)', $file_name or die "$0: $file_name: $!";    open my $file, '>:encoding(utf8)', $file_name or die "$0: $file_name: $!";
69      print $file "use utf8;\n";
70    print $file Data::Dumper::Dumper ($data);    print $file Data::Dumper::Dumper ($data);
71    close $file;    close $file;
72    unless ($had_file) {    unless ($had_file) {
# Line 97  sub hash_to_file_name ($) { Line 79  sub hash_to_file_name ($) {
79  } # hash_to_file_name  } # hash_to_file_name
80    
81  my $Entry = {};  my $Entry = {};
82    my $ModifiedHash = {};
83    
84  sub get_entry ($) {  sub get_entry ($) {
85    my $hash = shift;    my $hash = shift;
# Line 134  sub set_entry ($$$) { Line 117  sub set_entry ($$$) {
117      delete $Entry->{$file_name}->{pattern}->{$hash};      delete $Entry->{$file_name}->{pattern}->{$hash};
118    }    }
119    $Entry->{$file_name}->{modified} = 1;    $Entry->{$file_name}->{modified} = 1;
120      $ModifiedHash->{$hash} = 1;
121  } # set_entry  } # set_entry
122    
123  use Fcntl ':flock';  use Fcntl ':flock';
# Line 153  sub lock_entry ($) { Line 137  sub lock_entry ($) {
137  sub commit_entries ($) {  sub commit_entries ($) {
138    for my $file_name (keys %{$Entry}) {    for my $file_name (keys %{$Entry}) {
139      if ($Entry->{$file_name}->{modified}) {      if ($Entry->{$file_name}->{modified}) {
140          delete $Entry->{$file_name}->{modified};
141        write_data_file ($file_name => $Entry->{$file_name});        write_data_file ($file_name => $Entry->{$file_name});
142      }      }
143    }    }
144    
145      open my $file, '>>', $patch_file_name or die "$0: $patch_file_name: $!";
146      for (keys %$ModifiedHash) {
147        print $file "$_\n";
148      }
149      close $file;
150    
151    my $msg = shift // $0;    my $msg = shift // $0;
152    system_ ('cvs', 'commit', -m => $msg) if $UseCVS;    system_ ('cvs', 'commit', -m => $msg, $data2_dir_name) if $UseCVS;
153  } # commit_entries  } # commit_entries
154    
155  sub get_all_entries () {  sub get_all_entries () {
# Line 174  sub get_all_entries () { Line 165  sub get_all_entries () {
165    return $Entry;    return $Entry;
166  } # get_all_entries  } # get_all_entries
167    
168    sub for_each_entry_set ($;$) {
169      my $code = shift;
170      my $on_the_fly = shift;
171      
172      opendir my $dir, $data2_dir_name or die "$0: $data2_dir_name: $!";
173      for (readdir $dir) {
174        next unless /\Q$data2_suffix\E$/;
175        my $file_name = $data2_dir_name . $_;
176        next if $file_name eq $fallback_file_name;
177    
178        if ($Entry->{$file_name}) {
179          $code->($file_name, $Entry->{$file_name});
180        } elsif ($on_the_fly) {
181          $code->($file_name, read_data_file ($file_name));
182        } else {
183          $Entry->{$file_name} = read_data_file ($file_name);
184          $code->($file_name, $Entry->{$file_name});
185        }
186      }
187    } # for_each_entry_set
188    
189  my $FallbackEntry;  my $FallbackEntry;
190  sub get_fallback_entry ($) {  sub get_fallback_entry ($) {
191    my $hash = shift;    my $hash = shift;
# Line 183  sub get_fallback_entry ($) { Line 195  sub get_fallback_entry ($) {
195    return $FallbackEntry->{$hash} // {};    return $FallbackEntry->{$hash} // {};
196  } # get_fallback_entry  } # get_fallback_entry
197    
198    sub get_entry_or_fallback_entry ($) {
199      my $hash = shift;
200    
201      my ($is_pattern, $entry) = get_entry ($hash);
202      unless (defined $entry->{en}) {
203        $entry = get_fallback_entry ($hash);
204      }
205      $entry->{tags} ||= [];
206      $entry->{isPattern} = 1 if $is_pattern;
207    
208      return $entry;
209    } # get_entry_or_fallback_entry
210    
211  sub set_fallback_entry ($$) {  sub set_fallback_entry ($$) {
212    my ($hash, $value) = @_;    my ($hash, $value) = @_;
213    unless (defined $FallbackEntry) {    unless (defined $FallbackEntry) {
# Line 191  sub set_fallback_entry ($$) { Line 216  sub set_fallback_entry ($$) {
216    $FallbackEntry->{$hash} = $value;    $FallbackEntry->{$hash} = $value;
217  } # set_fallback_entry  } # set_fallback_entry
218    
219  sub clear_fallback_entries () {  sub get_fallback_entries () {
220    $FallbackEntry = {};    unless (defined $FallbackEntry) {
221  } # clear_fallback_entries      $FallbackEntry = read_data_file ($fallback_file_name);
222      }
223      
224      return $FallbackEntry;
225    } # get_fallback_entries
226    
227  sub save_fallback_entries () {  sub save_fallback_entries () {
228    write_data_file ($fallback_file_name => $FallbackEntry)    write_data_file ($fallback_file_name => $FallbackEntry)
229        if defined $FallbackEntry;        if defined $FallbackEntry;
230  } # save_fallback_entries  } # save_fallback_entries
231    
232    sub get_modified_hashes () {
233      open my $file, '<', $patch_file_name or die "$0: $patch_file_name: $!";
234      return map {tr/\x0D\x0A//d; $_} <$file>;
235    } # get_modified_hashes
236    
237    sub normalize_width ($) {
238      my $s = shift;
239      $s =~ tr{\x{3000}\x{FF01}-\x{FF5E}\x{FF61}-\x{FF9F}\x{FFE0}-\x{FFE6}}
240              { !-~。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン\x{3099}\x{309A}\xA2\xA3\xAC\xAF\xA6\xA5\x{20A9}};
241      return $s;
242    } # normalize_width
243    
244  sub htescape ($) {  sub htescape ($) {
245    my $s = shift;    my $s = shift;
246    $s =~ s/&/&amp;/g;    $s =~ s/&/&amp;/g;
# Line 209  sub htescape ($) { Line 250  sub htescape ($) {
250  } # htescape  } # htescape
251    
252  sub system_ (@) {  sub system_ (@) {
253    (system @_) == 0 or die "$0: $?";    (system join (' ', map {quotemeta $_} @_) . " > /dev/null") == 0
254          or die "$0: $?";
255  } # system_  } # system_
256    
257  1;  1;
258    
259    ## Author: Wakaba <w@suika.fam.cx>.
260    ## License: Copyright 2008 Wakaba.  You are granted a license to use,
261    ##     reproduce and create derivative works of this script.
262    ## $Date$

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.19

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24