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
))
76 (elt (and key
(sgml::find-element closure-html
::*html-dtd
* key
)))
77 (attlist (sgml::element-attlist elt
)))
78 (push (cons name elt
) (stack sink
))
79 (%write-rune
#/< sink
)
80 (%write-rod name sink
)
81 (dolist (a attributes
)
82 (let* ((aname (hax:attribute-name a
))
83 (akey (find-symbol (string-upcase (string-rod aname
)) :keyword
))
84 (att (and akey
(assoc akey attlist
)))
85 (values (second att
)))
86 (%write-rune
#/space sink
)
87 (%write-rod aname sink
)
88 (unless (and att
(listp values
) (eq (car att
) (car values
)))
89 (%write-rune
#/= sink
)
90 (%write-rune
#/\" sink
)
91 (unparse-string (hax:attribute-value a
) sink
)
92 (%write-rune
#/\" sink
))))
93 (%write-rune
#/> sink
)))
95 (defmethod hax:end-element
97 (let* ((prev (pop (stack sink
)))
98 (prev-name (car prev
))
100 (unless (rod= prev-name name
)
101 (error "output does not nest: expected ~A but got ~A"
103 (unless (and elt
(null (sgml::element-include elt
)))
104 (%write-rod
'#.
(string-rod "</") sink
)
105 (%write-rod name sink
)
106 (%write-rod
'#.
(string-rod ">") sink
))))
108 (defmethod hax:characters
((sink sink
) data
)
109 (let ((y (sink-ystream sink
)))
110 (loop for c across data do
(unparse-datachar-readable c y
))))
112 (defmethod hax:comment
((sink sink
) data
)
113 ;; XXX signal error if body is unprintable?
114 (%write-rod
#"<!--" sink
)
115 (map nil
(lambda (c) (%write-rune c sink
)) data
)
116 (%write-rod
#"-->" sink
))
118 (defun unparse-string (str sink
)
119 (let ((y (sink-ystream sink
)))
120 (loop for rune across str do
(unparse-datachar rune y
))))
122 (defun unparse-datachar (c ystream
)
123 (cond ((rune= c
#/&) (write-rod '#.
(string-rod "&") ystream
))
124 ((rune= c
#/<) (write-rod '#.
(string-rod "<") ystream
))
125 ((rune= c
#/>) (write-rod '#.
(string-rod ">") ystream
))
126 ((rune= c
#/\") (write-rod '#.
(string-rod """) ystream
))
127 ((rune= c
#/U
+0009) (write-rod '#.
(string-rod "	") ystream
))
128 ((rune= c
#/U
+000A
) (write-rod '#.
(string-rod " ") ystream
))
129 ((rune= c
#/U
+000D
) (write-rod '#.
(string-rod " ") ystream
))
131 (write-rune c ystream
))))
133 (defun unparse-datachar-readable (c ystream
)
134 (cond ((rune= c
#/&) (write-rod '#.
(string-rod "&") ystream
))
135 ((rune= c
#/<) (write-rod '#.
(string-rod "<") ystream
))
136 ((rune= c
#/>) (write-rod '#.
(string-rod ">") ystream
))
137 ((rune= c
#/\") (write-rod '#.
(string-rod """) ystream
))
138 ((rune= c
#/U
+000D
) (write-rod '#.
(string-rod " ") ystream
))
140 (write-rune c ystream
))))
142 (defun unparse-dtd-string (str sink
)
143 (let ((y (sink-ystream sink
)))
144 (loop for rune across str do
(unparse-dtd-char rune y
))))
146 (defun unparse-dtd-char (c ystream
)
147 (cond ((rune= c
#/%
) (write-rod '#.
(string-rod "%") ystream
))
148 ((rune= c
#/&) (write-rod '#.
(string-rod "&") ystream
))
149 ((rune= c
#/<) (write-rod '#.
(string-rod "<") ystream
))
150 ((rune= c
#/>) (write-rod '#.
(string-rod ">") ystream
))
151 ((rune= c
#/\") (write-rod '#.
(string-rod """) ystream
))
152 ((rune= c
#/U
+0009) (write-rod '#.
(string-rod "	") ystream
))
153 ((rune= c
#/U
+000A
) (write-rod '#.
(string-rod " ") ystream
))
154 ((rune= c
#/U
+000D
) (write-rod '#.
(string-rod " ") ystream
))
156 (write-rune c ystream
))))
158 (defun %write-rune
(c sink
)
159 (write-rune c
(sink-ystream sink
)))
161 (defun %write-rod
(r sink
)
162 (write-rod r
(sink-ystream sink
)))
165 ;;;; convenience functions for PTless HTML serialization
167 (defvar *current-element
*)
170 (defmacro with-html-output
171 ((sink &key
(name "HTML") public-id system-id
) &body body
)
172 `(invoke-with-html-output (lambda () ,@body
)
178 (defun invoke-with-html-output (fn sink name pubid sysid
)
180 (*current-element
* nil
))
181 (hax:start-document
*sink
* name pubid sysid
)
183 (hax:end-document
*sink
*)))
185 ;; fuer XML ist hier mehr zu tun, also gehen wir vorsichtshalber fuer HTML
186 ;; erstmal auch diesen Weg
187 (defmacro with-output-sink
((var) &body body
)
188 `(invoke-with-output-sink (lambda (,var
) ,@body
)))
189 (defun invoke-with-output-sink (fn)
192 (defmacro with-element
(name &body body
)
193 `(invoke-with-element (lambda () ,@body
) ,name
))
195 (defun maybe-emit-start-tag ()
196 (when *current-element
*
197 ;; starting child node, need to emit opening tag of parent first:
198 (destructuring-bind (name &rest attributes
) *current-element
*
199 (hax:start-element
*sink
* name
(reverse attributes
)))
200 (setf *current-element
* nil
)))
202 (defun invoke-with-element (fn name
)
203 (setf name
(rod name
))
204 (maybe-emit-start-tag)
205 (let ((*current-element
* (list name
)))
206 (multiple-value-prog1
208 (maybe-emit-start-tag)
209 (hax:end-element
*sink
* name
))))
211 (defgeneric unparse-attribute
(value))
212 (defmethod unparse-attribute ((value string
)) value
)
213 (defmethod unparse-attribute ((value null
)) nil
)
214 (defmethod unparse-attribute ((value integer
)) (write-to-string value
))
216 (defun attribute (name value
)
217 (setf name
(rod name
))
218 (setf value
(unparse-attribute value
))
219 (push (hax:make-attribute name value t
)
220 (cdr *current-element
*)))
223 (maybe-emit-start-tag)
224 (hax:characters
*sink
* (rod data
))
227 (defun comment (data)
228 (maybe-emit-start-tag)
229 (hax:comment
*sink
* (rod data
))