Enlive selectors: documented!

dsl,enlive — cgrand, 5 June 2009 @ 10 h 53 min

Selectors syntax

The Need for More Lack of Understanding

dsl,enlive,moustache — cgrand, 17 May 2009 @ 10 h 25 min

A recent post by Gilad Bracha echoed with my experience designing two small internal DSL in Clojure (Moustache and Enlive’s selectors).

It’s not the same kind of non-understanding which I have in mind. I’m talking about a macro/DSL being able to not understand what is passed to it.

I think it’s an important feature for the user to be able to get out of your DSL and back in regular Clojure.

In both Moustache and Enlive, I avoid to give a special meaning to lists (in Clojure you also have vectors, maps and sets), hence lists always denote user-code and are embedded as-is in the macro expansion. (If I really had to use lists for a DSL, I would check a list doesn’t start with a special value (eg do or unquote (~) — thanks to MB’s comment) before processing it).

That’s why in Enlive you can easily add your own selectors step [:p (my-selector-step arg1 arg2)] as long as (my-selector-step arg1 arg2) evaluates to a correct value (here a state machine).

That’s also how Moustache supports wrapping another Ring handler or custom route validation.

Screenscraping with Enlive

enlive — cgrand, 27 April 2009 @ 19 h 02 min

(select (html-resource (java.net.URL. "http://clojure-log.n01se.net/")) [:#main [:a (attr? :href)]]) returns a seq of link nodes.

Sanitizing HTML with Enlive

enlive — cgrand, 22 April 2009 @ 11 h 35 min
net.cgrand.enlive-html=> (sniptest "<div id=user-data>" 
  [:#user-data] (html-content "code injection<script>alert('boo')</script>") 
  [:#user-data (but #{:p :br :a :strong :em})] nil)
"<html><body><div id=\"user-data\">code injection</div></body></html>"

You also need to remove most attributes but it’s just a demo of something that was impossible with the old Enlive.

By the way, the old Enlive is no more. Long live the new Enlive!

Make it work, make it right, make it fast

enlive — cgrand, 18 April 2009 @ 9 h 33 min

Make it work, make it right, make it fast.

Make it work
sort of done: the old Enlive,
Make it right
work in progress, the new Enlive,
Make it fast
once the “right” branch is merged into master.

See the README file to know what’s new and here for an example.

(c) 2024 Clojure and me | powered by WordPress with Barecity