|
30 | 30 | (nextjournal.clerk/show! \"https://raw.githubusercontent.com/nextjournal/clerk-demo/main/notebooks/rule_30.clj\") |
31 | 31 | (nextjournal.clerk/show! (java.io.StringReader. \";; # Notebook from String 👋\n(+ 41 1)\")) |
32 | 32 | " |
33 | | - [file-or-ns] |
34 | | - (if config/*in-clerk* |
35 | | - ::ignored |
36 | | - (try |
37 | | - (webserver/set-status! {:progress 0 :status "Parsing…"}) |
38 | | - (let [file (cond |
39 | | - (nil? file-or-ns) |
40 | | - (throw (ex-info (str "`nextjournal.clerk/show!` cannot show `nil`.") |
41 | | - {:file-or-ns file-or-ns})) |
42 | | - |
43 | | - (or (symbol? file-or-ns) (instance? clojure.lang.Namespace file-or-ns)) |
44 | | - (or (some (fn [ext] |
45 | | - (io/resource (str (str/replace (namespace-munge file-or-ns) "." "/") ext))) |
46 | | - [".clj" ".cljc"]) |
47 | | - (throw (ex-info (str "`nextjournal.clerk/show!` could not find a resource on the classpath for: `" (pr-str file-or-ns) "`") |
48 | | - {:file-or-ns file-or-ns}))) |
49 | | - |
50 | | - :else |
51 | | - file-or-ns) |
52 | | - doc (try (parser/parse-file {:doc? true} file) |
53 | | - (catch java.io.FileNotFoundException _e |
54 | | - (throw (ex-info (str "`nextjournal.clerk/show!` could not find the file: `" (pr-str file-or-ns) "`") |
55 | | - {:file-or-ns file-or-ns})))) |
56 | | - _ (reset! !last-file file) |
57 | | - {:keys [blob->result]} @webserver/!doc |
58 | | - {:keys [result time-ms]} (eval/time-ms (eval/+eval-results blob->result (assoc doc :set-status-fn webserver/set-status!)))] |
59 | | - (println (str "Clerk evaluated '" file "' in " time-ms "ms.")) |
60 | | - (webserver/update-doc! result)) |
61 | | - (catch Exception e |
62 | | - (webserver/show-error! e) |
63 | | - (throw e))))) |
| 33 | + ([file-or-ns] (show! {} file-or-ns)) |
| 34 | + ([opts file-or-ns] |
| 35 | + (if config/*in-clerk* |
| 36 | + ::ignored |
| 37 | + (try |
| 38 | + (webserver/set-status! {:progress 0 :status "Parsing…"}) |
| 39 | + (let [file (cond |
| 40 | + (nil? file-or-ns) |
| 41 | + (throw (ex-info (str "`nextjournal.clerk/show!` cannot show `nil`.") |
| 42 | + {:file-or-ns file-or-ns})) |
| 43 | + |
| 44 | + (or (symbol? file-or-ns) (instance? clojure.lang.Namespace file-or-ns)) |
| 45 | + (or (some (fn [ext] |
| 46 | + (io/resource (str (str/replace (namespace-munge file-or-ns) "." "/") ext))) |
| 47 | + [".clj" ".cljc"]) |
| 48 | + (throw (ex-info (str "`nextjournal.clerk/show!` could not find a resource on the classpath for: `" (pr-str file-or-ns) "`") |
| 49 | + {:file-or-ns file-or-ns}))) |
| 50 | + |
| 51 | + :else |
| 52 | + file-or-ns) |
| 53 | + doc (try (merge opts |
| 54 | + {:nav-path (webserver/->nav-path file-or-ns)} |
| 55 | + (parser/parse-file {:doc? true} file)) |
| 56 | + (catch java.io.FileNotFoundException _e |
| 57 | + (throw (ex-info (str "`nextjournal.clerk/show!` could not find the file: `" (pr-str file-or-ns) "`") |
| 58 | + {:file-or-ns file-or-ns}))) |
| 59 | + (catch Exception e |
| 60 | + (throw (ex-info (str "`nextjournal.clerk/show!` could not not parse the file: `" (pr-str file-or-ns) "`") |
| 61 | + {::doc {:file file-or-ns}} |
| 62 | + e)))) |
| 63 | + _ (reset! !last-file file) |
| 64 | + {:keys [blob->result]} @webserver/!doc |
| 65 | + {:keys [result time-ms]} (try (eval/time-ms (eval/+eval-results blob->result (assoc doc :set-status-fn webserver/set-status!))) |
| 66 | + (catch Exception e |
| 67 | + (throw (ex-info (str "`nextjournal.clerk/show!` encountered an eval error with: `" (pr-str file-or-ns) "`") {::doc doc} e))))] |
| 68 | + (println (str "Clerk evaluated '" file "' in " time-ms "ms.")) |
| 69 | + (webserver/update-doc! result)) |
| 70 | + (catch Exception e |
| 71 | + (webserver/update-doc! (assoc (-> e ex-data ::doc) :error e)) |
| 72 | + (throw e)))))) |
64 | 73 |
|
65 | 74 | #_(show! "notebooks/exec_status.clj") |
66 | 75 | #_(clear-cache!) |
|
373 | 382 | "Experimental notebook viewer. You probably should not use this." |
374 | 383 | (partial with-viewer (:name v/notebook-viewer))) |
375 | 384 |
|
376 | | -(defn doc-url [path] (v/doc-url path)) |
| 385 | +(defn doc-url [& args] (apply v/doc-url args)) |
377 | 386 |
|
378 | 387 | (defmacro example |
379 | 388 | "Evaluates the expressions in `body` showing code next to results in Clerk. |
|
0 commit comments