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) (deftest test-message (client)
(let ((msg (message:create '(:test :data :field :info) :data '(:info "test data")))) (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 ,@body
(get-output-stream-string *output*))) (get-output-stream-string *output*)))
(defmacro write-nested (start end &body body) (defmacro put-string (s)
`(progn `(write-string ,s *output*))
(write-string ,start *output*)
,@body
(write-line ,end *output*)))
(defun write-simple (start end val) (defmacro put-char (c)
(write-string start *output*) `(write-char ,c *output*))
(write-string val *output*)
(write-string end *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 ;;;; tag-specific renderers
(defun dl (plist) (defun dl (plist)
(write-nested "<dl>" "</dl>" (nested "dl" nil
(loop for (key val . r) on plist by #'cddr do (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)))) (dd val))))
(defun dd (v) (defun dd (v)
(if (atom v) (if (atom v)
(setf v (list v))) (setf v (list v)))
(dolist (el v) (dolist (el v)
(write-simple "<dd>" "</dd>" (string el)))) (terminal "dd" nil (string el))))