1 ;;; -*- show-trailing-whitespace: t; indent-tabs: nil -*-
2 ;;; ---------------------------------------------------------------------------
3 ;;; Title: Unparse HTML
4 ;;; Created: 2007-10-14
5 ;;; Author: David Lichteblau <david@lichteblau.com>
7 ;;; ---------------------------------------------------------------------------
8 ;;; (c) copyright 2005-2007 David Lichteblau
11 (in-package :closure-html
)
14 ;;; SINK: an HTML output sink
16 (defclass sink
(hax:abstract-handler
)
17 ((ystream :initarg
:ystream
:accessor sink-ystream
)
18 (stack :initform nil
:accessor stack
)))
21 (defmethod hax:%want-strings-p
((handler sink
))
24 ;; bisschen unschoen hier SCHON WIEDER die ganze api zu duplizieren, aber die
25 ;; ystreams sind noch undokumentiert
26 (macrolet ((define-maker (make-sink make-ystream
&rest args
)
27 `(defun ,make-sink
(,@args
&rest initargs
)
28 (apply #'make-instance
30 :ystream
(,make-ystream
,@args
)
32 (define-maker make-octet-vector-sink make-octet-vector-ystream
)
33 (define-maker make-octet-stream-sink make-octet-stream-ystream stream
)
34 (define-maker make-rod-sink make-rod-ystream
)
37 (define-maker make-character-stream-sink make-character-stream-ystream stream
)
40 (define-maker make-string-sink
/utf8 make-string-ystream
/utf8
)
43 (define-maker make-character-stream-sink
/utf8
44 make-character-stream-ystream
/utf8
48 (defun make-string-sink (&rest args
) (apply #'make-rod-sink args
))
53 (defmethod hax:start-document
((sink sink
) name public-id system-id
)
54 (when (plusp (length system-id
))
55 (%write-rod
#"<!DOCTYPE " sink
)
56 (%write-rod name sink
)
58 ((plusp (length public-id
))
59 (%write-rod
#" PUBLIC \"" sink
)
60 (unparse-string public-id sink
)
61 (%write-rod
#"\" \"" sink
)
62 (unparse-string system-id sink
)
63 (%write-rod
#"\"" sink
))
65 (%write-rod
#" SYSTEM \"" sink
)
66 (unparse-string system-id sink
)
67 (%write-rod
#"\"" sink
)))
68 (%write-rod
#">" sink
)
69 (%write-rune
#/U
+000A sink
)))
71 (defmethod hax:end-document
((sink sink
))
72 (close-ystream (sink-ystream sink
)))
74 (defmethod hax:start-element
((sink sink
) name attributes
)
75 (let* ((key (find-symbol (string-upcase (rod-string name
)) :keyword
))
77 (and key
(sgml::find-element closure-html
::*html-dtd
* key nil nil
)))
78 (attlist (and elt
(sgml::element-attlist elt
))))
79 (push (cons name elt
) (stack sink
))
80 (%write-rune
#/< sink
)
81 (%write-rod name sink
)
82 (dolist (a attributes
)
83 (let* ((aname (hax:attribute-name a
))
84 (akey (find-symbol (string-upcase (string-rod aname
)) :keyword
))
85 (att (and akey
(assoc akey attlist
)))
86 (values (second att
)))
87 (%write-rune
#/space sink
)
88 (%write-rod aname sink
)
89 (unless (and att
(listp values
) (eq (car att
) (car values
)))
90 (%write-rune
#/= sink
)
91 (%write-rune
#/\" sink
)
92 (let ((value (hax:attribute-value a
)))
93 (when (uri-attribute-p aname
)
94 (setf value
(escape-uri-attribute value
)))
95 (unparse-attribute-string value sink
))
96 (%write-rune
#/\" sink
))))
97 (%write-rune
#/> sink
)))
99 ;;; everything written as %URI in the DTD. Complete list per element,
100 ;;; as found in the HTML 4.01 Strict DTD
105 ;;; img src longdesc usemap
106 ;;; object classid codebase data usemap
117 ;;; plus the reserved attribute datasrc.
118 (defun uri-attribute-p (name)
119 (find (rod-downcase name
)
120 '(#"action" #"cite" #"classid" #"codebase" #"data" #"for" #"href"
121 #"longdesc" #"profile" #"src" #"usemap")
124 (defun escape-uri-attribute (x)
128 (defmethod hax:end-element
130 (let* ((prev (pop (stack sink
)))
131 (prev-name (car prev
))
133 (unless (rod= prev-name name
)
134 (error "output does not nest: expected ~A but got ~A"
136 (unless (and elt
(null (sgml::element-include elt
)))
137 (%write-rod
'#.
(string-rod "</") sink
)
138 (%write-rod name sink
)
139 (%write-rod
'#.
(string-rod ">") sink
))))
141 (defmethod hax:characters
((sink sink
) data
)
142 (let ((y (sink-ystream sink
)))
143 (if (find (caar (stack sink
)) '("script" "style") :test
'equalp
)
144 (write-rod data
(sink-ystream sink
))
145 (loop for c across data do
(unparse-datachar-readable c y
)))))
147 (defmethod hax:unescaped
((sink sink
) data
)
148 (%write-rod data sink
))
150 (defmethod hax:comment
((sink sink
) data
)
151 ;; XXX signal error if body is unprintable?
152 (%write-rod
#"<!--" sink
)
153 (map nil
(lambda (c) (%write-rune c sink
)) data
)
154 (%write-rod
#"-->" sink
))
156 (defun unparse-string (str sink
)
157 (let ((y (sink-ystream sink
)))
158 (loop for rune across str do
(unparse-datachar rune y
))))
160 (defun unparse-attribute-string (str sink
)
161 (let ((y (sink-ystream sink
)))
167 (if (and (< i
(length str
)) (rune= (rune str i
) #/{))
169 (write-rod '#.
(string-rod "&") y
)))
170 ((rune= c
#/\") (write-rod '#.
(string-rod """) y
))
171 ((rune= c
#/U
+000A
) (write-rod '#.
(string-rod " ") y
))
172 ((rune= c
#/U
+000D
) (write-rod '#.
(string-rod " ") y
))
174 (write-rune c y
))))))
176 (defun unparse-datachar (c ystream
)
177 (cond ((rune= c
#/&) (write-rod '#.
(string-rod "&") ystream
))
178 ((rune= c
#/<) (write-rod '#.
(string-rod "<") ystream
))
179 ((rune= c
#/>) (write-rod '#.
(string-rod ">") ystream
))
180 ((rune= c
#/\") (write-rod '#.
(string-rod """) ystream
))
181 ((rune= c
#/U
+0009) (write-rod '#.
(string-rod "	") ystream
))
182 ((rune= c
#/U
+000A
) (write-rod '#.
(string-rod " ") ystream
))
183 ((rune= c
#/U
+000D
) (write-rod '#.
(string-rod " ") ystream
))
185 (write-rune c ystream
))))
187 (defun unparse-datachar-readable (c ystream
)
188 (cond ((rune= c
#/&) (write-rod '#.
(string-rod "&") ystream
))
189 ((rune= c
#/<) (write-rod '#.
(string-rod "<") ystream
))
190 ((rune= c
#/>) (write-rod '#.
(string-rod ">") ystream
))
191 ((rune= c
#/\") (write-rod '#.
(string-rod """) ystream
))
192 ((rune= c
#/U
+000D
) (write-rod '#.
(string-rod " ") ystream
))
194 (write-rune c ystream
))))
196 (defun unparse-dtd-string (str sink
)
197 (let ((y (sink-ystream sink
)))
198 (loop for rune across str do
(unparse-dtd-char rune y
))))
200 (defun unparse-dtd-char (c ystream
)
201 (cond ((rune= c
#/%
) (write-rod '#.
(string-rod "%") ystream
))
202 ((rune= c
#/&) (write-rod '#.
(string-rod "&") ystream
))
203 ((rune= c
#/<) (write-rod '#.
(string-rod "<") ystream
))
204 ((rune= c
#/>) (write-rod '#.
(string-rod ">") ystream
))
205 ((rune= c
#/\") (write-rod '#.
(string-rod """) ystream
))
206 ((rune= c
#/U
+0009) (write-rod '#.
(string-rod "	") ystream
))
207 ((rune= c
#/U
+000A
) (write-rod '#.
(string-rod " ") ystream
))
208 ((rune= c
#/U
+000D
) (write-rod '#.
(string-rod " ") ystream
))
210 (write-rune c ystream
))))
212 (defun %write-rune
(c sink
)
213 (write-rune c
(sink-ystream sink
)))
215 (defun %write-rod
(r sink
)
216 (write-rod r
(sink-ystream sink
)))
219 ;;;; convenience functions for PTless HTML serialization
221 (defvar *current-element
*)
224 (defmacro with-html-output
225 ((sink &key
(name "HTML") public-id system-id
) &body body
)
226 `(invoke-with-html-output (lambda () ,@body
)
232 (defun invoke-with-html-output (fn sink name pubid sysid
)
234 (*current-element
* nil
))
235 (hax:start-document
*sink
* name pubid sysid
)
237 (hax:end-document
*sink
*)))
239 ;; fuer XML ist hier mehr zu tun, also gehen wir vorsichtshalber fuer HTML
240 ;; erstmal auch diesen Weg
241 (defmacro with-output-sink
((var) &body body
)
242 `(invoke-with-output-sink (lambda (,var
) ,@body
)))
243 (defun invoke-with-output-sink (fn)
246 (defmacro with-element
(name &body body
)
247 `(invoke-with-element (lambda () ,@body
) ,name
))
249 (defun maybe-emit-start-tag ()
250 (when *current-element
*
251 ;; starting child node, need to emit opening tag of parent first:
252 (destructuring-bind (name &rest attributes
) *current-element
*
253 (hax:start-element
*sink
* name
(reverse attributes
)))
254 (setf *current-element
* nil
)))
256 (defun invoke-with-element (fn name
)
257 (setf name
(rod name
))
258 (maybe-emit-start-tag)
259 (let ((*current-element
* (list name
)))
260 (multiple-value-prog1
262 (maybe-emit-start-tag)
263 (hax:end-element
*sink
* name
))))
265 (defgeneric unparse-attribute
(value))
266 (defmethod unparse-attribute ((value string
)) value
)
267 (defmethod unparse-attribute ((value null
)) nil
)
268 (defmethod unparse-attribute ((value integer
)) (write-to-string value
))
270 (defun attribute (name value
)
271 (setf name
(rod name
))
272 (setf value
(unparse-attribute value
))
273 (push (hax:make-attribute name value t
)
274 (cdr *current-element
*)))
277 (maybe-emit-start-tag)
278 (hax:characters
*sink
* (rod data
))
281 (defun comment (data)
282 (maybe-emit-start-tag)
283 (hax:comment
*sink
* (rod data
))