Last active
August 29, 2015 14:12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
A string literal | |
--- | |
(defn a-function [x y] | |
;; concat x and y as strings | |
(str x y)) | |
--- |
Fixed. ;)
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
Hmm hehe don't take the title in L16, thats the one we slurped. Take it from the macro arguments so you don't have to unquote.