From 0e43f515a90645ac64fac3b73814eafc4ddeaca0 Mon Sep 17 00:00:00 2001 From: Martin Bilski Date: Tue, 6 Jan 2015 15:43:13 +0100 Subject: [PATCH 1/3] Add support for cljx reader tags #+clj and #+cljs to make it possible to generate marginalia docs in projects using cljx to target Clojure and ClojureScript. --- src/marginalia/parser.clj | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/marginalia/parser.clj b/src/marginalia/parser.clj index 709d5ae..297ffb5 100644 --- a/src/marginalia/parser.clj +++ b/src/marginalia/parser.clj @@ -391,10 +391,17 @@ (defn cljs-file? [filepath] (.endsWith (lower-case filepath) "cljs")) +(defn cljx-file? [filepath] + (.endsWith (lower-case filepath) "cljx")) + +(def cljx-data-readers {'+clj identity + '+cljs identity}) + (defn parse-file [file] - (let [readers (if (cljs-file? file) - (->> default-data-readers (merge *cljs-data-readers*)) - default-data-readers)] + (let [readers (merge {} + (when (cljs-file? file) *cljs-data-readers*) + (when (cljx-file? file) cljx-data-readers) + default-data-readers)] (binding [*data-readers* readers *comments-enabled* (atom true)] (parse (slurp file))))) From 319f26693d4d34901c61ed972f88293bc9e6da7d Mon Sep 17 00:00:00 2001 From: Martin Bilski Date: Tue, 6 Jan 2015 22:22:10 +0100 Subject: [PATCH 2/3] Fix no namespace extracted from .cljx files. --- src/marginalia/core.clj | 16 ++++------------ src/marginalia/parser.clj | 32 +++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/marginalia/core.clj b/src/marginalia/core.clj index 09ffdc2..8622160 100644 --- a/src/marginalia/core.clj +++ b/src/marginalia/core.clj @@ -36,9 +36,8 @@ [clojure.string :as str]) (:use [marginalia [html :only (uberdoc-html index-html single-page-html)] - [parser :only (parse-file)]] + [parser :only (parse-file parse-ns)]] [clojure.tools - [namespace :only (read-file-ns-decl)] [cli :only (cli)]])) @@ -93,7 +92,7 @@ (->> (io/file dir) (file-seq) (filter (partial processable-file? pred)) - (sort-by #(->> % read-file-ns-decl second)) + (sort-by #(->> % parse-ns second)) (map #(.getCanonicalPath %)))) ;; ## Project Info Parsing @@ -170,15 +169,8 @@ :else (recur (merge-line (first lines) cur-group) groups (rest lines))))) (defn path-to-doc [fn] - (let [file (java.io.File. fn) - ns (-> file - (read-file-ns-decl) - (second) - (str)) - ns (if (or (nil? ns) (empty? ns)) (.getName file) ns) - groups (parse-file fn)] - {:ns ns - :groups groups})) + {:ns (parse-ns (java.io.File. fn)) + :groups (parse-file fn)}) ;; ## Output Generation diff --git a/src/marginalia/parser.clj b/src/marginalia/parser.clj index 297ffb5..0721981 100644 --- a/src/marginalia/parser.clj +++ b/src/marginalia/parser.clj @@ -5,7 +5,8 @@ "Provides the parsing facilities for Marginalia." (:refer-clojure :exclude [replace]) (:use [clojure [string :only (join replace lower-case)]] - [cljs.tagged-literals :only [*cljs-data-readers*]])) + [cljs.tagged-literals :only [*cljs-data-readers*]] + [clojure.tools.namespace :only (read-file-ns-decl)])) ;; Extracted from clojure.contrib.reflect @@ -397,11 +398,24 @@ (def cljx-data-readers {'+clj identity '+cljs identity}) -(defn parse-file [file] - (let [readers (merge {} - (when (cljs-file? file) *cljs-data-readers*) - (when (cljx-file? file) cljx-data-readers) - default-data-readers)] - (binding [*data-readers* readers - *comments-enabled* (atom true)] - (parse (slurp file))))) +(defmacro with-readers-for [file & body] + `(let [readers# (merge {} + (when (cljs-file? ~file) *cljs-data-readers*) + (when (cljx-file? ~file) cljx-data-readers) + default-data-readers)] + (binding [*data-readers* readers#] + ~@body))) + +(defn parse-file [fn] + (with-readers-for fn + (binding [*comments-enabled* (atom true)] + (parse (slurp fn))))) + +(defn parse-ns [file] + (let [filename (.getName file)] + (with-readers-for filename + (or (not-empty (-> file + (read-file-ns-decl) + (second) + (str))) + filename)))) \ No newline at end of file From e35c3baeff72cc1d7e122a82584bc5c662f2b9bd Mon Sep 17 00:00:00 2001 From: Martin Bilski Date: Tue, 6 Jan 2015 22:25:40 +0100 Subject: [PATCH 3/3] Fix no newline at the end of parser.clj. --- src/marginalia/parser.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/marginalia/parser.clj b/src/marginalia/parser.clj index 0721981..3283d2d 100644 --- a/src/marginalia/parser.clj +++ b/src/marginalia/parser.clj @@ -418,4 +418,4 @@ (read-file-ns-decl) (second) (str))) - filename)))) \ No newline at end of file + filename))))