Attribute parsing issues.
[closure-html.git] / src / parse / html-parser.lisp
blob3e956b05caa80af50c60b506ac96149b9472ccf8
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 (etypecase input
69 (xstream
70 (parse-xstream input handler))
71 (rod
72 (let ((xstream (make-rod-xstream (string-rod input))))
73 ;;; (setf (xstream-name xstream)
74 ;;; (make-stream-name
75 ;;; :entity-name "main document"
76 ;;; :entity-kind :main
77 ;;; :uri nil))
78 (parse-xstream xstream handler)))
79 (array
80 (parse (make-octet-input-stream input) handler))
81 (pathname
82 (with-open-file (s input :element-type '(unsigned-byte 8))
83 (parse s handler :pathname input)))
84 (stream
85 (let ((xstream (make-xstream input :speed 8192)))
86 ;;; (setf (xstream-name xstream)
87 ;;; (make-stream-name
88 ;;; :entity-name "main document"
89 ;;; :entity-kind :main
90 ;;; :uri (pathname-to-uri
91 ;;; (merge-pathnames (or pathname (pathname input))))))
92 (parse-xstream xstream handler)))))
94 (defun serialize-pt-attributes (plist)
95 (loop
96 for (name value) on plist by #'cddr
97 collect
98 (let ((n (coerce (symbol-name name) 'rod))
99 (v (etypecase value
100 (symbol (coerce (string-downcase (symbol-name value)) 'rod))
101 (rod value)
102 (string (coerce value 'rod)))))
103 (hax:make-attribute n v t))))
105 (defun serialize-pt (document handler &key (name "HTML") public-id system-id)
106 (hax:start-document handler name public-id system-id)
107 (labels ((recurse (pt)
108 (cond
109 ((eq (gi pt) :pcdata)
110 (hax:characters handler (pt-attrs pt)))
112 (let ((name (coerce (symbol-name (pt-name pt)) 'rod))
113 (attrs (serialize-pt-attributes (pt-attrs pt))))
114 (hax:start-element handler name attrs)
115 (mapc #'recurse (pt-children pt))
116 (hax:end-element handler name))))))
117 (recurse document))
118 (hax:end-document handler))