summaryrefslogtreecommitdiff
path: root/src/dompa/nodes.cljc
blob: eef65d3184b776e3fe20804403060688d6a09644 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(ns dompa.nodes
  (:require
   [clojure.zip :as zip]))

(def ^:private default-void-nodes
  #{:!doctype :!DOCTYPE :area :base :br :col :embed :hr :img :input
    :link :meta :source :track :wbr})

(defn- node-attrs-reducer [attrs k v]
  (let [attr-name (-> k name)]
    (if (true? v)
      (str attrs " " attr-name)
      (str attrs " " attr-name "=\"" v "\""))))

(defn- node->html-reducer-fn
  "Returns a reducer function with the initial state of `void-nodes`
  set and `nodes->html-fn` function for recursive HTML creation."
  [void-nodes nodes->html-fn]
  (fn [html node]
    (cond
      ; fragment nodes expand their children to replace themselves
      (and (not (nil? node))
           (= (:node/name node) :<>))
      (str html (nodes->html-fn (:node/children node)))

      ; otherwise business as usual
      (not (nil? node))
      (let [node-name (-> node :node/name name)
            node-attrs (reduce-kv node-attrs-reducer "" (-> node :node/attrs))]
        (cond
          (= (-> node :node/name) :dompa/text)
          (str html (-> node :node/value))

          (contains? void-nodes (-> node :node/name))
          (str html "<" node-name node-attrs ">")

          :else
          (let [value (nodes->html-fn (-> node :node/children))]
            (str html "<" node-name node-attrs ">" value "</" node-name ">"))))

      :else "")))

(defn traverse
  "Recursively traverses given tree of `nodes` with a `traverser-fn`
  that gets a single node passed to it and returns a new updated tree.
  If the traverses function returns `nil`, the node will be removed.
  In any other case the node will be replaced. If you wish to keep
  a node unchanged, just return it as-is."
  [nodes traverser-fn]
  (-> (fn [updated-nodes node]
        (if-let [updated-node (traverser-fn node)]
          (let [children (traverse (-> updated-node :node/children) traverser-fn)]
            (conj updated-nodes (assoc updated-node :node/children children)))
          updated-nodes))
      (reduce [] nodes)))

(defn zip
  "Creates a zipper for given a given `node`."
  [node]
  (zip/zipper
   (fn branch? [node]
     (boolean (seq (:node/children node))))
   (fn children [node]
     (:node/children node))
   (fn make-node [node children]
     (assoc node :node/children children))
   node))

(defn ->html
  "Transform a vector of `nodes` into an HTML string.

  Options:
  - `void-nodes` - A set of node names that are self-closing, defaults to:
    - `:!doctype`
    - `:area`
    - `:base`
    - `:br`
    - `:col`
    - `:embed`
    - `:hr`
    - `:img`
    - `:input`
    - `:link`
    - `:meta`
    - `:source`
    - `:track`
    - `:wbr`
  "
  ([nodes]
   (->html nodes {:void-nodes default-void-nodes}))
  ([nodes {:keys [void-nodes]}]
   (-> (node->html-reducer-fn void-nodes ->html)
       (reduce "" nodes))))

(defmacro defhtml
  "Creates a new function with `name` that outputs HTML.

  Example usage:

  ```clojure
  (defhtml about-page [who]
    ($ :div
      ($ \"hello \" who)))

  (about-page \"world\")
  ```
  "
  [name & args-and-elements]
  (let [[args & elements] args-and-elements]
    `(defn ~name ~args
       (->html (vector ~@elements)))))

(defn $$
  "Creates a new node
  
  Usage:

  ```clojure
  ($ :div
    ($ \"hello world\" ))
  ```"
  [name & opts]
  (if (string? name)
    {:node/name  :dompa/text
     :node/value (apply str name opts)}
    (let [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 (flatten children))))))

(defn- list-of-one?
  [coll]
  (and (sequential? coll)
       (= (count coll) 1)))

(defn- list-of-many?
  [coll]
  (and (sequential? coll)
       (> (count coll) 1)))

(defn- empty-seq?
  [coll]
  (and (sequential? coll)
       (empty? coll)))

(defn- nodes-from-opt
  [opt]
  (cond (map? opt)
        opt

        (empty-seq? opt)
        nil

        (list-of-many? opt)
        {:node/name :<>
         :node/children opt}

        (list-of-one? opt)
        (first opt)

        :else
        {:node/name :dompa/text
         :node/value (str opt)}))

(defn- node-from-opts
  [opts]
  (let [first-opt (first opts)
        second-op (second opts)
        attrs? (and (map? second-op)
                    (not (contains? second-op :node/name)))
        attrs (if attrs? second-op {})
        children-opts (if attrs? (drop 2 opts) (rest opts))
        children-nodes (->> children-opts
                            (map nodes-from-opt)
                            flatten)]
    (cond-> {:node/name first-opt}
      attrs? (assoc :node/attrs attrs)
      (seq children-nodes) (assoc :node/children children-nodes))))

(defn- nodes-from-opts
  [opts]
  (let [nodes (->> (remove nil? opts)
                   (map nodes-from-opt))]
    (if (> (count nodes) 1)
      {:node/name :<>
       :node/children nodes}
      (first nodes))))

(defn $
  "Creates a new node
  
  Usage:

  ```clojure
  ($ :div
    ($ \"hello world\" ))
  ```"
  [& opts]
  (if (keyword? (first opts))
    (node-from-opts opts)
    (nodes-from-opts opts)))