How to convert a string into an HTML resource when using Enlive

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

I am still sort of new to Clojure, so I struggled with this one. Given a string of HTML, how do I turn it into something that html-resource will accept, and then turn into nodes? I had this string:
<select name=”answer” id=”answer-input”><option value=”:two-months”>Two months</option> <option value=”:one-week”>One week</option> <option value=”:three-weeks”>Three weeks</option> <option value=”:one-year”>One year</option> <option value=”:one-days”>One day</option> <option value=”:five-days”>Five days</option> <option value=”:one-month”>One month</option> <option value=”:two-weeks”>Two weeks</option> <option value=”:half-a-year”>Half a year</option> <option value=”:three-months”>Three months</option> <option value=”:four-days”>Four days</option> <option value=”:two-days”>Two days</option> <option value=”:i-live-here”>I live here</option> <option value=”:three-days”>Three days</option> </select>

and I wanted to feed it to enlive/html-resource, so it could become nodes that I could pass to a template (otherwise the HTML gets escaped by Enlive). I ended up doing this:

(:import
(java.net URL)
(java.io ByteArrayInputStream))

and then:

(defn make-select-box-from-answers [answers-as-map]
(let [html-options-as-string (st/join (vals (reduce
(fn [m [k v]] (assoc m k (apply str ““)))
{}
answers-as-map)))
html-select-box-as-string (apply str “select id=’answer-input’ name=’answer’>” (str html-options-as-string) “/select>”)
html-as-byte-buffer (ByteArrayInputStream. (.getBytes html-select-box-as-string “UTF-8”))
html-as-resource-nodes (enlive/html-resource html-as-byte-buffer)
html-select-box-as-nodes (enlive/select html-as-resource-nodes [:#answer-input])]
html-select-box-as-nodes)))

Source