From 20fa62ab90ed87e143bf9b140ba4b7708f1e91ee Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Tue, 23 Sep 2025 02:08:12 +0300 Subject: Make nodes have namespaces keys so that we could make `$` helper macro into a function instead. --- src/dompa/utils.cljc | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) (limited to 'src/dompa/utils.cljc') diff --git a/src/dompa/utils.cljc b/src/dompa/utils.cljc index 285566d..ea25d68 100644 --- a/src/dompa/utils.cljc +++ b/src/dompa/utils.cljc @@ -1,34 +1,40 @@ (ns dompa.utils (:require [dompa.nodes :as nodes])) -(defmacro $ [name & opts] +(defmacro defhtml [name & args-and-elements] + (let [[args & elements] args-and-elements] + `(defn ~name ~args + (nodes/->html (vector ~@elements))))) + +(defn $ [name & opts] (if (string? name) - {:name :dompa/text - :value name} - (let [node {:name name} - attrs? (map? (first opts)) + {:node/name :dompa/text + :node/value (str name (apply str opts))} + (let [node {:node/name name} + attrs? (not (get (first opts) :node/name)) attrs (if attrs? (first opts) {}) children (if attrs? (rest opts) opts)] (merge node (when attrs? - {:attrs attrs}) + {:node/attrs attrs}) (when-not (empty? children) - {:children (into [] children)}))))) + {:node/children children}))))) + +(defhtml page + [test] + ($ :!doctype {:html true}) + ($ :html {:lang "en"} + ($ :head + ($ :meta {:charset "utf-8"}) + ($ :link {:rel "stylesheet" :href "style.css"})) + ($ :body + ($ :span {:class "test"}) + ($ :span {:class "test2"} + ($ "hello" test))))) -(defn- page [] - (list - ($ :!doctype {:html true}) - ($ :html {:lang "en"} - ($ :head - ($ :meta {:charset "utf-8"}) - ($ :link {:rel "stylesheet" :href "style.css"})) - ($ :body - ($ :span {:class "test"} - ($ :span {:class "test2"} - ($ "hello, world."))))))) +(prn (page "world")) (comment - ($ "asdasd") - (page) - (nodes/->html (page))) \ No newline at end of file + ($ :div {:class "test"} + ($ "asdasd" "asd"))) \ No newline at end of file -- cgit v1.2.3