LHTML serialization.
[closure-html.git] / src / parse / html-parser.lisp
blob55fcf4c0fdd7ebc09a83c1e106b193e7e14a4fab
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.)
6 #||
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))
13 (unwind-protect
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)))
17 ((null x))
18 (assert (equal (symbol-name x) "PUBLIC"))
19 (let ((name (read input))
20 (file (read input)))
21 (assert (stringp name))
22 (assert (stringp file))
23 (push (cons name (url:merge-url (url:parse-url file) catalog-url))
24 *simple-catalog*)))))
25 (g/close io))))
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~%")
32 ||#
34 (defparameter sgml::*simple-catalog*
35 (let ((base
36 (merge-pathnames
37 "resources/"
38 (asdf:component-relative-pathname
39 (asdf:find-system :closure-html)))))
40 (loop
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)))
63 (if handler
64 (serialize-pt pt handler)
65 pt)))
67 (defun parse (input handler &key pathname)
68 (declare (ignore pathname))
69 (etypecase input
70 (xstream
71 (parse-xstream input handler))
72 (rod
73 (let ((xstream (make-rod-xstream (string-rod input))))
74 ;;; (setf (xstream-name xstream)
75 ;;; (make-stream-name
76 ;;; :entity-name "main document"
77 ;;; :entity-kind :main
78 ;;; :uri nil))
79 (parse-xstream xstream handler)))
80 (array
81 (parse (make-octet-input-stream input) handler))
82 (pathname
83 (with-open-file (s input :element-type '(unsigned-byte 8))
84 (parse s handler :pathname input)))
85 (stream
86 (let ((xstream (make-xstream input :speed 8192)))
87 ;;; (setf (xstream-name xstream)
88 ;;; (make-stream-name
89 ;;; :entity-name "main document"
90 ;;; :entity-kind :main
91 ;;; :uri (pathname-to-uri
92 ;;; (merge-pathnames (or pathname (pathname input))))))
93 (parse-xstream xstream handler)))))
95 (defun serialize-pt-attributes (plist)
96 (loop
97 for (name value) on plist by #'cddr
98 collect
99 (let ((n (coerce (symbol-name name) 'rod))
100 (v (etypecase value
101 (symbol (coerce (string-downcase (symbol-name value)) 'rod))
102 (rod value)
103 (string (coerce value 'rod)))))
104 (hax:make-attribute n v t))))
106 (defun serialize-pt (document handler &key (name "HTML") public-id system-id)
107 (hax:start-document handler name public-id system-id)
108 (labels ((recurse (pt)
109 (cond
110 ((eq (gi pt) :pcdata)
111 (hax:characters handler (pt-attrs pt)))
113 (let ((name (coerce (symbol-name (pt-name pt)) 'rod))
114 (attrs (serialize-pt-attributes (pt-attrs pt))))
115 (hax:start-element handler name attrs)
116 (mapc #'recurse (pt-children pt))
117 (hax:end-element handler name))))))
118 (recurse document))
119 (hax:end-document handler))
121 (defclass pt-builder (hax:abstract-handler)
122 ((current :initform nil :accessor current)
123 (root :initform nil :accessor root)))
125 (defun make-pt-builder ()
126 (make-instance 'pt-builder))
128 (defmethod hax:start-document ((handler pt-builder) name pubid sysid)
129 (declare (ignore name pubid sysid))
130 nil)
132 (defun unserialize-pt-attributes (attrs)
133 (loop
134 for a in attrs
135 collect (intern (string-upcase (hax:attribute-name a)) :keyword)
136 collect (hax:attribute-value a)))
138 (defmethod hax:start-element ((handler pt-builder) name attrs)
139 (let* ((parent (current handler))
140 (this (sgml::make-pt/low
141 :name (intern (string-upcase name) :keyword)
142 :attrs (unserialize-pt-attributes attrs)
143 :parent parent)))
144 (setf (current handler) this)
145 (if parent
146 (push this (pt-children parent))
147 (setf (root handler) this))))
149 (defmethod hax:characters ((handler pt-builder) data)
150 (push data (pt-children (current handler))))
152 (defmethod hax:comment ((handler pt-builder) data)
153 ;; zzz haven't found out what the representation of comments is...
154 data)
156 (defmethod hax:end-element ((handler pt-builder) name)
157 (let ((current (current handler)))
158 (setf (pt-children current) (nreverse (pt-children current)))
159 (setf (current handler) (pt-parent current))))
161 (defmethod hax:end-document ((handler pt-builder))
162 (root handler))