Debian Codegraph @

bin/html2perlstream

raw
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 html2perlstream - convert an HTML document to Perl code for that document
6
7 =head1 SYNOPSIS
8
9 html2perlstream
10 html2perlstream [options] -
11 html2perlstream [options] file.html .. file.html
12
13 =head1 DESCRIPTION
14
15 Takes an HTML file, and produces from it a Perl script that will
16 generate that HTML using the L<HTML::Stream> module.
17
18 For usage, just say:
19
20 html2perlstream
21
22 The normal form is:
23
24 html2perlstream [-options] [files]
25
26 Any named HTML input file F<file.html> will cause output script
27 F<file.html.pl> to be generated. To read from the standard input,
28 specify '-', like this:
29
30 html2perlstream - < test.html
31
32 If reading from the standard input, the output Perl code goes to the
33 standard output. So if you want to run it right away to see the output,
34 just do this:
35
36 html2perlstream - < test.html | perl
37
38 =head1 OPTIONS
39
40 =over 4
41
42 =item B<-d>
43
44 Automatically load and run the output Perl file, then do a diff on
45 that and the input file (you must input from a file and output to a
46 file in order to use this).
47
48 html2perlstream -w -d testin/test.html
49
50 You'll get fewer differences if you build the code with C<-w>.
51
52 =item B<-w>
53
54 Try to generate code which will output all whitespace between tags verbatim.
55 The default code only outputs whitespace if it is believed to be
56 needed; e.g., if we are inside a C<PRE> environment.
57
58 =back
59
60
61 =head1 REQUIRES
62
63 To run this, you need:
64
65 HTML::Entities
66 HTML::Parser
67
68
69 =head1 VERSION
70
71 $Id: html2perlstream,v 1.10 2001/08/17 00:50:00 eryq Exp $
72
73
74 =head1 AUTHOR
75
76 Eryq, 11 Jan 1997, F<[email protected]> or thereabouts.
77
78 Thanks (independently) to Tony Cebzanov and John Buckman for suggesting
79 that I write a tool like this.
80
81 =cut
82
83 # Try this:
84 #
85 # perl -I. bin/html2perlstream -w testin/test.html;
86 # perl -I. testin/test.html.pl > testin/test2.html
87 # diff testin/test.html testin/test2.html
88
89 use HTML::Parser;
90 use HTML::Entities;
91 use Getopt::Std;
92 use FileHandle;
93
94 use strict;
95 use vars qw($VERSION
96 $opt_d
97 $opt_w
98 );
99
100 #------------------------------------------------------------
101 #
102 # Globals...
103 #
104 #------------------------------------------------------------
105
106 # Version...
107 ( $VERSION ) = '$Revision: 1.10 $ ' =~ /\$Revision:\s+([^\s]+)/;
108
109 # Escape-map:
110 my %PerlUnescape = (
111 "\b"=>'b',
112 "\f"=>'f',
113 "\r"=>'r',
114 "\t"=>'t',
115 "\n"=>'n',
116 );
117 my $PerlEscapePat = join('',keys(%PerlUnescape));
118
119 #------------------------------------------------------------
120 # str2perl STRING,[QUOTECHAR]
121 #------------------------------------------------------------
122 # Convert a string to a double-quoted Perl string.
123 # Unsafe characters and non-printables are escaped.
124 #
125 # If QUOTECHAR is nonempty (default is '"'), it is escaped and also
126 # wrapped around the string.
127
128 sub str2perl {
129 my ($str, $qq) = @_;
130 defined($qq) or $qq = '"';
131
132 $str =~ s/[\\\$\@]/\\$&/g; # unsafe
133 $str =~ s/$qq/\\$qq/g if $qq; # quote char
134 $str =~ s/[$PerlEscapePat]/\\$PerlUnescape{$&}/og; # newlines, tabs...
135 $str =~ s/[\x00-\x1F\x7F-\xFF]/sprintf("\\x%02X", ord($&))/eg;
136 "$qq$str$qq";
137 }
138
139
140
141 #============================================================
142 package Main::Parser;
143 #============================================================
144
145 use HTML::Stream qw(:funcs);
146 use vars qw(@ISA);
147
148 @ISA = (qw(HTML::Parser));
149
150
151 # Are we inside a preformatter environment?
152 my $PREcount = 0;
153
154 #------------------------------------------------------------
155 # declaration
156 #------------------------------------------------------------
157 # This method is called when a markup declaration has been
158 # recognized. For typical HTML documents, the only declaration you are
159 # likely to find is <!DOCTYPE ...>. The initial ``<!'' and ending ``>''
160 # is not part of the string passed as argument. Comments
161 # are removed and entities have not been expanded yet.
162
163 sub declaration {
164 my ($self, $decl) = @_;
165
166 print '$HTML->io->print(', ::str2perl("<!$decl>"), ");\n";
167 }
168
169 #------------------------------------------------------------
170 # start
171 #------------------------------------------------------------
172 # This method is called when a complete start tag has been
173 # recognized. The first argument is the tag name (in lower case)
174 # and the second argument is a reference to a hash that contain
175 # all attributes found within the start tag. The attribute keys
176 # are converted to lower case. Entities found in the attribute
177 # values are already expanded.
178
179 sub start {
180 my ($self, $tag, $attr) = @_;
181
182 # Bookkeeping:
183 ++$PREcount if ($tag eq 'pre');
184
185 # Output:
186 if (keys %$attr) {
187
188 # Output and remember first line, up to open paren:
189 my $firstline = "\$HTML -> \U$tag\E(";
190 print $firstline;
191
192 # Determine nice indent:
193 my $indent = ' ' x length($firstline);
194 $indent =~ s/ {8}/\t/g;
195
196 # Output tag params:
197 my @keys = sort keys %$attr;
198 my $i;
199 for ($i = 0; $i < int(@keys); $i++) {
200 print "\U$keys[$i]\E => ";
201 print ::str2perl($attr->{$keys[$i]});
202 print ",\n$indent" unless ($i == (int(@keys)-1));
203 }
204 print ");\n";
205 }
206 else {
207 print "\$HTML -> \U$tag\E;\n";
208 }
209 }
210
211 #------------------------------------------------------------
212 # end
213 #------------------------------------------------------------
214 # This method is called when an end tag has been recognized.
215 # The argument is the lower case tag name.
216
217 sub end {
218 my ($self, $tag) = @_;
219 print "\$HTML -> _\U$tag\E;\n";
220
221 # Bookkeeping:
222 --$PREcount if ($tag eq 'pre');
223 }
224
225
226 #------------------------------------------------------------
227 # should_output_whitespace
228 #------------------------------------------------------------
229 sub should_output_whitespace {
230 $::opt_w || ($PREcount > 0);
231 }
232
233 #------------------------------------------------------------
234 # whitespace
235 #------------------------------------------------------------
236 sub whitespace {
237 my $ws = shift;
238
239 return if (!$ws);
240 if ($ws =~ /\A\n*\Z/) {
241 print "\$HTML -> nl";
242 print( (length($ws) > 1) ? '('.length($ws).')' : '');
243 print ";\n";
244 }
245 else {
246 print "\$HTML -> t(", ::str2perl($ws), ");\n";
247 }
248 }
249
250 #------------------------------------------------------------
251 # text
252 #------------------------------------------------------------
253 # This method is called as plain text in the document
254 # is recognized. The text is passed on unmodified and might contain
255 # multiple lines. Note that for efficiency reasons entities in the
256 # text are not expanded. You should call
257 # HTML::Entities::decode($text) before you process the text any
258 # further.
259
260 sub text {
261 my ($self, $text) = @_;
262
263 # Do nothing if empty string:
264 return if (!defined($text) or ($text eq ''));
265
266 # Unescape HTML:
267 $text = HTML::Entities::decode($text);
268
269 # Break into WHITE* NONWHITE* WHITE*
270 my ($pre, $in, $dummy, $post) = ($text =~ /\A(\s*)((.|\n)*?)(\s*)\Z/);
271
272 # Output leading whitespace:
273 if ($pre && should_output_whitespace()) {
274 whitespace($pre);
275 }
276
277 # Output main text:
278 if (defined($in) && ($in ne '')) {
279 if (length($in) < 70) { # output as t()
280 print "\$HTML -> t(", ::str2perl($in), ");\n";
281 }
282 else { # output as hereis()
283 my $hereis = ::str2perl($in, '');
284 $hereis =~ s/\\n/\n/g;
285
286 print "output \$HTML <<EOF;\n";
287 print $hereis;
288 print "\nEOF\n";
289 }
290 }
291
292 # Output trailing whitespace:
293 if ($post && should_output_whitespace()) {
294 whitespace($post);
295 }
296 1;
297 }
298
299 #------------------------------------------------------------
300 # comment
301 #------------------------------------------------------------
302 # This method is called as comments are recognized.
303 # The leading and trailing ``--'' sequences has been stripped off
304 # the comment text.
305
306 sub comment {
307 my ($self, $comment) = @_;
308
309 # Remove lead/trail single space (we'll put it back, honest):
310 $comment =~ s/^ //;
311 $comment =~ s/ $//;
312
313 print "\$HTML -> comment(", ::str2perl($comment), ");\n";
314 }
315
316
317 #============================================================
318 package main;
319 #============================================================
320
321 #------------------------------------------------------------
322 # print_header
323 #------------------------------------------------------------
324 sub print_header {
325 print <<EOF;
326 #!/usr/bin/perl -Tw
327
328 use HTML::Stream;
329 \$HTML = new HTML::Stream \\*STDOUT;
330 EOF
331
332 if ($opt_w) {
333 print <<EOF;
334
335 # You generated this code with "html2perlstream -w", so we turn off
336 # auto-formatting to make things look right:
337 \$HTML->auto_format(0);
338 EOF
339 }
340 else {
341 print <<EOF;
342
343 # You generated this code without "html2perlstream -w", so we keep
344 # auto-formatting on to add some whitespace here and there:
345 # \$HTML->auto_format(0);
346 EOF
347 }
348
349 print <<EOF;
350
351 # BEGIN GENERATED CODE
352 EOF
353 }
354
355 #------------------------------------------------------------
356 # print_fooer
357 #------------------------------------------------------------
358 sub print_footer {
359 print <<EOF;
360 # END GENERATED CODE
361 1;
362 EOF
363 }
364
365 #------------------------------------------------------------
366 # usage
367 #------------------------------------------------------------
368 sub usage {
369 print STDERR <<EOF;
370
371 html2perlstream version $VERSION by Eryq, Jan 1997, eryq\@zeegee.com.
372
373 Usage: html2perlstream [options] [files...]
374 -d automatically run Perl on output, and diff with input
375 -w output code to output whitespace verbatim
376
377 EOF
378 exit(-1);
379 }
380
381 #------------------------------------------------------------
382 # do_diff
383 #------------------------------------------------------------
384 sub do_diff {
385 my ($htmlfile, $perlfile) = @_;
386 my $html2 = "$perlfile.out";
387 my $diff = "$perlfile.diff";
388
389 # Sync up output:
390 STDOUT->flush;
391 STDERR->flush;
392
393 # Do it!
394 my $cmd = join(' ', ($^X, (map {"-I$_"} @INC), $perlfile));
395 # print STDERR "diff command = $cmd\n";
396 print STDERR " Running code: output in $html2\n";
397 system "$cmd > $html2";
398 print STDERR " Doing diff: output in $diff\n";
399 system "diff $htmlfile $html2 > $diff";
400 }
401
402 #------------------------------------------------------------
403 # main
404 #------------------------------------------------------------
405 sub main {
406 my $parser = new Main::Parser;
407
408 # Usage if no args:
409 @ARGV or usage();
410
411 # Get opts:
412 getopts "wd";
413
414 # Process files:
415 @ARGV or unshift @ARGV, '-';
416 my $infile;
417 while ($infile = shift @ARGV) {
418 my $outfile = (($infile eq '-') ? '-' : "$infile.pl");
419
420 # Trap for bad -d usage:
421 if ($opt_d and (($infile eq '-') || ($outfile eq '-'))) {
422 die "Cannot use -d if input is from STDIN\n";
423 }
424
425 # Report:
426 print STDERR "Converting ", ($infile eq '-' ? '<STDIN>' : $infile),
427 " to " , ($outfile eq '-' ? '<STDOUT>' : $outfile),
428 "\n";
429
430 # Open input and output:
431 open INPUT, "<$infile" or die "$infile: $!";
432 open OUTPUT, ">$outfile" or die "$outfile: $!";
433 select OUTPUT;
434
435 # Header:
436 print_header();
437 $parser->parse_file(\*INPUT);
438
439 # Footer:
440 print_footer();
441
442 # Close:
443 close OUTPUT;
444 close INPUT;
445
446 # Do a diff...
447 do_diff($infile, $outfile) if ($opt_d);
448
449 }
450 print STDERR "Done.\n";
451 1;
452 }
453 exit (&main ? 0 : -1);
454
455 #------------------------------------------------------------
456 1;