Creating Hiccup From Code Keeping Formatting and Comments

143 views
Skip to first unread message

Stefan Kamphausen

unread,
Jan 4, 2015, 10:57:14 AM1/4/15
to clo...@googlegroups.com
Hi,


Currently, I am trying to write a presentation using ring and reveal.js.  For the code samples, I'd like to write "real" clojure code, i.e. no strings or the like. 

Then, I want to turn that into a suitable hiccup vector which will create the correct reveal.js/highlight.js syntax. 

I wrote a trvial macro:

(defmacro example [& body]
  `[:pre
    [:code ~(apply str body)]])


The problem is, that it looses all formatting, because the Clojure reader already had its fun with the code. So,

(example
 "A string literal"
 (defn a-function [x y]
   ;; concat x and y as strings
   (str x y)))


macro-expands to

[:pre [:code "A string literal(defn a-function [x y] (str x y))"]]

which is pretty useless.

I already thought about using &form but this too has been read already.


Any ideas?


Kind regards,
stefan

James Reeves

unread,
Jan 4, 2015, 1:59:44 PM1/4/15
to clo...@googlegroups.com
Macros don't have access to formatting. You either need to put it in a string, or reformat the form afterwards.

- James

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sam Ritchie

unread,
Jan 4, 2015, 6:36:49 PM1/4/15
to clo...@googlegroups.com
Hey Stefan,

I wrote something much like this for the Om-Bootstrap doc site at om-bootstrap.herokuapp.com. I ended up putting all code examples into their own files, then slurping those up. The fun thing about the code below is that each example is executable, and actually runs on the om-bootstrap site.

Here's the Om code:

https://github.com/racehub/om-bootstrap/blob/develop/docs/src/cljs/om_bootstrap/docs/example.cljs

Then, I slurp up the code snippets from their respective files with a "slurp-example" macro:

https://github.com/racehub/om-bootstrap/blob/develop/docs/src/clj/om_bootstrap/macros.clj#L5

This lets me define examples like this:

https://github.com/racehub/om-bootstrap/blob/develop/docs/src/cljs/om_bootstrap/docs/components.cljs#L42

using the relative path under "dev/snippets". Here's the code that defines that linked example:

https://github.com/racehub/om-bootstrap/blob/develop/dev/snippets/button/types.cljs

January 4, 2015 at 8:57 AM
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
Sam Ritchie (@sritchie)

Thomas Heller

unread,
Jan 4, 2015, 7:09:24 PM1/4/15
to clo...@googlegroups.com
Here is a crazy idea I had.


Basically it slurps the .clj file of the current namespace, then looks at the form metadata to skip to the line where the (example ...) starts.

It then takes the next row as the title, then reads all rows until it find one that ends in ;; END. All lines are then joined and tada you got your function body with all indentation. If you want to do more advanced parsing you leave the ;; END bit out but I just wanted to test the concept. ;)

Maybe this works for you.

Cheers,
/thomas

Thomas Heller

unread,
Jan 4, 2015, 7:19:36 PM1/4/15
to clo...@googlegroups.com
Oops, simplified a little. We already have access to the title. ;)

Stefan Kamphausen

unread,
Jan 5, 2015, 4:48:49 AM1/5/15
to clo...@googlegroups.com
Hi,


Thanks for your pointers.  That is something to dive into.


Cheers,
stefan
Reply all
Reply to author
Forward
0 new messages