summaryrefslogtreecommitdiff
path: root/src/dompa/nodes.clj
blob: 53020badfc1f6c38983cc6481f491ce8fafc7e1b (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
(ns dompa.nodes)

(defmacro $
  "Creates a new node. Particularly useful
  where you need compile-time composition over run-time, like when
  combined with the `defhtml` macro to create HTML string outputs.

  Usage:

  ```clojure
  ($ :div
    ($ \"hello world\" ))
  ```"
  [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 children#)))))