/[suikacvs]/www/mozilla/tool/replace.pl
Suika

Contents of /www/mozilla/tool/replace.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Sat Oct 25 12:00:20 2003 UTC (21 years, 9 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +6 -4 lines
File MIME type: text/plain
NL = LF environment support

1 w 1.1 #!/usr/bin/perl
2     use strict;
3     require Getopt::Long;
4     my %opt = (type => 'js');
5     Getopt::Long::GetOptions (
6     'input=s' => \$opt{input},
7     'output-type=s' => \$opt{type},
8     );
9    
10     my %var = (percent => '%');
11     my $name;
12     while (<>) {
13 wakaba 1.2 if (/^(.+?)(?:\[([^]]+)\])?:\s*$/) {
14 w 1.1 $name = $1;
15     $var{$name}->{type} = $2;
16 wakaba 1.2 } elsif (/^\t(.*)/) {
17     my $s = $1;
18     $s =~ tr/\x0D\x0A//d;
19     $s = replace_percent ($s, \%var);
20 w 1.1 if ($var{$name}->{type} eq 'list') {
21     push @{$var{$name}->{value}}, $s;
22     } else {
23     $var{$name}->{value} .= "\n" if defined $var{$name}->{value};
24     $var{$name}->{value} .= $s;
25     }
26     }
27     }
28    
29     open SRC, $opt{input} or die "$0: $opt{input}: $!";
30     print scalar commentize (qq(This file is auto-generated (at @{[
31     sprintf '%04d-%02d-%02dT%02d:%02d:%02dZ',
32     (gmtime)[5]+1900, (gmtime)[4]+1, (gmtime)[3,2,1,0]
33     ]}).\n)
34     .qq(Do not edit by hand!\n))
35     unless $opt{type} eq 'xml';
36     while (<SRC>) {
37     print scalar replace_percent ($_, \%var);
38     }
39     close SRC;
40    
41     exit;
42    
43     sub replace_percent ($$) {
44     my ($s, $l) = @_;
45     $s =~ s{%%([^%]+)%%}{
46     my ($r, $type) = _get_replacement_text ($1, $l);
47     if ($type eq 'list') {
48     $r = join (', ', map {qq("$_")} @$r);
49     }
50     $r;
51     }ge;
52     $s;
53     }
54    
55     sub _get_replacement_text ($$) {
56     my ($n, $l) = @_;
57     my ($rm, $type) = ($l->{$n}->{value}, $l->{$n}->{type});
58     unless (defined $rm) {
59     if ($n eq 'current-date-time') {
60     $rm = sprintf '%04d-%02d-%02dT%02d:%02d:%02dZ',
61     (gmtime)[5]+1900, (gmtime)[4]+1, (gmtime)[3,2,1,0]
62     }
63     }
64     ($rm, $type);
65     }
66    
67     sub commentize ($) {
68     my $s = shift;
69     if ($opt{type} eq 'js') {
70     $s =~ s!^! * !mg;
71     return "/*\n" . $s . " */\n";
72     } elsif ($opt{type} eq 'xml') {
73     $s =~ s!^! - !mg;
74     return "<!--\n" . $s . " -->\n";
75     } else {
76     $s =~ s!^!## !mg;
77     return $s."\n";
78     }
79     }
80    
81    
82     =head1 LICENSE
83    
84     Copyright 2003 Wakaba <w@suika.fam.cx>.
85    
86     This program is free software; you can redistribute it and/or modify
87     it under the terms of the GNU General Public License as published by
88     the Free Software Foundation; either version 2 of the License, or
89     (at your option) any later version.
90    
91     This program is distributed in the hope that it will be useful,
92     but WITHOUT ANY WARRANTY; without even the implied warranty of
93     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
94     GNU General Public License for more details.
95    
96     You should have received a copy of the GNU General Public License
97     along with this program; see the file COPYING. If not, write to
98     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
99     Boston, MA 02111-1307, USA.
100    
101     =cut
102    
103 wakaba 1.2 ## $Date: 2003/07/06 11:12:48 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24