HTML generation

Please note that the latest version of this program is available via Tim Bradshaw's Lisp hacks page at

(with-html-output (var &optional stream) &body html) -> unspecified

Generates HTML output from Lisp.

If stream is supplied, var is bound to stream and output is written to that. If stream is not supplied, then var should previously have been bound to the desired output stream.

The HTML to be generated is specified in Lisp syntax. For example, see the following (included with the code):

(defun create-blank-page (s title) (with-html-output (s) (:html (:head (:title (esc title)) (lfd)) (:body (:h1 (esc title)) (lfd) "<!-- Body here -->" (lfd)))))

This generates a blank page with the given title. The list beginning :html is output as a begin <HTML> tag, followed by the tag content (the rest of the list after the keyword html), and lastly an end </HTML> tag... and so on.

The body of with-html-output consists of HTML in Lisp syntax (`LHTML') and/or Lisp code. For each form:

  • If the form is a keyword symbol it corresponds to an element with an empty content model - :br corresponds to <br>;
  • If the form is a list beginning with a keyword symbol it corresponds to an element with no attributes. If the list is empty and the element is defined as empty (below) then no closing tag is generated. (:p "foo") corresponds to <p>foo</p>;
  • If the form is a list beginning with a list which begins with a keyword symbol it corresponds to an element with attributes. The attributes and values are given by keyword `arguments'. ((:a :href "foo") "bar") corresponds to <a href "foo">bar</a>;
  • If the form is a string it corresponds to that literal string in the output; if the form is a character it corresponds to that character;
  • Otherwise, the form is Lisp code which is evaluated in the normal way.
  • with-html-output understands various shorthands for specifying particular HTML output, implemented as local macros:

    re-enters HTML mode, for instance from within Lisp code. This is essentially the same as a nested with-html-output
    shorthand for (format s ...) where s is the stream on which output is being generated
    escapes its argument string, quoting things that are magic to HTML. See escape-string below.
    outputs a linefeed on the output stream
    (escape-string string &optional (map *html-escape-map*) &optional) -> string

    This escapes the characters in map in string.

    *html-escape-map* -> map

    By default, is bound to the list

    ((#\< . "&lt;") (#\> . "&gt;") (#\& . "&amp;")))

    Indicating that the character < should be replaced by the string "&lt;", and so on.

    (define-empty-tags &rest tags)* -> tags

    Defines the list tags (in Lisp syntax) which have no content. In the source provided, a call to this macro is made defining the list of empty tags to be (:br :hr).

    Contact information

    Copyright htout.lisp is copyright 1999-2000 by me, Tim Bradshaw, and may be used for any purpose whatsoever by anyone. It has no warranty whatsoever. I would appreciate acknowledgement if you use it in anger, and I would also very much appreciate any feedback or bug fixes.
    Please mail me with any bugs or comments.

    Author: Tim Bradshaw(
    Copyright 1999–2003 Tim Bradshaw