Add hsx/builtin package

This commit is contained in:
paku 2024-05-27 15:51:32 +09:00
parent 145c01a7c4
commit 7a697a1c05
4 changed files with 46 additions and 45 deletions

33
src/builtin.lisp Normal file
View file

@ -0,0 +1,33 @@
(uiop:define-package #:hsx/builtin
(:use #:cl)
(:import-from #:alexandria
#:make-keyword)
(:import-from #:hsx/hsx
#:defhsx))
(in-package #:hsx/builtin)
(defparameter *builtin-elements* (make-hash-table))
(defmacro define-and-export-builtin-elements (&rest names)
`(progn
,@(mapcan (lambda (name)
(list `(defhsx ,name ,(string-downcase name))
`(setf (gethash (make-keyword ',name) *builtin-elements*) t)
`(export ',name)))
names)))
(define-and-export-builtin-elements
; tag-elements
a abbr address area article aside audio b base bdi bdo blockquote
body br button canvas caption cite code col colgroup data datalist
dd del details dfn dialog div dl dt em embed fieldset figcaption
figure footer form h1 h2 h3 h4 h5 h6 head header hr i iframe
img input ins kbd label legend li link main |map| mark meta meter nav
noscript object ol optgroup option output p param picture pre progress
q rp rt ruby s samp script section select small source span strong
style sub summary sup svg table tbody td template textarea tfoot th
thead |time| title tr track u ul var video wbr
; html-tag-element
html
; fragment-element
<>)

View file

@ -1,8 +1,7 @@
(uiop:define-package #:hsx/hsx (uiop:define-package #:hsx/hsx
(:use #:cl) (:use #:cl)
(:import-from #:alexandria (:import-from #:alexandria
#:symbolicate #:symbolicate)
#:make-keyword)
(:import-from #:hsx/element (:import-from #:hsx/element
#:create-element) #:create-element)
(:export #:defhsx (:export #:defhsx
@ -30,32 +29,6 @@
:finally (return (values props nil))) :finally (return (values props nil)))
(values nil body))) (values nil body)))
(defparameter *builtin-elements* (make-hash-table))
(defmacro define-and-export-builtin-elements (&rest names)
`(progn
,@(mapcan (lambda (name)
(list `(defhsx ,name ,(string-downcase name))
`(setf (gethash (make-keyword ',name) *builtin-elements*) t)
`(export ',name)))
names)))
(define-and-export-builtin-elements
; tag-elements
a abbr address area article aside audio b base bdi bdo blockquote
body br button canvas caption cite code col colgroup data datalist
dd del details dfn dialog div dl dt em embed fieldset figcaption
figure footer form h1 h2 h3 h4 h5 h6 head header hr i iframe
img input ins kbd label legend li link main |map| mark meta meter nav
noscript object ol optgroup option output p param picture pre progress
q rp rt ruby s samp script section select small source span strong
style sub summary sup svg table tbody td template textarea tfoot th
thead |time| title tr track u ul var video wbr
; html-tag-element
html
; fragment-element
<>)
(defmacro defcomp (name props &body body) (defmacro defcomp (name props &body body)
(let ((%name (symbolicate '% name))) (let ((%name (symbolicate '% name)))
`(eval-when (:compile-toplevel :load-toplevel :execute) `(eval-when (:compile-toplevel :load-toplevel :execute)
@ -67,26 +40,17 @@
;;;; hsx macro to find hsx symbols ;;;; hsx macro to find hsx symbols
(defmacro hsx (form) (defmacro hsx (form)
(modify-first-of-lists form (find-builtin-symbols form))
#'builtin-element-p
(lambda (node)
(find-symbol (string node) :hsx/hsx))))
(defun modify-first-of-lists (tree test result) (defun find-builtin-symbols (tree)
(if tree (if tree
(cons (let ((first-node (first tree))) (cons (let ((first-node (first tree)))
(cond (if (listp first-node)
((listp first-node) (find-builtin-symbols first-node)
(modify-first-of-lists first-node test result)) (or (find-symbol (string first-node) :hsx/builtin)
((funcall test first-node) first-node)))
(funcall result first-node))
(t first-node)))
(mapcar (lambda (node) (mapcar (lambda (node)
(if (listp node) (if (listp node)
(modify-first-of-lists node test result) (find-builtin-symbols node)
node)) node))
(rest tree))))) (rest tree)))))
(defun builtin-element-p (node)
(and (symbolp node)
(gethash (make-keyword node) *builtin-elements*)))

View file

@ -1,4 +1,7 @@
(defpackage :hsx (defpackage :hsx
(:nicknames #:hsx/main) (:nicknames #:hsx/main)
(:import-from #:hsx/element)
(:import-from #:hsx/hsx)
(:import-from #:hsx/builtin)
(:use #:cl)) (:use #:cl))
(in-package :hsx) (in-package :hsx)

View file

@ -1,7 +1,8 @@
(defpackage #:hsx-test/hsx (defpackage #:hsx-test/hsx
(:use #:cl (:use #:cl
#:fiveam #:fiveam
#:hsx/hsx) #:hsx/hsx
#:hsx/builtin)
(:import-from #:hsx/element (:import-from #:hsx/element
#:create-element)) #:create-element))
(in-package #:hsx-test/hsx) (in-package #:hsx-test/hsx)