1

Clojure and enlive are great. In trying to fathom the power of Enlive I'm attempting to apply two transformations to an html page.

The HTML page has 2 areas (divs) that I want to transform. The first div in question gets cloned ~16 times. The second div in question gets cloned 5 times. The original divs (from the html file) should be overwritten or just not appear at all.

Enlive has the idiomatic approach

(apply str (enlive-html/emit* ze-contant-transferm))

this works beautifully well for one transform.

however, I would like to apply two transforms to the page, so I tried something like:

(str
  (apply str (enlive-html/emit* ze-first-wan))
  (apply str (enlive-html/emit* ze-secand-wan)))

the transformations, done alone, do exactly what I wish: they eat up the original HTML and display the clones that I use for populating with infos.

However, done together in this way, the original html-page divs are preserved, so I end up having the original html file divs along with my clones, and that behavior is no bueno.

Please help.

Thanks-a-much-a.

sova
  • 5,468
  • 10
  • 40
  • 48

1 Answers1

3

Enlive-html provides the do-> function for this purpose.

(defn do->
 "Chains (composes) several transformations. Applies functions from left to right."
 [& fns]
  #(reduce (fn [nodes f] (flatmap f nodes)) (as-nodes %) fns))

Which you can use something like this:

 (apply str (enlive-html/emit* (enlive-html/do-> ze-first-wan ze-second-wan)))
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • thank you. your answer and some help from the #Clojure channel on freenode (thanks noonian!) helped me realize i could simply nest my transformations. the first transformation returns a node-set and the second transformation can use that as its input node-set. voila (= – sova Feb 11 '15 at 23:58