blob: c3cb902074399707b350a5cb2bfd4f368985f48d [file] [log] [blame]
Wes Hardaker0ec088a2010-10-11 14:11:17 +00001#!/usr/bin/perl -w
2##############################################################################
3#
4# Alex Burger - Oct 28th, 2004
5#
6# Purpose: Modify .html files to add a header and footer for use
7# on the Net-SNMP web site.
8#
9# Can also be used to change the 'section' variable
10# for use in the menu system.
11#
12# Notes: A backup of each file is made to *.old.
13#
14# Any DOS newlines are removed from the destination file.
15#
16# Permissions are maintained.
17#
18##############################################################################
19#
20use File::Copy;
21use File::stat;
22use Getopt::Long;
23
24my $tidy_options = '-f /dev/null -m -i -asxhtml -wrap 130 -quiet --quote-nbsp n';
25
26my $pattern = '';
27my $section = '';
28my $tidy = 0;
29my $body = 0;
30my $help = 0;
31my @files = ();
32
33GetOptions ('pattern=s' => \$pattern,
34 'section=s' => \$section,
35 'tidy' => \$tidy,
36 'body' => \$body,
37 'help' => \$help);
38
39if ($help == 1)
40{
41$USAGE = qq/
42Usage:
43 add-header-footer [<options>] file1 file2 file3 ...
44Options:
45 --section= Menu section
46 --tidy Run tidy on input file before processing (turns on --body)
47 --body Remove everything before <body> and after <\/body>
48 --help Display this message
49
50Examples:
51
52 add-header-footer.pl --section=tutorial --body cat.html dog.html mouse.html
53
54 find . -name '*.html' | add-header-footer.pl --section=tutorial --body
55
56/;
57 print $USAGE;
58 exit 0;
59}
60
61if ($ARGV[0]) {
62 # Files listed on command line
63 foreach my $arg (@ARGV) {
64 chomp $arg;
65 push @files, $arg;
66 #print "$arg\n";
67 }
68}
69else {
70 # No arguments, so accept STDIN
71 while (<STDIN>) {
72 chomp;
73 push @files, $_;
74 #print "$_\n";
75 }
76}
77
78if (! (@files) ) {
79 exit 0;
80}
81
82#print "@files";
83
84foreach my $file (@files) {
85 chomp $file;
86 print "Processing file: $file\n";
87
88 # Grab current permissions
89 my $sb = stat($file);
90 my $stat_permissions = sprintf ("%04o", $sb->mode & 07777);
91 my $stat_uid = $sb->uid;
92 my $stat_gid = $sb->gid;
93
94 my @old_file = ();
95 my @new_file = ();
96
97 my $body_count = 0;
98
99 # Backup old file
100 if (! (copy ("$file", "$file.old"))) {
101 print "Could not backup existing file $file to $file.new. Aborting.\n";
102 next;
103 }
104 # Set permissions on old file to match original file
105 chmod oct($stat_permissions), "$file.old";
106 chown $stat_uid, $stat_uid, "$file.old";
107
108
109 if ($tidy == 1) {
110 $body = 1; # Enable body, as tidy will add it in.
111 my $tidy_command = "tidy $tidy_options $file";
112 `$tidy_command`;
113 }
114
115 if (open (I, "<$file")) {
116 # Load entire file
117 while (<I>) {
118 s/\015//g; # Remove any DOS newlines
119 chomp;
120 push (@old_file, $_);
121 }
122 }
123 else {
124 print "Could not open file $file. Aborting\n";
125 next;
126 }
127
128 if (!@old_file) {
129 print "Empty file. Skipping\n";
130 next;
131 }
132
133 # Remove empty lines at start
134 while (1) {
135 if ($old_file[0] eq "") {
136 splice (@old_file, 0, 1);
137 }
138 else {
139 last;
140 }
141 }
142
143 # Remove empty lines at end
144 while (1) {
145 if ($old_file[$#old_file] eq "") {
146 splice (@old_file, -1, 1);
147 }
148 else {
149 last;
150 }
151 }
152
153 if ($body == 1) {
154 # Count the number of <body lines
155 for (my $i = 0; $i <= $#old_file; $i++) {
156 if ($old_file[$i] =~ /<body/) {
157 $body_count++;
158 next;
159 }
160 }
161
162 # Remove anything before and including <body
163 while ($body_count > 0) {
164 while (! ($old_file[0] =~ /<body/)) {
165 splice (@old_file, 0, 1);
166 }
167 splice (@old_file, 0, 1); # <body line
168 $body_count--;
169 }
170 }
171
172 # Start to build new file in memory with header
173 push (@new_file, "<!--#set var=\"section\" value=\"$section\" -->\n");
174 push (@new_file, '<!--#include virtual="/page-top.html" -->' . "\n");
175 push (@new_file, '<!-- CONTENT START -->' . "\n");
176
177 # Add in old file, skipping existing header and footer and stopping at <body/>
178 for (my $i = 0; $i <= $#old_file; $i++) {
179 if (!(defined($old_file[$i]))) { next; }
180 if ($body == 1 && ($old_file[$i] =~ /<\/body>/)) { last; }
181 elsif ($old_file[$i] =~ /<!--#set var="section" value=/) { next; }
182 elsif ($old_file[$i] =~ /<!--#include virtual="\/page-top.html" -->/) { next; }
183 elsif ($old_file[$i] =~ /<!-- CONTENT START -->/) { next; }
184 elsif ($old_file[$i] =~ /<!-- CONTENT END -->/) { next; }
185 elsif ($old_file[$i] =~ /<!--#include virtual="\/page-bottom.html" -->/) { next; }
186
187 push (@new_file, $old_file[$i] . "\n");
188 }
189
190 # Finish to building new file in memory with footer
191 push (@new_file, '<!-- CONTENT END -->' . "\n");
192 push (@new_file, '<!--#include virtual="/page-bottom.html" -->' . "\n");
193
194 # Save new file
195 if (open (O, ">$file")) {
196 for (my $i = 0; $i <= $#new_file; $i++) {
197 print O "$new_file[$i]";
198 }
199 print O "\n";
200 close O;
201
202 # Set permissions
203 chmod oct($stat_permissions), $file;
204 chown $stat_uid, $stat_uid, $file;
205 }
206 else {
207 print "Could not create new file: $file.new\n"
208 }
209 close I;
210}
211
212