Raynes / laser

HTML transformation/templating for people who do that sort of thing and stuff
120 stars 16 forks source link

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.

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.