Skip to content

Commit

Permalink
convert clerk_utils.clj to cljc (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
sritchie committed Apr 18, 2023
1 parent 9da2761 commit 696a1e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## [0.5.0]

- #29 converts `mentat.clerk-utils` to `cljc`. The CLJS side doesn't do
anything, but this allows the require and macro calls to live inside `cljc`
files.

- #28:

- Removes the `babashka` dependency from `mentat.clerk-utils.build`
Expand Down
45 changes: 27 additions & 18 deletions src/mentat/clerk_utils.clj → src/mentat/clerk_utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@
Expect this namespace to fission off into more specific namespaces as the
catalogue of functions grows."
(:import (java.io FileNotFoundException)))

(defn- ns-present?
"Returns true if `sym` corresponds to a namespace present on the classpath and
#?(:clj
(:import (java.io FileNotFoundException)))
#?(:cljs
(:require-macros [mentat.clerk-utils])))

#?(:clj
(defn- ns-present?
"Returns true if `sym` corresponds to a namespace present on the classpath and
available for loading, false otherwise.
NOTE that calling [[ns-present?]] will `require` the namespace as a
side-effect."
[sym]
(try (nil? (require sym))
(catch FileNotFoundException _ false)))
[sym]
(try (nil? (require sym))
(catch FileNotFoundException _ false))))

(defn- try-resolve
"Takes a namespaced symbol `sym` and delegates to
#?(:clj
(defn- try-resolve
"Takes a namespaced symbol `sym` and delegates to
`clojure.core/requiring-resolve` if its namespace is present on the classpath,
returns `nil` otherwise.
NOTE that calling [[try-resolve]] will `require` the `sym`'s namespace as a
side-effect."
[sym]
(try (requiring-resolve sym)
(catch FileNotFoundException _ nil)))
[sym]
(try (requiring-resolve sym)
(catch FileNotFoundException _ nil))))

;; ## Visibility Macros

Expand All @@ -37,8 +42,10 @@
Like `comment`, body must still be written in well-formed Clojure that can be
parsed by the reader."
[& body]
(when (ns-present? 'nextjournal.clerk)
`(do ~@body)))
(when-not (:ns &env)
(when #?(:clj (ns-present? 'nextjournal.clerk)
:cljs false)
`(do ~@body))))

(defmacro ->clerk-only
"If `nextjournal.clerk` is present on the classpath AND Clerk is evaluating
Expand All @@ -48,7 +55,9 @@
Use this macro to supply examples and forms that should render in a Clerk
environment but not affect normal Clojure evaluation in any way."
([& body]
(when (some-> (try-resolve
'nextjournal.clerk.config/*in-clerk*)
deref)
`(do ~@body))))
(when-not (:ns &env)
(when #?(:clj (some-> (try-resolve
'nextjournal.clerk.config/*in-clerk*)
deref)
:cljs false)
`(do ~@body)))))

0 comments on commit 696a1e8

Please sign in to comment.