(#& '&')
(#< '<')
(#> '>')
(#' ''')
(#' '"')
(t (format nil '&#~d;' (char-code char)))))
You can use this function as the basis for a function, escape
, that takes a string and a sequence of characters and returns a copy of the first argument with all occurrences of the characters in the second argument replaced with the corresponding character entity returned by escape-char
.
(defun escape (in to-escape)
(flet ((needs-escape-p (char) (find char to-escape)))
(with-output-to-string (out)
(loop for start = 0 then (1+ pos)
for pos = (position-if #'needs-escape-p in :start start)
do (write-sequence in out :start start :end pos)
when pos do (write-sequence (escape-char (char in pos)) out)
while pos))))
You can also define two parameters: *element-escapes*
, which contains the characters you need to escape in normal element data, and *attribute-escapes*
, which contains the set of characters to be escaped in attribute values.
(defparameter *element-escapes* '<>&')
(defparameter *attribute-escapes* '<>&''')
Here are some examples:
HTML> (escape 'foo & bar' *element-escapes*)
'foo & bar'
HTML> (escape 'foo & 'bar'' *element-escapes*)
'foo & 'bar''
HTML> (escape 'foo & 'bar'' *attribute-escapes*)
'foo & 'bar''
Finally, you'll need a variable, *escapes*
, that will be bound to the set of characters that need to be escaped. It's initially set to the value of *element-escapes*
, but when generating attributes, it will, as you'll see, be rebound to the value of *attribute-escapes*
.
(defvar *escapes* *element-escapes*)
To handle generating nicely indented output, you can define a class indenting-printer
, which wraps around an output stream, and functions that use an instance of that class to emit strings to the stream while keeping track of when it's at the beginning of the line. The class looks like this:
(defclass indenting-printer ()
((out :accessor out :initarg :out)
(beginning-of-line-p :accessor beginning-of-line-p :initform t)
(indentation :accessor indentation :initform 0)
(indenting-p :accessor indenting-p :initform t)))
The main function that operates on indenting-printer
s is emit
, which takes the printer and a string and emits the string to the printer's output stream, keeping track of when it emits a newline so it can reset the beginning-of-line-p
slot.
(defun emit (ip string)
(loop for start = 0 then (1+ pos)
for pos = (position #Newline string :start start)
do (emit/no-newlines ip string :start start :end pos)
when pos do (emit-newline ip)
while pos))
To actually emit the string, it uses the function emit/no-newlines
, which emits any needed indentation, via the helper indent-if-necessary
, and then writes the string to the stream. This function can also be called directly by other code to emit a string that's known not to contain any newlines.
(defun emit/no-newlines (ip string &key (start 0) end)
(indent-if-necessary ip)
(write-sequence string (out ip) :start start :end end)
(unless (zerop (- (or end (length string)) start))
(setf (beginning-of-line-p ip) nil)))
The helper indent-if-necessary
checks beginning-of-line-p
and indenting-p
to determine whether it needs to emit indentation and, if they're both true, emits as many spaces as indicated by the value of indentation
. Code that uses the indenting- printer
can control the indentation by manipulating the indentation
and indenting-p
slots. Incrementing and decrementing indentation
changes the number of leading spaces, while setting indenting-p
to NIL
can temporarily turn off indentation.
(defun indent-if-necessary (ip)
(when (and (beginning-of-line-p ip) (indenting-p ip))
(loop repeat (indentation ip) do (write-char #Space (out ip)))
(setf (beginning-of-line-p ip) nil)))
The last two functions in the indenting-printer
API are emit-newline
and emit-freshline
, which are both used to emit a newline character, similar to the ~ %
and ~& FORMAT
directives. That is, the only difference is that emit-newline
always emits a newline, while emit-freshline
does so only if beginning-of-line-p
is false. Thus, multiple calls to emit-freshline
without any intervening emit
s won't result in a blank line. This is handy when one piece of code wants to generate some output that should end with a newline while another piece of code wants to generate some output that should start on a newline but you don't want a blank line between the two bits of output.
(defun emit-newline (ip)
(write-char #Newline (out ip))
(setf (beginning-of-line-p ip) t))
(defun emit-freshline (ip)
(unless (beginning-of-line-p ip) (emit-newline ip)))
With those preliminaries out of the way, you're ready to get to the guts of the FOO processor.
Now you're ready to define the interface that'll be used by the FOO language processor to emit HTML. You can define this interface as a set of generic functions because you'll need two implementations—one that actually emits HTML and another that the html
macro can use to collect a list of actions that need to be performed, which can then be optimized and compiled into code that emits the same output in a more efficient way. I'll call this set of generic functions the