Created
December 20, 2013 16:53
-
-
Save ryantm/8057775 to your computer and use it in GitHub Desktop.
doc-from-ns-form thanks to Michał Marczyk for his help http://stackoverflow.com/questions/20698628/how-to-parse-metadata-from-clojure-namespace-form
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
(defn doc-from-ns-form | |
"Extract the docstring from a given ns form without evaluating the form. The docstring returned should be the return value of (:doc (meta namespace-symbol)) if the ns-form were to be evaluated." | |
[ns-form] | |
(let [meta-docstring (:doc (meta (second ns-form))) | |
references (next (next ns-form)) | |
docstring (when (string? (first references)) (first references)) | |
references (if docstring (next references) references) | |
attribute-docstring (:doc (when (map? (first references)) (first references)))] | |
(or attribute-docstring docstring meta-docstring))) | |
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
(defn test-doc-from-ns-form-helper | |
[docstring ns-form] | |
(eval ns-form) | |
(is (= docstring | |
(:doc (meta *ns*)) | |
(doc-from-ns-form ns-form)))) | |
(deftest doc-from-ns-form-test | |
(testing "doc-from-ns-form" | |
(test-doc-from-ns-form-helper | |
nil | |
'(ns no-doc-namespace-name)) | |
(test-doc-from-ns-form-helper | |
"Docstring" | |
'(ns regular-doc-namespace-name "Docstring")) | |
(test-doc-from-ns-form-helper | |
"Attribute-Docstring" | |
'(ns attribute-doc-namepsace-name {:doc "Attribute-Docstring"})) | |
(test-doc-from-ns-form-helper | |
"Meta-Docstring" | |
'(ns ^{:doc "Meta-Docstring"} meta-doc-namespace-name)) | |
(test-doc-from-ns-form-helper | |
"Docstring" | |
'(ns ^{:doc "Meta-Docstring"} meta-and-reg-doc-namespace-name "Docstring")) | |
(test-doc-from-ns-form-helper | |
"Attribute-Docstring" | |
'(ns reg-and-attribute-doc-namespace-name "Docstring" {:doc "Attribute-Docstring"})) | |
(test-doc-from-ns-form-helper | |
"Attribute-Docstring" | |
'(ns ^{:doc "Meta-Docstring"} all-doc-namespace-name "Docstring" {:doc "Attribute-Docstring"})))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment