This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-19
Channels
- # adventofcode (4)
- # beginners (80)
- # boot (4)
- # cbus (2)
- # cider (62)
- # clara (18)
- # cljs-dev (8)
- # cljsrn (10)
- # clojure (139)
- # clojure-brasil (3)
- # clojure-dev (27)
- # clojure-italy (1)
- # clojure-russia (3)
- # clojure-spec (4)
- # clojure-uk (47)
- # clojurescript (102)
- # core-async (10)
- # cursive (7)
- # datomic (71)
- # emacs (32)
- # fulcro (99)
- # funcool (1)
- # hoplon (3)
- # jobs (1)
- # jobs-discuss (6)
- # jobs_rus (2)
- # leiningen (3)
- # luminus (2)
- # lumo (14)
- # mount (7)
- # off-topic (19)
- # re-frame (25)
- # ring-swagger (4)
- # rum (3)
- # shadow-cljs (142)
- # specter (2)
- # sql (16)
- # timbre (1)
- # vim (3)
Hey guys, I wanted to share how I am finally going about “splicing” some tags in a html tree. I realize I hadn’t properly formulated my problem... Anyway here’s the code:
(require '[com.rpl.specter :as sp])
(require '[com.rpl.specter.zipper :as spz])
(require '[clojure.zip :as zip])
(def tag-to-unwrap :tag-to-unwrap)
(defn tag-to-unwrap? [t] (= (:tag t) tag-to-unwrap))
(defn tag?-to-unwrap? [t] (and (map? t) (tag-to-unwrap? t)))
(def TAGS-TO-UNWRAP (sp/path [spz/XML-ZIP spz/NEXT-WALK
(sp/selected? [spz/NODE tag?-to-unwrap?])]))
(defn unwrap [zipper]
(let [cs (zip/children zipper)
zipper (reduce zip/insert-right zipper (reverse cs))]
(zip/remove zipper)))
(defn splice-doc [doc]
(sp/transform TAGS-TO-UNWRAP unwrap doc))
(splice-doc
{:tag :body
:content ["toto"
{:tag tag-to-unwrap :content ["titi" "tutu"]}
{:tag :div
:content [{:tag tag-to-unwrap :content ["toto in a div"]}]}]})
;=>
{:tag :body, :content ["toto" "titi" "tutu" {:tag :div, :content ["toto in a div"]}]}