web/dom: improvements (but still work in progress)

This commit is contained in:
Helmut Merz 2024-07-13 12:46:23 +02:00
parent 00366e8b13
commit 98d2246017

View file

@ -7,6 +7,8 @@
(in-package :scopes/web/dom)
;;;; basic / common stuff
(defvar *output* nil)
(defmacro render (&body body)
@ -14,19 +16,28 @@
,@body
(get-output-stream-string *output*)))
(defmacro write-nested (start end &body body)
`(progn
(write-string ,start *output*)
,@body
(write-line ,end *output*)))
(defun write-simple (start end val)
(write-string start *output*)
(write-string val *output*)
(write-string end *output*))
;;;; tag-specific renderers
(defun dl (plist)
(write-string "<dl>" *output*)
(loop for r on plist by #'cddr do
(write-string "<dt>" *output*)
(write-string (string-downcase (car r)) *output*)
(write-string "</dt>" *output*)
(dd (cadr r)))
(write-line "</dl>" *output*))
(write-nested "<dl>" "</dl>"
(loop for (key val . r) on plist by #'cddr do
(write-simple "<dt>" "</dt>" (string-downcase key))
(dd val))))
(defun dd (v)
(if (atom v)
(setf v (list v)))
(dolist (el v)
(write-string "<dd>" *output*)
(write-string (string el) *output*)
(write-string "</dd>" *output*)))
(write-simple "<dd>" "</dd>" (string el))))