/[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.7 by wakaba, Thu Oct 23 06:12:18 2008 UTC revision 1.14 by wakaba, Mon Oct 27 04:52:38 2008 UTC
# Line 2  use strict; Line 2  use strict;
2    
3  my $data_suffix = q[.dat];  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/];
6    my $data2_suffix = q[.dat];
7    my $lock_suffix = q[.lock];
8    
9    ## SEE ALSO: |Makefile|.
10    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;
16    
17  sub normalize ($) {  sub normalize ($) {
18    my $s = shift;    my $s = shift;
# Line 11  sub normalize ($) { Line 22  sub normalize ($) {
22    return $s;    return $s;
23  } # normalize  } # normalize
24    
25    sub get_hash ($) {
26      require Digest::MD5;
27      require Encode;
28      return Digest::MD5::md5_hex (Encode::encode ('utf8', normalize ($_[0])));
29    } # get_hash
30    
31  sub create_pattern1 ($) {  sub create_pattern1 ($) {
32    my $s = quotemeta shift;    my $s = quotemeta shift;
33    $s =~ s/\\\*/(.+)/g;    $s =~ s/\\\*/(.+)/g;
# Line 55  sub for_each_data_file ($) { Line 72  sub for_each_data_file ($) {
72    }    }
73  } # for_each_data_file  } # for_each_data_file
74    
75    sub read_data_file ($) {
76      my $file_name = shift;
77      if (-f $file_name) {
78        warn "Loading $file_name...\n";
79        return do $file_name;
80      } else {
81        warn "File $file_name not found\n";
82        return {};
83      }
84    } # read_data_file
85    
86    sub write_data_file ($$) {
87      my ($file_name, $data) = @_;
88    
89      require Data::Dumper;
90      local $Data::Dumper::Sortkeys = 1;
91    
92      my $had_file = -f $file_name;
93      open my $file, '>:encoding(utf8)', $file_name or die "$0: $file_name: $!";
94      print $file Data::Dumper::Dumper ($data);
95      close $file;
96      unless ($had_file) {
97        system_ ('cvs', 'add', $file_name) if $UseCVS;
98      }
99    } # write_data_file
100    
101    sub hash_to_file_name ($) {
102      return $data2_dir_name . substr ($_[0], 0, 2) . $data2_suffix;
103    } # hash_to_file_name
104    
105    my $Entry = {};
106    my $ModifiedHash = {};
107    
108    sub get_entry ($) {
109      my $hash = shift;
110      
111      my $file_name = hash_to_file_name ($hash);
112      unless ($Entry->{$file_name}) {
113        $Entry->{$file_name} = read_data_file ($file_name);
114      }
115      
116      if ($Entry->{$file_name}->{exact}->{$hash}) {
117        return (0, $Entry->{$file_name}->{exact}->{$hash});
118      } elsif ($Entry->{$file_name}->{pattern}->{$hash}) {
119        return (1, $Entry->{$file_name}->{pattern}->{$hash});
120      } else {
121        return (undef, undef);
122      }
123    } # get_entry
124    
125    sub set_entry ($$$) {
126      my ($hash, $is_pattern, $value) = @_;
127      
128      my $file_name = hash_to_file_name ($hash);
129      unless ($Entry->{$file_name}) {
130        $Entry->{$file_name} = read_data_file ($file_name);    
131      }
132    
133      unless ($value) {
134        delete $Entry->{$file_name}->{exact}->{$hash};
135        delete $Entry->{$file_name}->{pattern}->{$hash};
136      } elsif ($is_pattern) {
137        delete $Entry->{$file_name}->{exact}->{$hash};
138        $Entry->{$file_name}->{pattern}->{$hash} = $value;
139      } else {
140        $Entry->{$file_name}->{exact}->{$hash} = $value;
141        delete $Entry->{$file_name}->{pattern}->{$hash};
142      }
143      $Entry->{$file_name}->{modified} = 1;
144      $ModifiedHash->{$hash} = 1;
145    } # set_entry
146    
147    use Fcntl ':flock';
148    my $Lock;
149    
150    sub lock_entry ($) {
151      if ($Lock) {
152        die "$0: lock_entry: Another entry is locked";
153      }
154    
155      my $hash = shift;
156      my $file_name = hash_to_file_name ($hash) . $lock_suffix;
157      open $Lock, '>', $file_name or die "$0: $file_name: $!";
158      flock $Lock, LOCK_EX;
159    } # lock_entry
160    
161    sub commit_entries ($) {
162      for my $file_name (keys %{$Entry}) {
163        if ($Entry->{$file_name}->{modified}) {
164          delete $Entry->{$file_name}->{modified};
165          write_data_file ($file_name => $Entry->{$file_name});
166        }
167      }
168    
169      open my $file, '>>', $patch_file_name or die "$0: $patch_file_name: $!";
170      for (keys %$ModifiedHash) {
171        print $file "$_\n";
172      }
173      close $file;
174    
175      my $msg = shift // $0;
176      system_ ('cvs', 'commit', -m => $msg, $data2_dir_name) if $UseCVS;
177    } # commit_entries
178    
179    sub get_all_entries () {
180      opendir my $dir, $data2_dir_name or die "$0: $data2_dir_name: $!";
181      for (readdir $dir) {
182        next unless /\Q$data2_suffix\E$/;
183        my $file_name = $data2_dir_name . $_;
184        next if $Entry->{$file_name};
185    
186        $Entry->{$file_name} = read_data_file ($file_name);
187      }
188    
189      return $Entry;
190    } # get_all_entries
191    
192    sub for_each_entry_set ($;$) {
193      my $code = shift;
194      my $on_the_fly = shift;
195      
196      opendir my $dir, $data2_dir_name or die "$0: $data2_dir_name: $!";
197      for (readdir $dir) {
198        next unless /\Q$data2_suffix\E$/;
199        my $file_name = $data2_dir_name . $_;
200        next if $file_name eq $fallback_file_name;
201    
202        if ($Entry->{$file_name}) {
203          $code->($file_name, $Entry->{$file_name});
204        } elsif ($on_the_fly) {
205          $code->($file_name, read_data_file ($file_name));
206        } else {
207          $Entry->{$file_name} = read_data_file ($file_name);
208          $code->($file_name, $Entry->{$file_name});
209        }
210      }
211    } # for_each_entry_set
212    
213    my $FallbackEntry;
214    sub get_fallback_entry ($) {
215      my $hash = shift;
216      unless (defined $FallbackEntry) {
217        $FallbackEntry = read_data_file ($fallback_file_name);
218      }
219      return $FallbackEntry->{$hash} // {};
220    } # get_fallback_entry
221    
222    sub get_entry_or_fallback_entry ($) {
223      my $hash = shift;
224    
225      my ($is_pattern, $entry) = get_entry ($hash);
226      unless (defined $entry->{en}) {
227        $entry = get_fallback_entry ($hash);
228      }
229      $entry->{tags} ||= [];
230      $entry->{isPattern} = 1 if $is_pattern;
231    
232      return $entry;
233    } # get_entry_or_fallback_entry
234    
235    sub set_fallback_entry ($$) {
236      my ($hash, $value) = @_;
237      unless (defined $FallbackEntry) {
238        $FallbackEntry = read_data_file ($fallback_file_name);
239      }
240      $FallbackEntry->{$hash} = $value;
241    } # set_fallback_entry
242    
243    sub get_fallback_entries () {
244      unless (defined $FallbackEntry) {
245        $FallbackEntry = read_data_file ($fallback_file_name);
246      }
247      
248      return $FallbackEntry;
249    } # get_fallback_entries
250    
251    sub clear_fallback_entries () {
252      $FallbackEntry = {};
253    } # clear_fallback_entries
254    
255    sub save_fallback_entries () {
256      write_data_file ($fallback_file_name => $FallbackEntry)
257          if defined $FallbackEntry;
258    } # save_fallback_entries
259    
260    sub get_modified_hashes () {
261      open my $file, '<', $patch_file_name or die "$0: $patch_file_name: $!";
262      return map {tr/\x0D\x0A//d; $_} <$file>;
263    } # get_modified_hashes
264    
265    sub clear_modified_hashes () {
266      open my $file, '>', $patch_file_name;
267      close $file;
268    } # clear_modified_hashes
269    
270    sub htescape ($) {
271      my $s = shift;
272      $s =~ s/&/&amp;/g;
273      $s =~ s/</&lt;/g;
274      $s =~ s/"/&quot;/g;
275      return $s;
276    } # htescape
277    
278    sub system_ (@) {
279      (system join (' ', map {quotemeta $_} @_) . " > /dev/null") == 0
280          or die "$0: $?";
281    } # system_
282    
283  1;  1;
284    

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.14

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24