summaryrefslogtreecommitdiff
path: root/src/dompa/utils.cljc
blob: 74185b5f1b40eb67b2d629d2311bcba794d3b7a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(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- flattench [children]
  (mapcat #(if (sequential? %) (flattench %) [%]) children))

(defmacro $
  {:clj-kondo/lint-as 'clojure.core/list}
  [name & opts]
  `(if (string? ~name)
     {:node/name  :dompa/text
      :node/value (apply str ~name ~opts)}
     (let [name# ~name
           opts-list# (list ~@opts)
           first-opt# (first opts-list#)
           attrs?# (and (map? first-opt#)
                        (not (contains? first-opt# :node/name)))
           attrs# (if attrs?# first-opt# {})
           children# (if attrs?# (rest opts-list#) opts-list#)]
       (merge
         {:node/name name#}
         (when attrs?# {:node/attrs attrs#})
         (when (seq children#) {:node/children (flattench children#)})))))

(defhtml page [who]
  ($ :div {:class "test"}
    ($ "hello world")
    (let [n "who"]
      ($ n))
    (for [x ["1" "2" "3"]]
      ($ x))
    ($ who)
    (mapv #($ %) ["a" "b" "c"])))

(page "world")