Change type of element-type, add deftag macro

This commit is contained in:
Akira Tempaku 2024-05-28 19:31:50 +09:00
commit 8455ed4553
5 changed files with 48 additions and 56 deletions

View file

@ -1,16 +1,14 @@
(uiop:define-package #:hsx/builtin
(:use #:cl)
(:import-from #:alexandria
#:make-keyword)
(:import-from #:hsx/defhsx
#:defhsx))
#:deftag))
(in-package #:hsx/builtin)
(defmacro define-and-export-builtin-elements (&rest names)
`(eval-when (:compile-toplevel :load-toplevel :execute)
,@(mapcan (lambda (name)
(list `(defhsx ,name ,(string-downcase name))
(list `(deftag ,name)
`(export ',name)))
names)))

View file

@ -1,10 +1,11 @@
(uiop:define-package #:hsx/defhsx
(:use #:cl)
(:import-from #:alexandria
#:make-keyword
#:symbolicate)
(:import-from #:hsx/element
#:create-element)
(:export #:defhsx
(:export #:deftag
#:defcomp))
(in-package #:hsx/defhsx)
@ -26,6 +27,10 @@
:return (values props thing)
:finally (return (values props nil)))
(values nil body)))
(defmacro deftag (name)
`(eval-when (:compile-toplevel :load-toplevel :execute)
(defhsx ,name ,(make-keyword name))))
(defmacro defcomp (name props &body body)
(let ((%name (symbolicate '% name)))

View file

@ -30,13 +30,14 @@
(defclass component-element (element) ())
;;;; constructor
;;;; factory
(defun create-element (type props &rest children)
(let ((elm (make-instance (cond ((functionp type) 'component-element)
((string= type "<>") 'fragment-element)
((string= type "html") 'html-tag-element)
(t 'tag-element))
((eq type :<>) 'fragment-element)
((eq type :html) 'html-tag-element)
((keywordp type) 'tag-element)
(t (error "element-type must be either a keyword or a function.")))
:type type
:props props
:children (flatten children))))
@ -69,18 +70,19 @@
(with-accessors ((type element-type)
(props element-props)
(children element-children)) elm
(if children
(format stream (if (rest children)
"~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_</~a>~:>"
"~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_</~a>~:>")
type
(props->string props)
children
type)
(format stream "<~a~a></~a>"
type
(props->string props)
type))))
(let ((type-str (string-downcase type)))
(if children
(format stream (if (rest children)
"~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_</~a>~:>"
"~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_</~a>~:>")
type-str
(props->string props)
children
type-str)
(format stream "<~a~a></~a>"
type-str
(props->string props)
type-str)))))
(defun props->string (props)
(with-output-to-string (stream)