1 (in-package :closure-html
)
3 ;;; FIXME: I liked the old SLURP-CATALOG code better than the LOOP below.
4 ;;; (Except for the use of NETLIB and URI, which we don't have here.)
8 (defun slurp-catalog (catalog-url)
9 ;; Really dirty implementation
10 (setf *simple-catalog
* nil
)
11 (multiple-value-bind (io header
) (netlib::open-document-2 catalog-url
)
12 (declare (ignore header
))
14 (let ((str (glisp::gstream-as-string io
)))
15 (with-input-from-string (input str
)
16 (do ((x (read input nil nil
) (read input nil nil
)))
18 (assert (equal (symbol-name x
) "PUBLIC"))
19 (let ((name (read input
))
21 (assert (stringp name
))
22 (assert (stringp file
))
23 (push (cons name
(url:merge-url
(url:parse-url file
) catalog-url
))
27 (format T
"~&;; Parsing DTD~% ")
28 (sgml:slurp-catalog
(url:parse-url
"file://closure/resources/dtd/catalog"))
29 (setf cl-user
::*html-dtd
* (sgml:parse-dtd
'(:public
"-//W3C//DTD HTML 4.0 Frameset//EN")))
30 (format T
"~&;; done~%")
34 (defparameter sgml
::*simple-catalog
*
38 (asdf:component-relative-pathname
39 (asdf:find-system
:closure-html
)))))
41 :for
(name . filename
)
42 :in
'(("-//W3O//DTD W3 HTML 3.0//EN" .
"dtd/HTML-3.0")
43 ("NETSCAPE-Bookmark-file-1" .
"dtd/NETSCAPE-Bookmark-file-1")
44 ("-//W3C//ENTITIES Special//EN//HTML" .
"dtd/Entities-Special")
45 ("-//W3C//ENTITIES Symbols//EN//HTML" .
"dtd/Entities-Symbols")
46 ("-//W3C//ENTITIES Latin1//EN//HTML" .
"dtd/Entities-Latin1")
47 ("-//W3C//DTD HTML 4.0 Frameset//EN" .
"dtd/DTD-HTML-4.0-Frameset")
48 ("-//W3C//DTD HTML 4.0//EN" .
"dtd/DTD-HTML-4.0")
49 ("-//W3C//DTD HTML 4.0 Transitional//EN" .
"dtd/DTD-HTML-4.0-Transitional"))
50 :collect
(cons name
(merge-pathnames filename base
)))))
52 (defparameter *html-dtd
*
53 (sgml:parse-dtd
'(:public
"-//W3C//DTD HTML 4.0 Frameset//EN")))
55 (defun parse-xstream (input handler
)
56 (setf (sgml::a-stream-scratch input
)
57 (make-array #.
(* 2 4096) :element-type
'runes
:rune
))
58 (sgml::setup-code-vector input
:utf-8
)
59 (let* ((dtd *html-dtd
*)
60 (sgml::*unmungle-attribute-case
* t
)
61 (r (sgml:sgml-parse dtd input
))
62 (pt (sgml::post-mortem-heuristic dtd r
)))
64 (serialize-pt pt handler
)
67 (defun parse (input handler
)
70 (parse-xstream input handler
))
72 #-rune-is-integer
(setf input
(string-rod input
))
73 (let ((xstream (make-rod-xstream input
)))
74 ;;; (setf (xstream-name xstream)
76 ;;; :entity-name "main document"
77 ;;; :entity-kind :main
79 (parse-xstream xstream handler
)))
80 ((and array
(not string
))
81 (parse (make-octet-input-stream input
) handler
))
85 (make-array (length input
) :element-type
'(unsigned-byte 8))))
86 (map-into bytes
#'char-code input
)
87 (parse bytes handler
)))
89 (with-open-file (s input
:element-type
'(unsigned-byte 8))
92 (let ((xstream (make-xstream input
:speed
8192)))
93 ;;; (setf (xstream-name xstream)
95 ;;; :entity-name "main document"
96 ;;; :entity-kind :main
97 ;;; :uri (pathname-to-uri
98 ;;; (merge-pathnames (or pathname (pathname input))))))
99 (parse-xstream xstream handler
)))))
101 (defun serialize-pt-attributes (plist recode
)
103 for
(name value
) on plist by
#'cddr
105 ;; better don't emit as HAX what would be bogus as SAX anyway
106 (string-equal name
"xmlns")
108 (let* ((n #+rune-is-character
(coerce (symbol-name name
) 'rod
)
109 #-rune-is-character
(symbol-name name
))
111 (symbol (coerce (string-downcase (symbol-name value
)) 'rod
))
112 (rod (funcall recode value
))
113 (string (coerce value
'rod
)))))
114 (hax:make-attribute n v t
))))
116 (defun serialize-pt (document handler
117 &key
(name "HTML") public-id system-id
(documentp t
))
118 (let* ((recodep (or #+rune-is-integer
(hax:%want-strings-p handler
)))
123 (rod-to-utf8-string rod
)
127 (hax:start-document handler name public-id system-id
))
128 (labels ((recurse (pt)
130 ((eq (gi pt
) :pcdata
)
131 (hax:characters handler
(funcall recode
(pt-attrs pt
))))
133 (let* ((name (symbol-name (pt-name pt
)))
135 #+rune-is-character
(coerce name
'rod
)
137 (if recodep name
(string-rod name
)))
139 (serialize-pt-attributes (pt-attrs pt
) recode
)))
140 (hax:start-element handler name attrs
)
141 (mapc #'recurse
(pt-children pt
))
142 (hax:end-element handler name
))))))
145 (hax:end-document handler
))))
147 (defclass pt-builder
(hax:abstract-handler
)
148 ((current :initform nil
:accessor current
)
149 (root :initform nil
:accessor root
)))
152 (defmethod hax:%want-strings-p
((handler pt-builder
))
155 (defun make-pt-builder ()
156 (make-instance 'pt-builder
))
158 (defmethod hax:start-document
((handler pt-builder
) name pubid sysid
)
159 (declare (ignore name pubid sysid
))
162 (defun unserialize-pt-attributes (attrs)
165 collect
(intern (string-upcase (hax:attribute-name a
)) :keyword
)
166 collect
(hax:attribute-value a
)))
168 (defmethod hax:start-element
((handler pt-builder
) name attrs
)
169 (let* ((parent (current handler
))
170 (this (sgml::make-pt
/low
171 :name
(intern (string-upcase name
) :keyword
)
172 :attrs
(unserialize-pt-attributes attrs
)
174 (setf (current handler
) this
)
176 (push this
(pt-children parent
))
177 (setf (root handler
) this
))))
179 (defmethod hax:characters
((handler pt-builder
) data
)
180 (push (sgml::make-pt
/low
183 :parent
(current handler
))
184 (pt-children (current handler
))))
186 (defmethod hax:comment
((handler pt-builder
) data
)
187 ;; zzz haven't found out what the representation of comments is...
190 (defmethod hax:end-element
((handler pt-builder
) name
)
191 (let ((current (current handler
)))
192 (setf (pt-children current
) (nreverse (pt-children current
)))
193 (setf (current handler
) (pt-parent current
))))
195 (defmethod hax:end-document
((handler pt-builder
))