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 name 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:
100 (defun uri-attribute-p (ename aname
)
101 (find (rod-downcase aname
)
102 (cdr (find (rod-downcase ename
)
106 (#"img" #"src" #"longdesc" #"usemap")
107 (#"object" #"classid" #"codebase" #"data" #"usemap")
109 (#"blockquote" #"cite")
113 (#"input" #"src" #"usemap")
116 (#"script" #"src" ;; #"for"
122 (defun escape-uri-attribute (x)
124 (with-output-to-string (s)
126 for c across
(rod-to-utf8-string x
)
127 for code
= (char-code c
)
131 (format s
"%~2,'0X" code
))))))
133 (defmethod hax:end-element
135 (let* ((prev (pop (stack sink
)))
136 (prev-name (car prev
))
138 (unless (rod= prev-name name
)
139 (error "output does not nest: expected ~A but got ~A"
141 (unless (and elt
(null (sgml::element-include elt
)))
142 (%write-rod
'#.
(string-rod "</") sink
)
143 (%write-rod name sink
)
144 (%write-rod
'#.
(string-rod ">") sink
))))
146 (defmethod hax:characters
((sink sink
) data
)
147 (let ((y (sink-ystream sink
)))
148 (if (find (caar (stack sink
)) '("script" "style") :test
'equalp
)
149 (ystream-write-escapable-rod data
(sink-ystream sink
))
150 (loop for c across data do
(unparse-datachar-readable c y
)))))
152 (defmethod hax:unescaped
((sink sink
) data
)
153 (%write-rod data sink
))
155 (defmethod hax:comment
((sink sink
) data
)
156 ;; XXX signal error if body is unprintable?
157 (%write-rod
#"<!--" sink
)
158 (map nil
(lambda (c) (%write-rune c sink
)) data
)
159 (%write-rod
#"-->" sink
))
161 (defun unparse-string (str sink
)
162 (let ((y (sink-ystream sink
)))
163 (loop for rune across str do
(unparse-datachar rune y
))))
165 (defun unparse-attribute-string (str sink
)
166 (let ((y (sink-ystream sink
)))
172 (if (and (< i
(length str
)) (rune= (rune str i
) #/{))
173 (ystream-write-rune c y
)
174 (ystream-write-rod '#.
(string-rod "&") y
)))
175 ((rune= c
#/\") (ystream-write-rod '#.
(string-rod """) y
))
176 ((rune= c
#/U
+000A
) (ystream-write-rod '#.
(string-rod " ") y
))
177 ((rune= c
#/U
+000D
) (ystream-write-rod '#.
(string-rod " ") y
))
179 (ystream-write-escapable-rune c y
))))))
181 (defun unparse-datachar (c ystream
)
182 (cond ((rune= c
#/&) (ystream-write-rod '#.
(string-rod "&") ystream
))
183 ((rune= c
#/<) (ystream-write-rod '#.
(string-rod "<") ystream
))
184 ((rune= c
#/>) (ystream-write-rod '#.
(string-rod ">") ystream
))
185 ((rune= c
#/\") (ystream-write-rod '#.
(string-rod """) ystream
))
186 ((rune= c
#/U
+0009) (ystream-write-rod '#.
(string-rod "	") ystream
))
187 ((rune= c
#/U
+000A
) (ystream-write-rod '#.
(string-rod " ") ystream
))
188 ((rune= c
#/U
+000D
) (ystream-write-rod '#.
(string-rod " ") ystream
))
190 (ystream-write-escapable-rune c ystream
))))
192 (defun unparse-datachar-readable (c ystream
)
193 (cond ((rune= c
#/&) (ystream-write-rod '#.
(string-rod "&") ystream
))
194 ((rune= c
#/<) (ystream-write-rod '#.
(string-rod "<") ystream
))
195 ((rune= c
#/>) (ystream-write-rod '#.
(string-rod ">") ystream
))
196 ((rune= c
#/\") (ystream-write-rod '#.
(string-rod """) ystream
))
197 ((rune= c
#/U
+000D
) (ystream-write-rod '#.
(string-rod " ") ystream
))
199 (ystream-write-escapable-rune c ystream
))))
201 (defun unparse-dtd-string (str sink
)
202 (let ((y (sink-ystream sink
)))
203 (loop for rune across str do
(unparse-dtd-char rune y
))))
205 (defun unparse-dtd-char (c ystream
)
206 (cond ((rune= c
#/%
) (ystream-write-rod '#.
(string-rod "%") ystream
))
207 ((rune= c
#/&) (ystream-write-rod '#.
(string-rod "&") ystream
))
208 ((rune= c
#/<) (ystream-write-rod '#.
(string-rod "<") ystream
))
209 ((rune= c
#/>) (ystream-write-rod '#.
(string-rod ">") ystream
))
210 ((rune= c
#/\") (ystream-write-rod '#.
(string-rod """) ystream
))
211 ((rune= c
#/U
+0009) (ystream-write-rod '#.
(string-rod "	") ystream
))
212 ((rune= c
#/U
+000A
) (ystream-write-rod '#.
(string-rod " ") ystream
))
213 ((rune= c
#/U
+000D
) (ystream-write-rod '#.
(string-rod " ") ystream
))
215 (ystream-write-escapable-rune c ystream
))))
217 (defun %write-rune
(c sink
)
218 (ystream-write-rune c
(sink-ystream sink
)))
220 (defun %write-rod
(r sink
)
221 (ystream-write-rod r
(sink-ystream sink
)))
224 ;;;; convenience functions for PTless HTML serialization
226 (defvar *current-element
*)
229 (defmacro with-html-output
230 ((sink &key
(name "HTML") public-id system-id
) &body body
)
231 `(invoke-with-html-output (lambda () ,@body
)
237 (defun invoke-with-html-output (fn sink name pubid sysid
)
239 (*current-element
* nil
))
240 (hax:start-document
*sink
* name pubid sysid
)
242 (hax:end-document
*sink
*)))
244 ;; fuer XML ist hier mehr zu tun, also gehen wir vorsichtshalber fuer HTML
245 ;; erstmal auch diesen Weg
246 (defmacro with-output-sink
((var) &body body
)
247 `(invoke-with-output-sink (lambda (,var
) ,@body
)))
248 (defun invoke-with-output-sink (fn)
251 (defmacro with-element
(name &body body
)
252 `(invoke-with-element (lambda () ,@body
) ,name
))
254 (defun maybe-emit-start-tag ()
255 (when *current-element
*
256 ;; starting child node, need to emit opening tag of parent first:
257 (destructuring-bind (name &rest attributes
) *current-element
*
258 (hax:start-element
*sink
* name
(reverse attributes
)))
259 (setf *current-element
* nil
)))
261 (defun invoke-with-element (fn name
)
262 (setf name
(rod name
))
263 (maybe-emit-start-tag)
264 (let ((*current-element
* (list name
)))
265 (multiple-value-prog1
267 (maybe-emit-start-tag)
268 (hax:end-element
*sink
* name
))))
270 (defgeneric unparse-attribute
(value))
271 (defmethod unparse-attribute ((value string
)) value
)
272 (defmethod unparse-attribute ((value null
)) nil
)
273 (defmethod unparse-attribute ((value integer
)) (write-to-string value
))
275 (defun attribute (name value
)
276 (setf name
(rod name
))
277 (setf value
(unparse-attribute value
))
278 (push (hax:make-attribute name value t
)
279 (cdr *current-element
*)))
282 (maybe-emit-start-tag)
283 (hax:characters
*sink
* (rod data
))
286 (defun comment (data)
287 (maybe-emit-start-tag)
288 (hax:comment
*sink
* (rod data
))