web/dom: basically working - still missing: render attrs

This commit is contained in:
Helmut Merz 2024-07-13 16:22:44 +02:00
parent 98d2246017
commit b81d50cd43
2 changed files with 32 additions and 13 deletions

View file

@ -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>")))

View file

@ -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))))