]> pere.pagekite.me Git - homepage.git/blob - linux/html-search-replace
Generated.
[homepage.git] / linux / html-search-replace
1 #!/store/bin/perl
2 #
3 # Search and replace params in HTML tags. Change all lines to end with \r\n
4 #
5 # Author: Petter Reinholdtsen <pere@td.org.uit.no>
6 # Date: 1997-05-27
7 # Changed:1997-05-27
8
9 #$debug = 1;
10
11 require "find.pl";
12 use Getopt::Std;
13
14 sub usage {
15 print "$0 [-c <configfile>|'<search-replace command>'] <paths>\n";
16 print " -c Change default config-file\n\n";
17 print "Command format: <TAG>.<PARAM> {<from>-><to>|-}\n";
18 print " If <from> is '*', the param will be inserted where necessary.\n";
19 print " If command is '-', the param is removed.\n\n";
20 }
21
22 if (! getopts('c:') || ! @ARGV) {
23 usage();
24 exit 1;
25 }
26
27 if ($opt_c) {
28 open(CFILE, "<$opt_c") || die "Unable to open $opt_c for reading";
29 while (<CFILE>) {
30 next if (/^\#/);
31 push(@commands,msdosChompLine($_));
32 }
33 close(CFILE);
34 } else {
35 @commands = (shift(@ARGV));
36 }
37
38 umask 002;
39
40 &find(@ARGV);
41
42 sub wanted {
43 return if ( ! /\.s?html?$/i || ! -r "$dir/$_" );
44 $filename = "$dir/$_";
45 print "Prosessing $filename\n";
46 $oldtime = (stat("$filename"))[10]; # save change-time to compare before saveing
47 if (! open(FILE, "<$filename") ) {
48 warn "Unable to open $filename for reading. Nothing changed.";
49 return;
50 }
51 foreach $line (<FILE>) {
52 push(@html, $line);
53 $line = msdosChompLine($line);
54 foreach $command (@commands) {
55 local($tag, $param, $cmd) = $command =~ m/^(\w+)\.(\w+) (.+)$/;
56 if ( $line =~ /\<$tag[^\>]*\>/i ) {
57 print "L: \"$line\"\nC: $tag $param $cmd\n" if ($debug);
58 if ($cmd eq "-") { # Remove the param
59 $line =~ s/(\<$tag[^\>]*) $param\s*=\s*\"?[^\" ]+\"?([ >])/\1\2/i;
60 } elsif ($cmd =~ /\-\>/) { # change param
61 ($from, $to) = $cmd =~ m/^(.+)-\>(.+)$/;
62 print "$tag.$param $from -> $to\n" if ($debug);
63
64 if ( $line =~
65 m/^(.*\<$tag[^\>]* )($param\s*=\s*\"?)([^\" >]+)(\"?.*)$/i ) {
66 print "Param hit: \"$3\"\n" if ($debug);
67 # param exist - change it
68 if ($from eq '*' || $from eq $3) {
69 $line = "$1\U$2"."$to$4";
70 print "Replaced\n" if ($debug);
71 }
72 } elsif ($from eq '*') {
73 # new param in tag - insert it
74 $line =~ m/^(.*\<$tag[^\>]+)(\>.*)$/i;
75 $line = "$1 \U$param=\""."$to\"$2";
76 print "Inserted\n" if ($debug);
77 } # else nothing is done
78 }
79 # Pack and remove redundand space inside tags
80 $line =~ s/(\<[^\>]+) \s+([^\>]*>)/\1 \2/;
81 $line =~ s/\s+\>/\>/;
82 print "L2: \"$line\"\n" if ($debug);
83 }
84 }
85 push(@newhtml, "$line\r\n");
86 }
87 close(FILE);
88
89 # Save backup and replace
90 if ($oldtime == (stat("$filename"))[10]) { # Not changed while we prossessed
91 print @newhtml if ($debug);
92 local($backupname) = "$filename.backup";##-$$";
93 if (open(BACKUP, ">$backupname") ) {
94 if (print BACKUP @html) { # backup saved - replace original
95 close(BACKUP);
96 if ( open(FILE, ">$filename") ) {
97 if (print FILE @newhtml) { # Original replaced - all OK
98 close(FILE);
99 } else {
100 close(FILE);
101 warn "Unable to write to $filename. Probably damage - check backup $backupname."
102 }
103 } else {
104 warn "Unable to open $filename for writeing. Possible damage! - check backup $backupname";
105 }
106 } else {
107 close(BACKUP);
108 unlink $backupname;
109 warn "Unable to save backup for $filename. Nothing changed.";
110 }
111 } else {
112 warn "Unable to open backupfile $filename for writeing. Nothing changed.";
113 }
114 }
115 }
116
117 sub msdosChompLine {
118 local($line) = @_;
119 local($tmp) = $/;
120 $/ = "\n";
121 chomp($line);
122 $/ = "\r";
123 chomp($line);
124 $/ = $tmp;
125 return $line;
126 }