From 8c539dc87938934c82b96b1d274b02c57325ac24 Mon Sep 17 00:00:00 2001 From: paku Date: Thu, 3 Oct 2024 10:25:30 +0900 Subject: [PATCH] Merge hsx/group into hsx/utils --- hsx-test.asd | 1 - src/element.lisp | 13 ++++++++++--- src/group.lisp | 30 ------------------------------ src/utils.lisp | 22 ++++++++++++++++++++-- tests/group.lisp | 14 -------------- tests/utils.lisp | 16 ++++++++++++++++ 6 files changed, 46 insertions(+), 50 deletions(-) delete mode 100644 src/group.lisp delete mode 100644 tests/group.lisp diff --git a/hsx-test.asd b/hsx-test.asd index 9dd4f5a..0b30e8f 100644 --- a/hsx-test.asd +++ b/hsx-test.asd @@ -4,6 +4,5 @@ :depends-on ("rove" "hsx-test/utils" "hsx-test/element" - "hsx-test/group" "hsx-test/hsx") :perform (test-op (o c) (symbol-call :rove :run c :style :dot))) diff --git a/src/element.lisp b/src/element.lisp index 955d2a5..1656253 100644 --- a/src/element.lisp +++ b/src/element.lisp @@ -1,12 +1,10 @@ (defpackage #:hsx/element (:use #:cl) (:import-from #:hsx/utils + #:defgroup #:escape-html-attribute #:escape-html-text-content #:minify) - (:import-from #:hsx/group - #:self-closing-tag-p - #:non-escaping-tag-p) (:export #:element #:tag #:html-tag @@ -22,6 +20,15 @@ #:render-to-string)) (in-package #:hsx/element) +;;; tag group definitions + +(defgroup self-closing-tag + area base br col embed hr img input + link meta param source track wbr) + +(defgroup non-escaping-tag + script style) + ;;;; class definitions (defclass element () diff --git a/src/group.lisp b/src/group.lisp deleted file mode 100644 index 039b0c8..0000000 --- a/src/group.lisp +++ /dev/null @@ -1,30 +0,0 @@ -(defpackage #:hsx/group - (:use #:cl) - (:import-from #:alexandria - #:make-keyword - #:symbolicate) - (:export #:defgroup - #:self-closing-tag-p - #:non-escaping-tag-p)) -(in-package #:hsx/group) - -(defun make-keyword-hash-table (symbols) - (let ((ht (make-hash-table))) - (mapcar (lambda (sym) - (setf (gethash (make-keyword sym) ht) t)) - symbols) - ht)) - -(defmacro defgroup (name &body symbols) - (let ((p-name (symbolicate '* name '*))) - `(progn - (defparameter ,p-name (make-keyword-hash-table ',symbols)) - (defun ,(symbolicate name '-p) (keyword) - (gethash keyword ,p-name))))) - -(defgroup self-closing-tag - area base br col embed hr img input - link meta param source track wbr) - -(defgroup non-escaping-tag - script style) diff --git a/src/utils.lisp b/src/utils.lisp index 4678a1d..31c93f8 100644 --- a/src/utils.lisp +++ b/src/utils.lisp @@ -1,10 +1,13 @@ (defpackage #:hsx/utils (:use #:cl) (:import-from #:alexandria - #:alist-hash-table) + #:alist-hash-table + #:make-keyword + #:symbolicate) (:export #:escape-html-attribute #:escape-html-text-content - #:minify)) + #:minify + #:defgroup)) (in-package #:hsx/utils) (defparameter *text-content-escape-map* @@ -55,3 +58,18 @@ (defun whitespace-p (char) (member char '(#\Space #\Newline #\Tab #\Return) :test #'char=)) + +(defun make-keyword-hash-table (symbols) + (let ((ht (make-hash-table))) + (mapcar (lambda (sym) + (setf (gethash (make-keyword sym) ht) t)) + symbols) + ht)) + +(defmacro defgroup (name &body symbols) + (let ((param-name (symbolicate '* name '*)) + (pred-name (symbolicate name '-p))) + `(progn + (defparameter ,param-name (make-keyword-hash-table ',symbols)) + (defun ,pred-name (keyword) + (gethash keyword ,param-name))))) diff --git a/tests/group.lisp b/tests/group.lisp deleted file mode 100644 index 56e7c99..0000000 --- a/tests/group.lisp +++ /dev/null @@ -1,14 +0,0 @@ -(defpackage #:hsx-test/group - (:use #:cl - #:rove - #:hsx/group)) -(in-package #:hsx-test/group) - -(defgroup fruit - apple banana orange) - -(deftest group-test - (testing "defgroup" - (ok (hash-table-p *fruit*)) - (ok (fruit-p :apple)) - (ng (fruit-p :tomato)))) diff --git a/tests/utils.lisp b/tests/utils.lisp index d9e8c3d..2997521 100644 --- a/tests/utils.lisp +++ b/tests/utils.lisp @@ -21,3 +21,19 @@ toggle() { this.open = ! this.open }, }") "{ open: false, get isOpen() { return this.open }, toggle() { this.open = ! this.open }, }")))) + +(defgroup fruit + apple banana) + +(deftest group-util-test + (testing "defgroup" + (ok (expands '(defgroup fruit apple banana) + '(progn + (defparameter *fruit* + (hsx/utils::make-keyword-hash-table '(apple banana))) + (defun fruit-p (keyword) + (gethash keyword *fruit*))))) + (ok (hash-table-p *fruit*)) + (ok (fboundp 'fruit-p)) + (ok (fruit-p :apple)) + (ng (fruit-p :tomato))))