Output encoding support, using Babel
[closure-html.git] / src / parse / unparse.lisp
blobb0cc805f41fe17f67b707744588d24e613550aa6
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>
6 ;;; License: BSD-style
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)))
20 #-rune-is-character
21 (defmethod hax:%want-strings-p ((handler sink))
22 nil)
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
29 'sink
30 :ystream (,make-ystream ,@args)
31 initargs))))
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)
36 #+rune-is-character
37 (define-maker make-character-stream-sink make-character-stream-ystream stream)
39 #-rune-is-character
40 (define-maker make-string-sink/utf8 make-string-ystream/utf8)
42 #-rune-is-character
43 (define-maker make-character-stream-sink/utf8
44 make-character-stream-ystream/utf8
45 stream))
47 #+rune-is-character
48 (defun make-string-sink (&rest args) (apply #'make-rod-sink args))
51 ;;;; Events
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)
57 (cond
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
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)
103 '((#"a" #"href")
104 (#"area" #"href")
105 (#"link" #"href")
106 (#"img" #"src" #"longdesc" #"usemap")
107 (#"object" #"classid" #"codebase" #"data" #"usemap")
108 (#"q" #"cite")
109 (#"blockquote" #"cite")
110 (#"inl" #"cite")
111 (#"del" #"cite")
112 (#"form" #"action")
113 (#"input" #"src" #"usemap")
114 (#"head" #"profile")
115 (#"base" #"href")
116 (#"script" #"src" ;; #"for"
118 :key #'car
119 :test #'rod=))
120 :test #'rod=))
122 (defun escape-uri-attribute (x)
123 (string-rod
124 (with-output-to-string (s)
125 (loop
126 for c across (rod-to-utf8-string x)
127 for code = (char-code c)
129 (if (< code 128)
130 (write-char c s)
131 (format s "%~2,'0X" code))))))
133 (defmethod hax:end-element
134 ((sink sink) name)
135 (let* ((prev (pop (stack sink)))
136 (prev-name (car prev))
137 (elt (cdr prev)))
138 (unless (rod= prev-name name)
139 (error "output does not nest: expected ~A but got ~A"
140 name prev-name))
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)))
167 (loop
168 for i from 1
169 for c across str
171 (cond ((rune= c #/&)
172 (if (and (< i (length str)) (rune= (rune str i) #/{))
173 (ystream-write-rune c y)
174 (ystream-write-rod '#.(string-rod "&amp;") y)))
175 ((rune= c #/\") (ystream-write-rod '#.(string-rod "&quot;") y))
176 ((rune= c #/U+000A) (ystream-write-rod '#.(string-rod "&#10;") y))
177 ((rune= c #/U+000D) (ystream-write-rod '#.(string-rod "&#13;") y))
179 (ystream-write-escapable-rune c y))))))
181 (defun unparse-datachar (c ystream)
182 (cond ((rune= c #/&) (ystream-write-rod '#.(string-rod "&amp;") ystream))
183 ((rune= c #/<) (ystream-write-rod '#.(string-rod "&lt;") ystream))
184 ((rune= c #/>) (ystream-write-rod '#.(string-rod "&gt;") ystream))
185 ((rune= c #/\") (ystream-write-rod '#.(string-rod "&quot;") ystream))
186 ((rune= c #/U+0009) (ystream-write-rod '#.(string-rod "&#9;") ystream))
187 ((rune= c #/U+000A) (ystream-write-rod '#.(string-rod "&#10;") ystream))
188 ((rune= c #/U+000D) (ystream-write-rod '#.(string-rod "&#13;") ystream))
190 (ystream-write-escapable-rune c ystream))))
192 (defun unparse-datachar-readable (c ystream)
193 (cond ((rune= c #/&) (ystream-write-rod '#.(string-rod "&amp;") ystream))
194 ((rune= c #/<) (ystream-write-rod '#.(string-rod "&lt;") ystream))
195 ((rune= c #/>) (ystream-write-rod '#.(string-rod "&gt;") ystream))
196 ((rune= c #/\") (ystream-write-rod '#.(string-rod "&quot;") ystream))
197 ((rune= c #/U+000D) (ystream-write-rod '#.(string-rod "&#13;") 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 "&#37;") ystream))
207 ((rune= c #/&) (ystream-write-rod '#.(string-rod "&amp;") ystream))
208 ((rune= c #/<) (ystream-write-rod '#.(string-rod "&lt;") ystream))
209 ((rune= c #/>) (ystream-write-rod '#.(string-rod "&gt;") ystream))
210 ((rune= c #/\") (ystream-write-rod '#.(string-rod "&quot;") ystream))
211 ((rune= c #/U+0009) (ystream-write-rod '#.(string-rod "&#9;") ystream))
212 ((rune= c #/U+000A) (ystream-write-rod '#.(string-rod "&#10;") ystream))
213 ((rune= c #/U+000D) (ystream-write-rod '#.(string-rod "&#13;") 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*)
227 (defvar *sink*)
229 (defmacro with-html-output
230 ((sink &key (name "HTML") public-id system-id) &body body)
231 `(invoke-with-html-output (lambda () ,@body)
232 ,sink
233 ,name
234 ,public-id
235 ,system-id))
237 (defun invoke-with-html-output (fn sink name pubid sysid)
238 (let ((*sink* sink)
239 (*current-element* nil))
240 (hax:start-document *sink* name pubid sysid)
241 (funcall fn)
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)
249 (funcall fn *sink*))
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
266 (funcall fn)
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*)))
281 (defun text (data)
282 (maybe-emit-start-tag)
283 (hax:characters *sink* (rod data))
284 data)
286 (defun comment (data)
287 (maybe-emit-start-tag)
288 (hax:comment *sink* (rod data))
289 data)