Skip to content

Commit d1fd29e

Browse files
authored
Fix :clojure.core/eval-file metadata when calling show! with ns arg (#684)
Closes #681.
1 parent dbfeef2 commit d1fd29e

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/nextjournal/clerk/analyzer.clj

+18-5
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,17 @@
360360
state
361361
(info-store-keys info)))
362362

363+
(defn extract-file
364+
"Extracts the string file path from the given `resource` to for usage
365+
on the `:clojure.core/eval-file` form meta key."
366+
[^java.net.URL resource]
367+
(case (.getProtocol resource)
368+
"file" (str (.getFile resource))
369+
"jar" (str (.getJarEntry ^java.net.JarURLConnection (.openConnection resource)))))
370+
371+
#_(extract-file (io/resource "clojure/core.clj"))
372+
#_(extract-file (io/resource "nextjournal/clerk.clj"))
373+
363374
(defn analyze-doc
364375
"Goes through `:blocks` of `doc`, reads and analyzes block forms, populates `:->analysis-info`"
365376
([doc]
@@ -380,10 +391,12 @@
380391
form+loc (cond-> form
381392
(instance? clojure.lang.IObj form)
382393
(vary-meta merge (cond-> loc
383-
(:file doc) (assoc :clojure.core/eval-file (str (:file doc))))))
394+
(:file doc) (assoc :clojure.core/eval-file
395+
(str (cond-> (:file doc)
396+
(instance? java.net.URL (:file doc)) extract-file))))))
384397
{:as analyzed :keys [ns-effect?]} (cond-> (analyze form+loc)
385398
(:file doc) (assoc :file (:file doc)))
386-
_ (when ns-effect? ;; needs to run before setting doc `:ns` via `*ns*`
399+
_ (when ns-effect? ;; needs to run before setting doc `:ns` via `*ns*`
387400
(eval form))
388401
block-id (get-block-id !id->count (merge analyzed block))
389402
analyzed (assoc analyzed :id block-id)]
@@ -394,10 +407,10 @@
394407
(update :blocks conj (-> block
395408
(merge (dissoc analyzed :deps :no-cache? :ns-effect?))
396409
(cond->
397-
(parser/ns? form) (assoc :ns? true)
398-
doc? (assoc :text-without-meta (parser/text-with-clerk-metadata-removed text (ns-resolver notebook-ns))))))
410+
(parser/ns? form) (assoc :ns? true)
411+
doc? (assoc :text-without-meta (parser/text-with-clerk-metadata-removed text (ns-resolver notebook-ns))))))
399412
(cond->
400-
(and doc? (not (contains? state :ns)))
413+
(and doc? (not (contains? state :ns)))
401414
(merge (parser/->doc-settings form) {:ns *ns*}))))))
402415

403416
(-> state

test/nextjournal/clerk/eval_test.clj

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns nextjournal.clerk.eval-test
2-
(:require [clojure.java.io :as io]
2+
(:require [babashka.fs :as fs]
3+
[clojure.java.io :as io]
34
[clojure.string :as str]
45
[clojure.test :refer [deftest is testing]]
56
[matcher-combinators.test :refer [match?]]
@@ -267,3 +268,11 @@
267268
(catch Exception _ nil))
268269
(clerk/show! (java.io.StringReader. code))
269270
(is (= result-first-run (get-result)))))))
271+
272+
(deftest file-var-metadata-test
273+
(testing "show with file string arg"
274+
(clerk/show! "test/nextjournal/clerk/fixtures/hello.clj")
275+
(is (fs/exists? (:file (meta (resolve 'nextjournal.clerk.fixtures.hello/answer))))))
276+
(testing "show with ns arg"
277+
(clerk/show! 'nextjournal.clerk.fixtures.hello)
278+
(is (fs/exists? (:file (meta (resolve 'nextjournal.clerk.fixtures.hello/answer)))))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(ns nextjournal.clerk.fixtures.hello
2+
{:nextjournal.clerk/no-cache true})
3+
4+
(def answer 42)

0 commit comments

Comments
 (0)