Skip to content

Instantly share code, notes, and snippets.

@thheller
Last active August 29, 2015 14:12
(ns m
(:import [java.io StringReader BufferedReader])
(:require [clojure.java.io :as io]
[clojure.string :as str]))
(defmacro example [title & body]
(let [ns-name (.getName *ns*)
file (io/resource (str ns-name ".clj"))
{:keys [line column]} (meta &form)
example-text (->> file
(slurp)
(StringReader.)
(io/reader)
(line-seq)
(drop (inc line))
(take-while #(not (.endsWith % ";; END")))
(map #(subs % column))
(str/join "\n"))]
(println "---")
(println title)
(println "---")
(println example-text)
(println "---")
)
:printed)
;; example must end with ;; END
(example
"A string literal"
(defn a-function [x y]
;; concat x and y as strings
(str x y))
) ;; END
---
A string literal
---
(defn a-function [x y]
;; concat x and y as strings
(str x y))
---
@ska2342
Copy link

ska2342 commented Jan 5, 2015

The first line of the example macrox was just code and not a title, but otherwise a neat solution. Thanks, I'll try something along those lines. Probably putting my code into a dedicated namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment