119 lines
3 KiB
Common Lisp
119 lines
3 KiB
Common Lisp
;;;; cl-scopes/web/dom - "Data Output Method" = simple and dedicated HTML generator
|
|
|
|
(defpackage :scopes/web/dom
|
|
(:use :common-lisp)
|
|
(:local-nicknames (:util :scopes/util)
|
|
(:alx :alexandria))
|
|
(:export #:elem #:element #:void-element #:render
|
|
#:dlist))
|
|
|
|
(in-package :scopes/web/dom)
|
|
|
|
;;;; basic definitions
|
|
|
|
(defgeneric put (s)
|
|
(:method ((s string)) (put-string s))
|
|
(:method ((s symbol)) (put (string-downcase s)))
|
|
(:method ((s cons)) (dolist (e s) (put e))))
|
|
|
|
(defclass element ()
|
|
((tag :reader tag :initarg :tag)
|
|
(attrs :reader attrs :initarg :attrs :initform nil)
|
|
(body :reader body :initarg :body :initform nil)))
|
|
|
|
(defun elem (tag &optional attrs body)
|
|
(make-instance 'element :tag tag :attrs attrs :body body))
|
|
|
|
(defun element (tag attrs &rest body)
|
|
(elem tag attrs body))
|
|
|
|
(defmethod print-object ((el element) stream)
|
|
(format stream "<~a ~s>~s" (tag el) (attrs el) (body el)))
|
|
|
|
(defmethod put ((el element))
|
|
(let ((tag (string-downcase (tag el)))
|
|
(body (body el)))
|
|
(start tag (attrs el) :close (not body))
|
|
(when body
|
|
(dolist (c body)
|
|
(put c))
|
|
(end tag))))
|
|
|
|
(defclass void-element (element) ())
|
|
|
|
(defun void-element (tag attrs)
|
|
(make-instance 'void-element :tag tag :attrs attrs))
|
|
|
|
(defmethod put ((el void-element))
|
|
(start (tag el) (attrs el)))
|
|
|
|
(defmacro make-elements (tags &optional (elem-fn 'elem))
|
|
`(progn
|
|
,@(mapcan (lambda (tag) ;`(make-element ,tag)) tags)))
|
|
(list `(defun ,tag (attrs &rest body)
|
|
(funcall #',elem-fn ',tag attrs body))
|
|
`(export ',tag)))
|
|
tags)))
|
|
|
|
(eval-when (:compile-toplevel :load-toplevel :execute)
|
|
(make-elements (div label)))
|
|
|
|
;;;; elements with specific functionality
|
|
|
|
(defun dlist (attrs plist)
|
|
(elem :dl attrs
|
|
(util:loop-plist plist key val append
|
|
(cons (element :dt nil (string-downcase key)) (dds nil val)))))
|
|
|
|
(defun dds (attrs cont)
|
|
(if (atom cont)
|
|
(list (element :dd attrs cont))
|
|
(mapcar #'(lambda (x) (element :dd nil x)) cont)))
|
|
|
|
;;;; rendering
|
|
|
|
(defvar *output* (make-string-output-stream))
|
|
|
|
(defun render (&rest elems)
|
|
(let ((*output* (make-string-output-stream)))
|
|
(put elems)
|
|
(get-output-stream-string *output*)))
|
|
|
|
(defun start (tag attrs &key close)
|
|
(put-char #\<)
|
|
(put-string tag)
|
|
(put-attrs attrs)
|
|
(if close
|
|
(put-char #\/))
|
|
(put-char #\>))
|
|
|
|
(defun end (tag)
|
|
(put-string "</")
|
|
(put-string tag)
|
|
(put-char #\>))
|
|
|
|
(defun put-string (s)
|
|
(write-string s *output*))
|
|
|
|
(defun put-char (c)
|
|
(write-char c *output*))
|
|
|
|
(defun put-attrs (plist)
|
|
;(loop for (key val . r) on plist by #'cddr do
|
|
(util:loop-plist plist key val do
|
|
(put-char #\Space)
|
|
(when val
|
|
(put-string (string-downcase key))
|
|
(when (not (eq val t))
|
|
(put-string "=\"")
|
|
(put-string (attr-str key val))
|
|
(put-char #\")))))
|
|
|
|
(defun attr-str (key val)
|
|
(case key
|
|
((:id :class) (util:to-string val :lower-case t))
|
|
(t (util:to-string val))))
|
|
|
|
(defun newline ()
|
|
(put-char #\Newline))
|
|
|