clojureenlive

Multiple transforms on an html page using enlive


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.


Solution

  • 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)))