diff --git a/src/scicloj/clay/v2/api.clj b/src/scicloj/clay/v2/api.clj index e42617b7..dea6348d 100644 --- a/src/scicloj/clay/v2/api.clj +++ b/src/scicloj/clay/v2/api.clj @@ -1,12 +1,11 @@ (ns scicloj.clay.v2.api (:require - [clojure.string :as string] [clojure.test] [scicloj.clay.v2.config :as config] + [scicloj.clay.v2.files :as files] [scicloj.clay.v2.live-reload :as live-reload] [scicloj.clay.v2.server :as server] [scicloj.clay.v2.make :as make] - [scicloj.kindly.v4.api :as kindly] [scicloj.kindly.v4.kind :as kind])) (defn stop! [] diff --git a/src/scicloj/clay/v2/files.clj b/src/scicloj/clay/v2/files.clj index 3e53c8d8..1fb047cd 100644 --- a/src/scicloj/clay/v2/files.clj +++ b/src/scicloj/clay/v2/files.clj @@ -2,6 +2,9 @@ (:require [clojure.java.io :as io] [babashka.fs :as fs])) +;; will be bound when the notebook is being evaluated +(def ^:dynamic *context* nil) + (def *target-path->files (atom {})) (defn init-target! [target-path] @@ -43,3 +46,15 @@ (get value))] (io/make-parents new-file) [new-file (relative-url target-path new-file)]))))) + +(defn notebook-relative-file! + "When evaluated by Clay as a notebook, + returns a pair of [file relative-path] such that one can write to file, + and include it in a document using the relative path. + When evaluated outside of Clay, returns nil. + `custom-name` will be used as a filename prefix. + `value` is used for de-duplication, only one file is returned per value. + `ext` is the file extension." + [custom-name value ext] + (when *context* + (next-file! *context* custom-name value ext))) diff --git a/src/scicloj/clay/v2/notebook.clj b/src/scicloj/clay/v2/notebook.clj index 6d5f9e79..773422fb 100644 --- a/src/scicloj/clay/v2/notebook.clj +++ b/src/scicloj/clay/v2/notebook.clj @@ -1,6 +1,7 @@ (ns scicloj.clay.v2.notebook (:require [clojure.string :as str] [clojure.pprint :as pp] + [scicloj.clay.v2.files :as files] [scicloj.clay.v2.util.path :as path] [scicloj.clay.v2.item :as item] [scicloj.clay.v2.prepare :as prepare] @@ -430,7 +431,8 @@ (binding [*ns* *ns* *warn-on-reflection* *warn-on-reflection* *unchecked-math* *unchecked-math* - pp/*print-right-margin* pprint-margin] + pp/*print-right-margin* pprint-margin + files/*context* spec] (-> (relevant-notes spec) (complete-notes spec) (with-out-err-captured))))