From cc4b3e19c68f93b6c7f4c14e2c3b74d043d845ec Mon Sep 17 00:00:00 2001 From: paku Date: Wed, 29 May 2024 10:30:06 +0900 Subject: [PATCH] Rename elm to element, add render method --- src/element.lisp | 55 ++++++++++++++++++++++++++---------------------- src/main.lisp | 13 ++++++++---- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/element.lisp b/src/element.lisp index aab1195..9740380 100644 --- a/src/element.lisp +++ b/src/element.lisp @@ -4,7 +4,8 @@ #:element-type #:element-props #:element-children - #:expand-component)) + #:expand-component + #:render)) (in-package #:hsx/element) @@ -33,16 +34,16 @@ ;;;; factory (defun create-element (type props &rest children) - (let ((elm (make-instance (cond ((functionp type) 'component) - ((eq type :<>) 'fragment) - ((eq type :html) 'html-tag) - ((keywordp type) 'tag) - (t (error "element-type must be either a keyword or a function."))) - :type type - :props props - :children (flatten children)))) - (create-element-hook elm) - elm)) + (let ((element (make-instance (cond ((functionp type) 'component) + ((eq type :<>) 'fragment) + ((eq type :html) 'html-tag) + ((keywordp type) 'tag) + (t (error "element-type must be either a keyword or a function."))) + :type type + :props props + :children (flatten children)))) + (create-element-hook element) + element)) (defun flatten (x) (labels ((rec (x acc) @@ -53,23 +54,23 @@ (rec (cdr x) acc)))))) (rec x nil))) -(defmethod create-element-hook ((elm element))) +(defmethod create-element-hook ((element element))) -(defmethod create-element-hook ((elm fragment)) - (when (element-props elm) +(defmethod create-element-hook ((element fragment)) + (when (element-props element) (error "Cannot pass props to fragment."))) -(defmethod create-element-hook ((elm component)) +(defmethod create-element-hook ((element component)) ;dry-run to validate props - (expand-component elm)) + (expand-component element)) ;;;; methods -(defmethod print-object ((elm tag) stream) +(defmethod print-object ((element tag) stream) (with-accessors ((type element-type) (props element-props) - (children element-children)) elm + (children element-children)) element (let ((type-str (string-downcase type))) (if children (format stream @@ -100,12 +101,12 @@ key-str value)))))) -(defmethod print-object ((elm html-tag) stream) +(defmethod print-object ((element html-tag) stream) (format stream "~%") (call-next-method)) -(defmethod print-object ((elm fragment) stream) - (with-accessors ((children element-children)) elm +(defmethod print-object ((element fragment) stream) + (with-accessors ((children element-children)) element (if children (format stream (if (rest children) @@ -113,16 +114,20 @@ "~<~a~:>") children)))) -(defmethod print-object ((elm component) stream) - (print-object (expand-component elm) stream)) +(defmethod print-object ((element component) stream) + (print-object (expand-component element) stream)) -(defmethod expand-component ((elm component)) +(defmethod expand-component ((element component)) (with-accessors ((type element-type) (props element-props) - (children element-children)) elm + (children element-children)) element (apply type (merge-children-into-props props children)))) (defun merge-children-into-props (props children) (append props (and children (list :children children)))) + +(defmethod render ((element element) &key minify) + (with-output-to-string (stream) + (write element :stream stream :pretty (not minify)))) diff --git a/src/main.lisp b/src/main.lisp index d97b2ff..3500f02 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -1,7 +1,12 @@ -(uiop:define-package :hsx +(defpackage :hsx (:nicknames #:hsx/main) - (:use #:cl) - (:use-reexport #:hsx/defhsx) + (:use #:cl + #:hsx/element + #:hsx/defhsx + #:hsx/hsx) (:import-from #:hsx/builtin) - (:use-reexport #:hsx/hsx)) + (:export #:hsx + #:deftag + #:defcomp + #:render)) (in-package :hsx)