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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.13

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24