Need help with laser?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

Raynes
123 Stars 16 Forks 248 Commits 6 Opened issues

Description

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

Services available

!
?

Need anything else?

Contributors list

# 71,206
Clojure
Shell
Erlang
Elixir
205 commits
# 63,030
Shell
Clojure
webfram...
Node.js
6 commits
# 106,820
Clojure
quine
TeX
repl
3 commits
# 76,510
Elixir
Erlang
css-sel...
touch-d...
3 commits
# 186,005
Shell
C++
Clojure
dns
2 commits
# 749,105
Clojure
2 commits
# 70,551
Ruby
session...
Clojure
Shell
2 commits
# 485,579
Clojure
HTML
1 commit
# 117,385
Clojure
pelican
opencl
Git
1 commit
# 328,519
Shell
HTML
Clojure
1 commit
# 620,299
Clojure
Erlang
Shell
1 commit

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:

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

Let's get that in a Clojure string.

(def html "

foo

bar

baz

")

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"))
"

omg

omg

omg

"

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"))
"

foo

bar

omg

"

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"))
"

foo

omg

baz

"

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"))
"

foo

omg

omg

"

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.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.