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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download)
Sun Jul 6 11:12:48 2003 UTC (22 years ago) by w
Branch: MAIN
File MIME type: text/plain
Moved to here

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24