web/dom: basically working - still missing: render attrs
This commit is contained in:
parent
98d2246017
commit
b81d50cd43
2 changed files with 32 additions and 13 deletions
|
@ -42,4 +42,5 @@
|
|||
|
||||
(deftest test-message (client)
|
||||
(let ((msg (message:create '(:test :data :field :info) :data '(:info "test data"))))
|
||||
(== (client:send-message client msg) "test data")))
|
||||
(== (str:trim (client:send-message client msg))
|
||||
"<dl><dt>info</dt><dd>test data</dd></dl>")))
|
||||
|
|
42
web/dom.lisp
42
web/dom.lisp
|
@ -16,28 +16,46 @@
|
|||
,@body
|
||||
(get-output-stream-string *output*)))
|
||||
|
||||
(defmacro write-nested (start end &body body)
|
||||
`(progn
|
||||
(write-string ,start *output*)
|
||||
,@body
|
||||
(write-line ,end *output*)))
|
||||
(defmacro put-string (s)
|
||||
`(write-string ,s *output*))
|
||||
|
||||
(defun write-simple (start end val)
|
||||
(write-string start *output*)
|
||||
(write-string val *output*)
|
||||
(write-string end *output*))
|
||||
(defmacro put-char (c)
|
||||
`(write-char ,c *output*))
|
||||
|
||||
(defmacro nested (tag attrs &body body)
|
||||
`(progn
|
||||
(start ,tag ,attrs)
|
||||
,@body
|
||||
(end ,tag t)))
|
||||
|
||||
(defun terminal (tag attrs val)
|
||||
(start tag attrs)
|
||||
(put-string val)
|
||||
(end tag))
|
||||
|
||||
(defun start (tag &optional attrs)
|
||||
(put-char #\<)
|
||||
(put-string tag)
|
||||
(put-char #\>))
|
||||
|
||||
(defun end (tag &optional newline)
|
||||
(put-string "</")
|
||||
(put-string tag)
|
||||
(put-char #\>)
|
||||
(if newline
|
||||
(put-char #\Newline)))
|
||||
|
||||
;;;; tag-specific renderers
|
||||
|
||||
(defun dl (plist)
|
||||
(write-nested "<dl>" "</dl>"
|
||||
(nested "dl" nil
|
||||
(loop for (key val . r) on plist by #'cddr do
|
||||
(write-simple "<dt>" "</dt>" (string-downcase key))
|
||||
(terminal "dt" nil (string-downcase key))
|
||||
(dd val))))
|
||||
|
||||
(defun dd (v)
|
||||
(if (atom v)
|
||||
(setf v (list v)))
|
||||
(dolist (el v)
|
||||
(write-simple "<dd>" "</dd>" (string el))))
|
||||
(terminal "dd" nil (string el))))
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue