LispKit SXML HTML

Library (lispkit sxml html) implements a permissive, streaming HTML parser. Unlike the strict XML parser of library (lispkit sxml xml), this parser is designed to cope with the malformed and inconsistent markup that is commonplace in real-world HTML documents: it inserts “virtual” start and end tags as needed to keep the parsed document a properly nested tree, aiming for behavior comparable to how common web browsers parse HTML.

The parser is implemented as a tree-folding parser (in the style of foldts) with an interface modeled after the SSAX parser of library (lispkit sxml xml): a low-level, callback-based procedure make-html-parser for building custom parsers, plus two ready-to-use convenience procedures, html->sxml and html-strip, built on top of it.

(make-html-parser key val …)     [procedure]

Returns a new HTML parser, a procedure (seed [port]), that parses the HTML document from input port port (or, if port is a string, an input port opened on that string), invoking the given callbacks while folding over the document tree. If port is not provided, the parser reads from the current input port (as defined by current-input-port of library (lispkit port)). seed is the initial seed value, threaded through the callbacks and returned as the final result once the whole document (or fragment) has been parsed.

make-html-parser accepts the following keyword arguments; all of them are optional:

(define (html-strip . o)
  (call-with-output-string
    (lambda (out)
      (let ((parse
              (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 out)))))
        (apply parse (cons #f #f) o)))))

(html->sxml)     [procedure]
(html->sxml port)

Parses the HTML document available from input port port (or a string, in which case an input port is opened on it) and returns its content as an SXML tree, wrapped in a *TOP* element (see library (lispkit sxml) for details on the SXML representation). If port is not provided, html->sxml reads from the current input port. Declarations, processing instructions, and comments are represented, respectively, as (*DECL* name attr ...), (*PI* target body), and (*COMMENT* text) nodes.

(import (lispkit sxml html) (lispkit port))

(call-with-port (open-input-string "<ul><li>one</li><li>two</li></ul>")
  (lambda (port) (html->sxml port)))
  ⇒ (*TOP* (ul (li "one") (li "two")))

;; A missing closing tag for <b> is automatically fixed up:
(call-with-port (open-input-string "<p>Hello, <b>World</p>")
  (lambda (port) (html->sxml port)))
⇒ (*TOP* (p "Hello, " (b "World")))

(html-strip)     [procedure]
(html-strip port)

Parses the HTML document available from input port port (or a string) and returns a string containing all of its text content, with every tag removed. No whitespace reduction or other post-processing of the extracted text is performed. If port is not provided, html-strip reads from the current input port.

(html-strip "<p>5 &lt; 7 &amp; 7 &gt; 5</p>")
⇒ "5 < 7 & 7 > 5"