/
util.clj
33 lines (29 loc) · 1.1 KB
/
util.clj
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
(ns cryogen-core.util
(:require [hiccup.core :as hiccup]))
(defn filter-html-elems
"Recursively walks a sequence of enlive-style html elements depth first
and returns a flat sequence of the elements where (pred elem)"
[pred html-elems]
(reduce (fn [acc {:keys [content] :as elem}]
(into (if (pred elem) (conj acc elem) acc)
(filter-html-elems pred content)))
[] html-elems))
(defn conj-some
"Like conj, but ignores xs that are nil."
([] [])
([coll] coll)
([coll & xs]
(apply conj coll (remove nil? xs))))
(defn enlive->hiccup
"Transform enlive style html to hiccup.
Can take (and return) either a node or a sequence of nodes."
[node-or-nodes]
(cond (string? node-or-nodes) node-or-nodes
(empty? node-or-nodes) nil
(sequential? node-or-nodes) (map enlive->hiccup node-or-nodes)
:else (let [{:keys [tag attrs content]} node-or-nodes]
(conj-some [tag] (not-empty attrs) (enlive->hiccup content)))))
(defn hic=
"Tests whether the xs are equivalent hiccup."
[x & xs]
(apply = (map #(hiccup/html %) (cons x xs))))