All Projects → Raynes → Laser

Raynes / Laser

HTML transformation/templating for people who do that sort of thing and stuff

Programming Languages

clojure
4091 projects

DEPRECATED laser

WARNING: THIS PROJECT IS NO LONGER MAINTAINED

When I started Laser, Enlive has not been committed to for many, many, many months and pull requests were untended to for literally a year or longer. I was under the impression that the project had begun to wind down and as such developed Laser to be a maintained alternative.

However, since then, Enlive has been updated and improved substantially. Folks are using it successfully and it is much faster than Laser. I could spend time optimizing laser and making it faster, but I think our time is better spent on improving Enlive and building off of it. Not much point in laser if the codebase is ugly and bogged down by optimizations.

I don't know how many people are using laser today. Refheap makes use of it. On my next iteration of refheap development I will be moving to a different templating system. Dunno if it'll be Enlive, but it won't be laser. ;)

Cheers!

Laser Gun

Build Status

API reference

I wrote a fairly large and thorough guide to laser here, but there is also a simple example below to get you started if you like.

Check the changelog for changes between versions.

Laser is an HTML transformation library. I wouldn't call it a templating library, but that's the purpose it'll likely be used for the most and it is well suited to the task.

Laser is similar to Enlive and Tinsel. Like those libraries, the idea is to work with plain HTML with no special markup. You take that plain HTML in and use selectors to select pieces of HTML and transformation functions to transform the HTML the way you want. Laser comes with a bunch of selectors and transformers built in.

Huge props to David Santiago for writing hickory and helping me out with zippers.

WHYYYYYYYYY!?!?!

I wrote laser for a couple of reasons.

  • Enlive does its job and is the precursor to the way laser does things. However, it is very large and (arguably?) complex compared to laser. laser strives to be as simple as possible.
  • Enlive historically used tagsoup and I wanted something backed by jsoup.
  • I prefer function-based selectors rather than faux css selectors. Laser is completely function based and designed to be an abstraction upon which you can build things like css selectors if you want (but they are not necessary at all).
  • Tinsel is really nice, but one of my specific use-cases is a one-off runtime transformation and tinsel isn't designed for that sort of thing (though it could do it).
  • I like writing libraries so I can do really fun and crazy things with them that make me and other people happy. I also get to bitch about having too many things to maintain.

Example

Laser is designed around selectors and transformers and combinators for them. It's pretty easy. Let's put together some HTML to transform:

<html>
  <head></head>
  <body>
    <p>foo</p>
    <p id="hi">bar</p>
    <div>
      <p class="meow">baz</p>
    </div>
  </body>
</html>

Let's get that in a Clojure string.

(def html "<html><head></head><body><p>foo</p><p id=\"hi\">bar</p><div><p class=\"meow\">baz</p></div></body></html>")

Now, let's try some transformations. laser/document takes a parsed document and alternating selector and transformer arguments:

user> (laser/document (laser/parse html) (laser/element= :p) (laser/content "omg"))
"<html><head></head><body><p>omg</p><p id=\"hi\">omg</p><div><p class=\"meow\">omg</p></div></body></html>"

Easy enough, right? This transforms our HTML document to make all p tags have "omg" content. Everybody needs this, right? It's important.

But darn it, we don't want all of our p to be omg. Let's only change the ones with the meow class!

user> (laser/document (laser/parse html) (laser/class= "meow") (laser/content "omg"))
"<html><head></head><body><p>foo</p><p id=\"hi\">bar</p><div><p class=\"meow\">omg</p></div></body></html>"

Great! How about if we only want to transform the one with id "hi"?

user> (laser/document (laser/parse html) (laser/id= "hi") (laser/content "omg"))
"<html><head></head><body><p>foo</p><p id=\"hi\">omg</p><div><p class=\"meow\">baz</p></div></body></html>"

How about we transform the one with id "hi" and the ones with class "meow" at the same time? WILD!

user> (laser/document (laser/parse html)
        (laser/id= "hi")      (laser/content "omg")
        (laser/class= "meow") (laser/content "omg"))
"<html><head></head><body><p>foo</p><p id=\"hi\">omg</p><div><p class=\"meow\">omg</p></div></body></html>"

Ermahgerd.

That's pretty simple, right? Laser can do a lot more than this. Please read the full (and fairly massive) guide to laser at https://github.com/Raynes/laser/blob/master/docs/guide.md

Credits

https://github.com/Raynes/laser/contributors

License

Copyright © 2013 Anthony Grimes

Distributed under the Eclipse Public License, the same as Clojure.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].