-
Notifications
You must be signed in to change notification settings - Fork 11
/
process.clj
73 lines (66 loc) · 2.85 KB
/
process.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(ns stencil.process
"These functions are called from Java."
(:import [java.io InputStream]
[java.util.zip ZipEntry ZipOutputStream]
[io.github.erdos.stencil PrepareOptions]
[io.github.erdos.stencil.impl FileHelper ZipHelper])
(:require [clojure.java.io :as io]
[stencil.log :as log]
[stencil.model :as model]))
(set! *warn-on-reflection* true)
;; merge a set of fragment names under the :fragments key
(defn- merge-fragment-names [model]
(assoc model
:fragments
(-> #{}
(into (-> model :main :executable :fragments))
(into (for [x (:headers+footers (:main model))
f (:fragments (:executable x))] f)))))
(defn- merge-variable-names [model]
(assoc model
:variables
(-> #{}
(into (-> model :main :executable :variables))
(into (for [x (:headers+footers (:main model))
v (:variables (:executable x))] v)))))
;; Called from Java API
(defn prepare-template [^InputStream stream, ^PrepareOptions options]
(assert (instance? InputStream stream))
(let [zip-dir (FileHelper/createNonexistentTempFile
(.getTemporaryDirectoryOverride options)
"stencil-" ".zip.contents")
options {:only-includes (.isOnlyIncludes options)}]
(with-open [zip-stream stream]
(ZipHelper/unzipStreamIntoDirectory zip-stream zip-dir))
(-> zip-dir
(model/load-template-model options)
merge-fragment-names
merge-variable-names)))
;; Called from Java API
(defn prepare-fragment [input, ^PrepareOptions options]
(assert (some? input))
(let [zip-dir (FileHelper/createNonexistentTempFile
(.getTemporaryDirectoryOverride options)
"stencil-fragment-" ".zip.contents")
options {:only-includes (.isOnlyIncludes options)}]
(with-open [zip-stream (io/input-stream input)]
(ZipHelper/unzipStreamIntoDirectory zip-stream zip-dir))
(model/load-fragment-model zip-dir options)))
(defn- render-writers-map [writers-map outstream]
(assert (map? writers-map))
(io!
(with-open [zipstream (new ZipOutputStream outstream)]
(doseq [[k writer] writers-map
:let [rel-path (FileHelper/toUnixSeparatedString (.toPath (io/file k)))
ze (new ZipEntry rel-path)]]
(assert (not (.contains rel-path "../")))
(log/trace "ZIP: writing {}" rel-path)
(.putNextEntry zipstream ze)
(writer zipstream)
(.closeEntry zipstream)))))
;; Called from Java API
(defn eval-template [{:keys [template data function fragments]}]
(assert (:source-folder template))
(let [data (into {} data)
writers-map (model/template-model->writers-map template data function fragments)]
{:writer (partial render-writers-map writers-map)}))