From cf55a6a6688c54527a3d27a8d6027858ced99257 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Thu, 25 Sep 2025 21:39:10 +0300 Subject: Rename utils to templates, as it is more appropriate. --- deps.edn | 2 +- resources/clj-kondo/config.edn | 4 ++-- resources/clj-kondo/hooks/dompa.clj | 13 +++++----- src/dompa/templates.cljc | 48 +++++++++++++++++++++++++++++++++++++ src/dompa/utils.cljc | 48 ------------------------------------- 5 files changed, 58 insertions(+), 57 deletions(-) create mode 100644 src/dompa/templates.cljc delete mode 100644 src/dompa/utils.cljc diff --git a/deps.edn b/deps.edn index 6e121a7..925ba74 100644 --- a/deps.edn +++ b/deps.edn @@ -1,3 +1,3 @@ {:deps {} - :paths ["src" "resources"] + :paths ["src" "resources/clj-kondo"] :aliases {:dev {:extra-deps {clj-kondo/clj-kondo {:mvn/version "2025.07.28"}}}}} \ No newline at end of file diff --git a/resources/clj-kondo/config.edn b/resources/clj-kondo/config.edn index fa8d235..813976f 100644 --- a/resources/clj-kondo/config.edn +++ b/resources/clj-kondo/config.edn @@ -1,2 +1,2 @@ -{:hooks {:analyze-call {dompa.utils/$ hooks.dompa/$}} - :linters {:dompa.utils/$-arg-validation {:level :warning}}} \ No newline at end of file +{:hooks {:analyze-call {dompa.templates/$ hooks.dompa/$}} + :linters {:dompa.templates/$-arg-validation {:level :warning}}} \ No newline at end of file diff --git a/resources/clj-kondo/hooks/dompa.clj b/resources/clj-kondo/hooks/dompa.clj index fb3b823..0db709f 100644 --- a/resources/clj-kondo/hooks/dompa.clj +++ b/resources/clj-kondo/hooks/dompa.clj @@ -19,8 +19,9 @@ (api/reg-finding! (assoc (meta invalid-arg) :message (str "Invalid argument type. When creating a text node, " - "only literal values (strings, numbers and symbols) are allowed.") - :type :dompa.utils/$-arg-validation))))) + "only literal values (strings, numbers and symbols) " + "are allowed.") + :type :dompa.templates/$-arg-validation))))) ; if the first arg is a keyword, then the second argument can only be ; a sequence or a map. @@ -34,10 +35,10 @@ "the second argument must be a sequence or a map. " "In other words, the second argument must be an attribute map " "or sequence of other nodes created with the $ macro.") - :type :dompa.utils/$-arg-validation)) + :type :dompa.templates/$-arg-validation)) ; if the first arg is a keyword, the second arg is a list, then - ; every arg has to be a list. + ; every arg has to be a list node. (and (api/keyword-node? first-arg) (api/list-node? (first rest-args)) (not (every? #(api/list-node? %) (rest rest-args)))) @@ -47,7 +48,7 @@ (assoc (meta arg) :message (str "Invalid argument type. Argument has to be a $ macro " "or a sequence of $ macros.") - :type :dompa.utils/$-arg-validation)))) + :type :dompa.templates/$-arg-validation)))) ; if the first arg is a keyword, the second arg is a map, then from ; the second forwards everything has to be a list node @@ -59,4 +60,4 @@ :message (str "Invalid argument type. When having a attribute map, " "the rest of the arguments must be a $ macro or a sequence " "of $ macros") - :type :dompa.utils/$-arg-validation))))) + :type :dompa.templates/$-arg-validation))))) diff --git a/src/dompa/templates.cljc b/src/dompa/templates.cljc new file mode 100644 index 0000000..8a7aae9 --- /dev/null +++ b/src/dompa/templates.cljc @@ -0,0 +1,48 @@ +(ns dompa.templates + (:require [dompa.nodes :as nodes])) + +(defmacro defhtml + {:clj-kondo/lint-as 'clojure.core/defn} + [name & args-and-elements] + (let [[args & elements] args-and-elements] + `(defn ~name ~args + (nodes/->html (vector ~@elements))))) + +(defn ->flat-xf [] + (fn [rf] + (letfn [(step [result input] + (if (sequential? input) + (reduce step result input) + (rf result input)))] + (fn + ([] (rf)) + ([result] (rf result)) + ([result input] (step result input)))))) + +(defn ->flat [children] + (into [] (->flat-xf) children)) + +(defmacro $ + [name & opts] + `(if (string? ~name) + {:node/name :dompa/text + :node/value (str ~name ~@opts)} + (let [opts# (list ~@opts) + first-opt# (first opts#) + attrs?# (and (map? first-opt#) + (not (contains? first-opt# :node/name))) + attrs# (if attrs?# first-opt# {}) + children# (if attrs?# (rest opts#) opts#)] + (cond-> {:node/name ~name} + attrs?# (assoc :node/attrs attrs#) + (seq children#) (assoc :node/children (->flat children#)))))) + +(defhtml test-page [] + (let [n 123] + ($ :<> + ($ :div + ($ "hello world" n)) + ($ "hello") + ($ :div)))) + +(test-page) diff --git a/src/dompa/utils.cljc b/src/dompa/utils.cljc deleted file mode 100644 index 0396e8a..0000000 --- a/src/dompa/utils.cljc +++ /dev/null @@ -1,48 +0,0 @@ -(ns dompa.utils - (:require [dompa.nodes :as nodes])) - -(defmacro defhtml - {:clj-kondo/lint-as 'clojure.core/defn} - [name & args-and-elements] - (let [[args & elements] args-and-elements] - `(defn ~name ~args - (nodes/->html (vector ~@elements))))) - -(defn ->flat-xf [] - (fn [rf] - (letfn [(step [result input] - (if (sequential? input) - (reduce step result input) - (rf result input)))] - (fn - ([] (rf)) - ([result] (rf result)) - ([result input] (step result input)))))) - -(defn ->flat [children] - (into [] (->flat-xf) children)) - -(defmacro $ - [name & opts] - `(if (string? ~name) - {:node/name :dompa/text - :node/value (str ~name ~@opts)} - (let [opts# (list ~@opts) - first-opt# (first opts#) - attrs?# (and (map? first-opt#) - (not (contains? first-opt# :node/name))) - attrs# (if attrs?# first-opt# {}) - children# (if attrs?# (rest opts#) opts#)] - (cond-> {:node/name ~name} - attrs?# (assoc :node/attrs attrs#) - (seq children#) (assoc :node/children (->flat children#)))))) - -(defhtml test-page [] - (let [n 123] - ($ :<> - ($ :div - ($ "hello world" n)) - ($ "hello") - ($ :div)))) - -(test-page) -- cgit v1.2.3