Clojure's pretty printer (clojure.pprint) takes unformatted code like this:
(defn fib ([n] (fib n 1 0)) ([n a b] (if (= n 0) a (fib (dec n) (+ a b) a))))
And makes it nice, like this.
(defn fib
([n] (fib n 1 0))
([n a b]
(if (= n 0)
a
(fib (dec n) (+ a b) a))))
I'd like to put some source in a web page, so I'd like it to be pretty-printed. But I'd also like to wrap each form in a set of < span > tags with a unique ID so I can manipulate the representation with javascript. That is, I want to turn
(foo bar baz)
into
<span id="001">(<span id="002">foo</span> <span id="003">bar</span> <span id="004">baz</span>)</span>
But I still want the resulting forms to be indented like the pretty printer would, so that the code that actually gets displayed looks right.
Some of the documentation for the pretty printer mentions that it can take custom dispatch functions, but I can't find anything about what they do or how to define them. Is it possible to do what I want with such a beast, and if so can someone provide me with some information on how to do it?
There are ways to pretty print XML, as you can see here: https://nakkaya.com/2010/03/27/pretty-printing-xml-with-clojure/
That person used
So if you put your HTMl string (which is not exactly a subset of XML), you would get:
output:
In Clojure, using Compojure, you can build HTML/XML tags in a very lispy syntax. You can use them too:
With the output of:
You see also suggestions here: Compojure HTML Formatting