Feedback: Nested dom content search replace

30 views
Skip to first unread message

Jacob Emcken

unread,
Apr 1, 2013, 3:50:31 AM4/1/13
to enliv...@googlegroups.com
Hi.

I need something which can manipulate text content of tags, but not all tags (ie. pre and code tags should be unaffected).
Every where I looked for inspiration on the internet I was specifically recommended not to use regex (which was my initial take on the problem) - so after several hours I came up with the following solution using Enlive:

Since I'm quite inexperienced in the land of Clojure, I would appreciate any feedback (like poor design decisions).

And if it is all fine and dandy I hope someone else might find use for it:

(defn filter-tags
  [tag skip-tags]
  (some true? (map #(= % tag) skip-tags)))

(defn replace-html-content
  [l f skip-tags]
  (w/walk #(if (map? %)
             (if (filter-tags (:tag %) skip-tags) %
               (merge % {:content (replace-html-content (:content %) f skip-tags)}))
             (f %))
          identity l))

(defn replace-smilies
  [html]
  (replace-html-content
    (enlive/html-snippet html)
    #(string/replace % ":)" ":-)")
    [:pre :code]))

(comment "Example")

(replace-smilies "<p>Test replace <code>command :)</code></p><p>:)</p>")


Cheers Jacob
Reply all
Reply to author
Forward
0 new messages