summaryrefslogtreecommitdiff
path: root/src/dompa/utils.cljc
blob: ea25d681489f13ea2c185e215537426efaebd281 (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
(ns dompa.utils
  (:require [dompa.nodes :as nodes]))

(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)
    {: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?
          {:node/attrs attrs})
        (when-not (empty? 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)))))

(prn (page "world"))

(comment
  ($ :div {:class "test"}
    ($ "asdasd" "asd")))