A permissive HTML parser supporting scalable streaming with a tree folding interface. This copies the interface of Oleg Kiselyov's SSAX parser, as well as providing simple convenience utilities. It correctly handles all invalid HTML, inserting "virtual" starting and closing tags as needed to maintain the proper tree structure needed for the foldts down/up logic. A major goal of this parser is bug-for-bug compatibility with the way common web browsers parse HTML.
Returns a procedure of two arguments, and initial seed and an optional input port, which parses the HTML document from the port with the callbacks specified in the plistkeys
(using
normal, quoted symbols, for portability and to avoid making this a
macro). The following callbacks are recognized:
-
(start tag attrs seed virtual?)
- fdown in foldts, called when a start-tag is encountered.tag
: tag nameattrs
: tag attributes as a alistseed
: current seed valuevirtual?
: #t iff this start tag was inserted to fix the HTML tree
-
(end tag attrs parent-seed seed virtual?)
- fup in foldts, called when an end-tag is encountered.tag
: tag nameattrs
: tag attributes of the corresponding start tagparent-seed
: parent seed value (i.e. seed passed to the start tag)seed
: current seed valuevirtual?
: #t iff this end tag was inserted to fix the HTML tree
-
(text text seed)
- fhere in foldts, called when any text is encountered. May be called multiple times between a start and end tag, so you need to string-append yourself if desired.text
: entity-decoded textseed
: current seed value
-
(comment text seed)
- fhere on comment data -
(decl name attrs seed)
- fhere on declaration data -
(process list seed)
- fhere on process-instruction data -
(entity name-or-num seed)
- convert an entity with a given name, or number via &#NNNN; to a string
entities
: keyword.
Example: the parser for html-strip could be defined as:
(make-html-parser
'start: (lambda (tag attrs seed virtual?) seed)
'end: (lambda (tag attrs parent-seed seed virtual?) seed)
'text: (lambda (text seed) (display text)))
Also see the parser code for html->sxml
.
Returns the SXML representation of the document from port
,
using the default parsing options.
Returns a string representation of the document from port
with all tags removed. No whitespace reduction or other rendering
is done.