package Vend::Parser; # $Id: Parser.pm,v 1.4 2000/02/06 01:50:33 mike Exp $ # # # Copyright 1996 Gisle Aas. All rights reserved. # # Modifications for MiniVend Copyright 1997-2000 by Michael J. Heins # # =head1 NAME Vend::Parser - MiniVend parser class =head1 SYNOPSIS require Vend::Parser; $p = Vend::Parser->new; # should really a be subclass $p->parse($chunk1); $p->parse($chunk2); #... $p->eof; # signal end of document # Parse directly from file $p->parse_file("foo.html"); # or open(F, "foo.html") || die; $p->parse_file(\*F); =head1 DESCRIPTION The C will tokenize a MiniVend page when the $p->parse() method is called. The document to parse can be supplied in arbitrary chunks. Call $p->eof() the end of the document to flush any remaining text. The return value from parse() is a reference to the parser object. The $p->parse_file() method can be called to parse text from a file. The argument can be a filename or an already opened file handle. The return value from parse_file() is a reference to the parser object. In order to make the parser do anything interesting, you must make a subclass where you override one or more of the following methods as appropriate: =over 4 =item $self->start($tag, $attr, $attrseq, $origtext) 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. The third argument is a reference to an array with the lower case attribute keys in the original order. The fourth argument is the original MiniVend page. =item $self->end($tag) This method is called when an end tag has been recognized. The argument is the lower case tag name. =item $self->text($text) This method is called when 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 B expanded. =item $self->comment($comment) This method is called as comments are recognized. The leading and trailing "--" sequences have been stripped off the comment text. =back The default implementation of these methods does nothing, I the tokens are just ignored. =head1 BUGS You can instruct the parser to parse comments the way Netscape does it by calling the netscape_buggy_comment() method with a TRUE argument. This means that comments will always be terminated by the first occurence of "-->". =head1 SEE ALSO L, L, L =head1 COPYRIGHT Copyright 1996 Gisle Aas. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Modified for use by MiniVend. Copyright 1997-1998 Mike Heins. =head1 AUTHOR Gisle Aas Modified by Mike Heins =cut use strict; use HTML::Entities (); use vars qw($VERSION); $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/); sub new { my $class = shift; my $self = bless { '_buf' => '' }, $class; $self; } # How does Netscape do it: It parse in the depreceated 'literal' # mode, i.e. no tags are recognized until a is found. # # is parsed like
, i.e. tags are recognized.  # are presentend in smaller font than 
#
# Netscape does not parse this comment correctly (it terminates the comment
# too early):
#
#     more comment -->
#
# Netscape does not allow space after the initial "<" in the start tag.
# Like this ""
#
# Netscape ignore '' within the