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

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24