Skip to content

Instantly share code, notes, and snippets.

@mgaare
Created August 1, 2013 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgaare/0ab86563ecacc76a7ab9 to your computer and use it in GitHub Desktop.
Save mgaare/0ab86563ecacc76a7ab9 to your computer and use it in GitHub Desktop.
Problem with enlive
;; input:
({:tag :tr,
:attrs nil,
:content
("\n "
{:tag :td,
:attrs nil,
:content
("\n "
{:tag :a, :attrs {:href "/view/:id"}, :content ("Name")}
"\n ")}
"\n "
{:tag :td,
:attrs nil,
:content
("\n "
{:tag :a,
:attrs {:href "mailto:john@aol.com"},
:content ("john@aol.com")}
"\n ")}
"\n "
{:tag :td,
:attrs nil,
:content
("\n "
{:tag :a,
:attrs {:href "+12125551234"},
:content ("212-555-1234")}
"\n ")}
"\n "
{:tag :td,
:attrs nil,
:content
("")}
"\n "
{:tag :td, :attrs nil, :content ("42")}
"\n ")})
;; output:
({:tag :tr,
:attrs nil,
:content
("\n "
[:tag :td]
[:attrs nil]
[:content
("\n "
{:tag :a, :attrs {:href "/view/:id"}, :content ("Name")}
"\n ")]
"\n "
{:tag :td,
:attrs nil,
:content
("\n "
{:tag :a,
:attrs {:href "mailto:john@aol.com"},
:content ("john@aol.com")}
"\n ")}
"\n "
[:tag :td]
[:attrs nil]
[:content
("\n "
{:tag :a,
:attrs {:href "+12125551234"},
:content ("212-555-1234")}
"\n ")]
"\n "
{:tag :td,
:attrs nil,
:content
("")}
"\n "
[:tag :td]
[:attrs nil]
[:content ("42")]
"\n ")})
;; relevant code
(defn set-link-and-text
[target text]
#(transform % [:a]
(do->
(content text)
(set-attr :href target))))
(defn test-summary [nodes rec]
(at nodes
[[:td (html/nth-of-type 1)]]
(set-link-and-text
(str "/view/" (:id rec))
(str (:first-name rec) " " (:last-name rec)))
[[:td (html/nth-of-type 2)]]
(set-link-and-text
(str "mailto:" (:email rec))
(str (:email rec)))
[[:td (html/nth-of-type 3)]]
(let [phone (:phone rec)]
(set-link-and-text
(get-phone-dial phone)
(get-phone-display phone)))
[[:td (html/nth-of-type 4)]]
(content "Last activity")
[[:td (html/nth-of-type 5)]]
(content (str (:score rec)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment