Skip to content

Commit

Permalink
Merge pull request #28 from retrogradeorbit/issue-24-refactor-path-ca…
Browse files Browse the repository at this point in the history
…pture-to-use-context-thread-local

use thread local context/*path* for file path
  • Loading branch information
retrogradeorbit authored Nov 3, 2019
2 parents a7c51f6 + a5bf5aa commit 8b7851c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 56 deletions.
7 changes: 5 additions & 2 deletions src/bootleg/edn.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
(ns bootleg.edn
(:require [bootleg.file :as file]
[bootleg.context :as context]
[clojure.edn :as edn]))

(defn load-edn [path file]
(-> (file/path-join path file) slurp edn/read-string))
(defn load-edn
([file] (load-edn context/*path* file))
([path file]
(-> (file/path-join path file) slurp edn/read-string)))
11 changes: 7 additions & 4 deletions src/bootleg/glob.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns bootleg.glob
(:require [bootleg.file :as file]
[bootleg.context :as context]
[clojure.string :as string]
[clojure.java.io :as io]))

Expand Down Expand Up @@ -101,9 +102,11 @@
#_ (glob-files "." "./src/../../**/bootleg/*.clj")
#_ (glob-files "." "**/foo/**/*.txt")

(defn glob [path pattern]
(->> (glob-files path pattern)
(map #(.getPath %))
(map (partial file/relativise path))))
(defn glob
([pattern] (glob context/*path* pattern))
([path pattern]
(->> (glob-files path pattern)
(map #(.getPath %))
(map (partial file/relativise path)))))

#_ (glob "." "**/foo/**/*.txt")
33 changes: 18 additions & 15 deletions src/bootleg/hiccup.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[bootleg.context :as context]
[bootleg.glob :as glob]
[sci.core :as sci]
[fipp.edn :refer [pprint]]))
[fipp.edn :refer [pprint]]
[clojure.java.io :as io]))

(defn load-file* [ctx file]
(let [s (slurp file)]
Expand All @@ -26,20 +27,20 @@
:bindings
{
;; file loading
'markdown (markdown/make-markdown-fn path)
'mustache (mustache/make-mustache-fn path)
'slurp #(slurp (file/input-stream path %))
'html (html/make-html-fn path)
'hiccup (partial process-hiccup path)
'markdown markdown/markdown
'mustache mustache/mustache
'slurp #(-> % file/path-relative io/input-stream)
'html html/html
'hiccup process-hiccup
'selmer selmer/selmer

;; vars files
'yaml (partial yaml/load-yaml path)
'json (partial json/load-json path)
'edn (partial edn/load-edn path)
'yaml yaml/load-yaml
'json json/load-json
'edn edn/load-edn

;; directories and filenames
'glob (partial glob/glob path)
'glob glob/glob

;; testing
'is-hiccup? utils/is-hiccup?
Expand All @@ -65,8 +66,10 @@
ctx
(file/path-join path %))))))))

(defn process-hiccup [path file]
(->> file
(file/path-join path)
slurp
(process-hiccup-data path)))
(defn process-hiccup
([file] (process-hiccup context/*path* file))
([path file]
(->> file
(file/path-join path)
slurp
(process-hiccup-data path))))
16 changes: 8 additions & 8 deletions src/bootleg/html.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(ns bootleg.html
(:require [bootleg.file :as file]
[bootleg.utils :as utils]))
[bootleg.utils :as utils]
[clojure.java.io :as io]))

(defn make-html-fn [path]
(fn html [source & options]
(let [flags (into #{} options)
markup (if (:data flags)
source
(slurp (file/input-stream path source)))]
(utils/html-output-to flags markup))))
(defn html [source & options]
(let [flags (into #{} options)
markup (if (:data flags)
source
(slurp (io/input-stream (file/path-relative source))))]
(utils/html-output-to flags markup)))
11 changes: 7 additions & 4 deletions src/bootleg/json.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
(ns bootleg.json
(:require [bootleg.file :as file]
[bootleg.context :as context]
[clojure.data.json :as json]))

(defn load-json [path file]
(-> (file/path-join path file)
slurp
(json/read-str :key-fn keyword)))
(defn load-json
([file] (load-json context/*path* file))
([path file]
(-> (file/path-join path file)
slurp
(json/read-str :key-fn keyword))))
20 changes: 10 additions & 10 deletions src/bootleg/markdown.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(ns bootleg.markdown
(:require [bootleg.file :as file]
[bootleg.utils :as utils]
[markdown.core :as markdown]))
[markdown.core :as markdown]
[clojure.java.io :as io]))

(defn make-markdown-fn [path]
(fn markdown [source & options]
(let [flags (into #{} options)
markup (if (:data flags)
(markdown/md-to-html-string source)
(let [body (java.io.ByteArrayOutputStream.)]
(markdown/md-to-html (file/input-stream path source) body)
(.toString body)))]
(utils/html-output-to flags markup))))
(defn markdown [source & options]
(let [flags (into #{} options)
markup (if (:data flags)
(markdown/md-to-html-string source)
(let [body (java.io.ByteArrayOutputStream.)]
(markdown/md-to-html (io/input-stream (file/path-relative source)) body)
(.toString body)))]
(utils/html-output-to flags markup)))
18 changes: 9 additions & 9 deletions src/bootleg/mustache.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(ns bootleg.mustache
(:require [bootleg.file :as file]
[bootleg.utils :as utils]
[cljstache.core :as mustache]))
[cljstache.core :as mustache]
[clojure.java.io :as io]))

(defn make-mustache-fn [path]
(fn mustache [source vars & options]
(let [flags (into #{} options)
pre-markup (if (:data flags)
source
(slurp (file/input-stream path source)))
markup (mustache/render pre-markup vars)]
(utils/html-output-to flags markup))))
(defn mustache [source vars & options]
(let [flags (into #{} options)
pre-markup (if (:data flags)
source
(slurp (io/input-stream (file/path-relative source))))
markup (mustache/render pre-markup vars)]
(utils/html-output-to flags markup)))
11 changes: 7 additions & 4 deletions src/bootleg/yaml.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
(ns bootleg.yaml
(:require [bootleg.file :as file]
[bootleg.context :as context]
[yaml.core :as yaml]))

(defn load-yaml [path file]
(-> (file/path-join path file)
slurp
(yaml/parse-string :keywords true)))
(defn load-yaml
([file] (load-yaml context/*path* file))
([path file]
(-> (file/path-join path file)
slurp
(yaml/parse-string :keywords true))))

0 comments on commit 8b7851c

Please sign in to comment.