to be generated. To read from the standard input,
specify '-', like this:
html2perlstream - < test.html
If reading from the standard input, the output Perl code goes to the
standard output. So if you want to run it right away to see the output,
just do this:
html2perlstream - < test.html | perl
=head1 OPTIONS
=over 4
=item B<-d>
Automatically load and run the output Perl file, then do a diff on
that and the input file (you must input from a file and output to a
file in order to use this).
html2perlstream -w -d testin/test.html
You'll get fewer differences if you build the code with C<-w>.
=item B<-w>
Try to generate code which will output all whitespace between tags verbatim.
The default code only outputs whitespace if it is believed to be
needed; e.g., if we are inside a C environment.
=back
=head1 REQUIRES
To run this, you need:
HTML::Entities
HTML::Parser
=head1 VERSION
$Id: html2perlstream,v 1.10 2001/08/17 00:50:00 eryq Exp $
=head1 AUTHOR
Eryq, 11 Jan 1997, F or thereabouts.
Thanks (independently) to Tony Cebzanov and John Buckman for suggesting
that I write a tool like this.
=cut
# Try this:
#
# perl -I. bin/html2perlstream -w testin/test.html;
# perl -I. testin/test.html.pl > testin/test2.html
# diff testin/test.html testin/test2.html
use HTML::Parser;
use HTML::Entities;
use Getopt::Std;
use FileHandle;
use strict;
use vars qw($VERSION
$opt_d
$opt_w
);
#------------------------------------------------------------
#
# Globals...
#
#------------------------------------------------------------
# Version...
( $VERSION ) = '$Revision: 1.10 $ ' =~ /\$Revision:\s+([^\s]+)/;
# Escape-map:
my %PerlUnescape = (
"\b"=>'b',
"\f"=>'f',
"\r"=>'r',
"\t"=>'t',
"\n"=>'n',
);
my $PerlEscapePat = join('',keys(%PerlUnescape));
#------------------------------------------------------------
# str2perl STRING,[QUOTECHAR]
#------------------------------------------------------------
# Convert a string to a double-quoted Perl string.
# Unsafe characters and non-printables are escaped.
#
# If QUOTECHAR is nonempty (default is '"'), it is escaped and also
# wrapped around the string.
sub str2perl {
my ($str, $qq) = @_;
defined($qq) or $qq = '"';
$str =~ s/[\\\$\@]/\\$&/g; # unsafe
$str =~ s/$qq/\\$qq/g if $qq; # quote char
$str =~ s/[$PerlEscapePat]/\\$PerlUnescape{$&}/og; # newlines, tabs...
$str =~ s/[\x00-\x1F\x7F-\xFF]/sprintf("\\x%02X", ord($&))/eg;
"$qq$str$qq";
}
#============================================================
package Main::Parser;
#============================================================
use HTML::Stream qw(:funcs);
use vars qw(@ISA);
@ISA = (qw(HTML::Parser));
# Are we inside a preformatter environment?
my $PREcount = 0;
#------------------------------------------------------------
# declaration
#------------------------------------------------------------
# This method is called when a markup declaration has been
# recognized. For typical HTML documents, the only declaration you are
# likely to find is . The initial ``''
# is not part of the string passed as argument. Comments
# are removed and entities have not been expanded yet.
sub declaration {
my ($self, $decl) = @_;
print '$HTML->io->print(', ::str2perl(""), ");\n";
}
#------------------------------------------------------------
# start
#------------------------------------------------------------
# This method is called when a complete start tag has been
# recognized. The first argument is the tag name (in lower case)
# and the second argument is a reference to a hash that contain
# all attributes found within the start tag. The attribute keys
# are converted to lower case. Entities found in the attribute
# values are already expanded.
sub start {
my ($self, $tag, $attr) = @_;
# Bookkeeping:
++$PREcount if ($tag eq 'pre');
# Output:
if (keys %$attr) {
# Output and remember first line, up to open paren:
my $firstline = "\$HTML -> \U$tag\E(";
print $firstline;
# Determine nice indent:
my $indent = ' ' x length($firstline);
$indent =~ s/ {8}/\t/g;
# Output tag params:
my @keys = sort keys %$attr;
my $i;
for ($i = 0; $i < int(@keys); $i++) {
print "\U$keys[$i]\E => ";
print ::str2perl($attr->{$keys[$i]});
print ",\n$indent" unless ($i == (int(@keys)-1));
}
print ");\n";
}
else {
print "\$HTML -> \U$tag\E;\n";
}
}
#------------------------------------------------------------
# end
#------------------------------------------------------------
# This method is called when an end tag has been recognized.
# The argument is the lower case tag name.
sub end {
my ($self, $tag) = @_;
print "\$HTML -> _\U$tag\E;\n";
# Bookkeeping:
--$PREcount if ($tag eq 'pre');
}
#------------------------------------------------------------
# should_output_whitespace
#------------------------------------------------------------
sub should_output_whitespace {
$::opt_w || ($PREcount > 0);
}
#------------------------------------------------------------
# whitespace
#------------------------------------------------------------
sub whitespace {
my $ws = shift;
return if (!$ws);
if ($ws =~ /\A\n*\Z/) {
print "\$HTML -> nl";
print( (length($ws) > 1) ? '('.length($ws).')' : '');
print ";\n";
}
else {
print "\$HTML -> t(", ::str2perl($ws), ");\n";
}
}
#------------------------------------------------------------
# text
#------------------------------------------------------------
# This method is called as plain text in the document
# is recognized. The text is passed on unmodified and might contain
# multiple lines. Note that for efficiency reasons entities in the
# text are not expanded. You should call
# HTML::Entities::decode($text) before you process the text any
# further.
sub text {
my ($self, $text) = @_;
# Do nothing if empty string:
return if (!defined($text) or ($text eq ''));
# Unescape HTML:
$text = HTML::Entities::decode($text);
# Break into WHITE* NONWHITE* WHITE*
my ($pre, $in, $dummy, $post) = ($text =~ /\A(\s*)((.|\n)*?)(\s*)\Z/);
# Output leading whitespace:
if ($pre && should_output_whitespace()) {
whitespace($pre);
}
# Output main text:
if (defined($in) && ($in ne '')) {
if (length($in) < 70) { # output as t()
print "\$HTML -> t(", ::str2perl($in), ");\n";
}
else { # output as hereis()
my $hereis = ::str2perl($in, '');
$hereis =~ s/\\n/\n/g;
print "output \$HTML < comment(", ::str2perl($comment), ");\n";
}
#============================================================
package main;
#============================================================
#------------------------------------------------------------
# print_header
#------------------------------------------------------------
sub print_header {
print <auto_format(0);
EOF
}
else {
print <auto_format(0);
EOF
}
print <flush;
STDERR->flush;
# Do it!
my $cmd = join(' ', ($^X, (map {"-I$_"} @INC), $perlfile));
# print STDERR "diff command = $cmd\n";
print STDERR " Running code: output in $html2\n";
system "$cmd > $html2";
print STDERR " Doing diff: output in $diff\n";
system "diff $htmlfile $html2 > $diff";
}
#------------------------------------------------------------
# main
#------------------------------------------------------------
sub main {
my $parser = new Main::Parser;
# Usage if no args:
@ARGV or usage();
# Get opts:
getopts "wd";
# Process files:
@ARGV or unshift @ARGV, '-';
my $infile;
while ($infile = shift @ARGV) {
my $outfile = (($infile eq '-') ? '-' : "$infile.pl");
# Trap for bad -d usage:
if ($opt_d and (($infile eq '-') || ($outfile eq '-'))) {
die "Cannot use -d if input is from STDIN\n";
}
# Report:
print STDERR "Converting ", ($infile eq '-' ? '' : $infile),
" to " , ($outfile eq '-' ? '' : $outfile),
"\n";
# Open input and output:
open INPUT, "<$infile" or die "$infile: $!";
open OUTPUT, ">$outfile" or die "$outfile: $!";
select OUTPUT;
# Header:
print_header();
$parser->parse_file(\*INPUT);
# Footer:
print_footer();
# Close:
close OUTPUT;
close INPUT;
# Do a diff...
do_diff($infile, $outfile) if ($opt_d);
}
print STDERR "Done.\n";
1;
}
exit (&main ? 0 : -1);
#------------------------------------------------------------
1;
HTML-Stream-1.60/Changes 0000644 0000766 0000766 00000010305 11046440635 016642 0 ustar danieltstaal danieltstaal CHANGE LOG
Version 1.60 (2008/08/06)
Fixed up the tests some more, updated changelog. (Which I'd
forgotten about...)
Version 1.59 (2008/06/01)
Better tests, better Meta.yml.
Version 1.58 (2008/05/28)
Another attempt at cleanup, as well expanding the Meta.yml file.
Version 1.57 (2008/05/28)
Cleaned up the Mac-specific files that were getting created in the
archive.
Version 1.56 (2008/05/27)
Added the start of a testing suite. In the process, I found an
error: HTML defines the tag 'NOFRAMES', not 'NOFRAME'. Both are
currently in the tag list, but consider 'NOFRAME' depriciated.
The test suite requires Test::More and Test::Output.
Version 1.55 (2003/10/28)
New maintainer: Daniel T. Staal. No major changes in the code,
except to complete the tag list to HTML 4.01 specifications. (With
the exception of the 'S' tag, which I want to test, and is
depreciated anyway. Note that the DOCTYPE is not actually a HTML
tag, and is not currently included.)
Version 1.54 (2001/08/20)
The terms-of-use have been placed in the distribution file
"COPYING". Also, small documentation tweaks were made.
Version 1.51 (2001/08/16)
No real changes to code; just improved documentation, and removed
HTML::Entities and HTML::Parser from ./etc at CPAN's request.
Version 1.47 (2000/06/10)
No real changes to code; just improved documentation.
Version 1.45 (1999/02/09)
Cleanup for Perl 5.005: removed duplicate typeglob assignments.
Version 1.44 (1998/01/14)
Win95 install (5.004) now works. Added SYNOPSIS to POD.
Version 1.41 (1998/01/02)
Removed $& for efficiency. *Thanks, Andreas!*
Added support for OPTION, and default now puts newlines after SELECT
and /SELECT. Also altered "TELEM" syntax to put newline after
end-tags of list element tags (like /OPTION, /LI, etc.). In theory,
this change could produce undesireable results for folks who embed
lists inside of PRE environments... however, that kind of stuff was
done in the days before TABLEs; also, you can always turn it off if
you really need to. *Thanks to John D Groenveld for these patches.*
Added text_nbsp(). *Thanks to John D Groenveld for the patch.* This
method may also be invoked as nbsp_text() as in the original patch,
but that's sort of a private tip-of-the-hat to the patch author, and
the synonym may go away in the future.
Version 1.37 (1997/02/09)
No real change; just trying to make CPAN.pm happier.
Version 1.32 (1997/01/12)
NEW TOOL for generating Perl code which uses HTML::Stream! Check
your toolkit for html2perlstream.
Added built-in support for escaping 8-bit characters.
Added "LATIN_1" auto-escape, which uses HTML::Entities to generate
mnemonic entities. This is now the default method for
HTML::Stream::Latin1.
Added "auto_format()," so you can now turn auto-formatting off/on.
Added "private_tags()", so it is now possible for HTML streams to
each have their own "private" copy of the %Tags table, for use by
"set_tag()".
Added "set_tag()". The tags tables may now be modified dynamically
so as to change how formatting is done on-the-fly. This will
hopefully not compromise the efficiency of the chocolate interface
(until now, the formatting was compiled into the method itself), and
*will* add greater flexibility for more-complex programs.
Added POD documentation for all subroutines in the public interface.
Version 1.29 (1996/12/10)
Added terminating newline to comment(). *Thanks to John D Groenveld
for the suggestion and the patch.*
Version 1.27 (1996/12/10)
Added built-in HTML::Stream::Latin1, which does a very simple
encoding of all characters above ASCII 127.
Fixed bug in accept_tag(), where 'my' variable was shadowing
argument. *Thanks to John D Groenveld for the bug report and the
patch.*
Version 1.26 (1996/09/27)
Start of history. HTML-Stream-1.60/COPYING 0000644 0000766 0000120 00000003701 10000052601 014764 0 ustar danieltstaal admin The "HTML-Stream" Perl5 toolkit.
Copyright (c) 1996 by Eryq. All rights reserved.
Copyright (c) 1999 by ZeeGee Software Inc. All rights reserved.
Copyright (c) 2004 by Daniel Staal. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
You should have received a copy of the Perl license along with
Perl; see the file README in Perl distribution.
You should have received a copy of the GNU General Public License
along with Perl; see the file Copying. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
You should have received a copy of the Artistic License
along with Perl; see the file Artistic.
NO WARRANTY
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
HTML-Stream-1.60/docs/ 0000755 0000766 0000120 00000000000 11046441050 014673 5 ustar danieltstaal admin HTML-Stream-1.60/docs/giggles.html 0000644 0000766 0000120 00000000661 07340272077 017222 0 ustar danieltstaal admin
Generated Mon Aug 20 16:34:07 2001 by cvu_pod2html
HTML-Stream-1.60/docs/HTML/ 0000755 0000766 0000120 00000000000 11046441050 015437 5 ustar danieltstaal admin HTML-Stream-1.60/docs/HTML/icons/ 0000755 0000766 0000120 00000000000 11046441050 016552 5 ustar danieltstaal admin HTML-Stream-1.60/docs/HTML/icons/h1bullet.gif 0000644 0000766 0000120 00000000645 07340272052 020774 0 ustar danieltstaal admin GIF87a `Lx(hxX P H @ @$XPP 8 8 8