/
api.clj
59 lines (49 loc) · 2.02 KB
/
api.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
(ns stencil.api
"A simple public API for document generation from templates."
(:require [clojure.walk :refer [stringify-keys]])
(:import [io.github.erdos.stencil API PreparedTemplate TemplateData]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(defn prepare
"Creates a prepared template instance from an input document."
[input]
(cond
(instance? PreparedTemplate input) input
(nil? input) (throw (ex-info "Template is missing!" {}))
:otherwise (API/prepare (clojure.java.io/file input))))
(defn- make-template-data [x]
(if (map? x)
(TemplateData/fromMap ^java.util.Map (stringify-keys x))
(throw (ex-info (str "Unsupported template data type " (type x) "!")
{:template-data x}))))
(defn render!
"Takes a prepared template instance and renders it.
By default it returns an InputStream of the rendered document.
Options map keys:
- {:output FNAME} renders output to file FNAME (string or File object). Throws exception
if file already exists and :overwrite? option is not set.
- {:output :input-stream} returns an input stream of the result document.
- {:output :reader} returns the input stream reader of the result document."
[template template-data & {:as opts}]
(let [template (prepare template)
template-data (make-template-data template-data)
result (API/render template ^TemplateData template-data)]
(cond
(#{:stream :input-stream} (:output opts))
(.getInputStream result)
(#{:reader} (:output opts))
(new java.io.InputStreamReader (.getInputStream result))
(:output opts)
(let [f (clojure.java.io/file (:output opts))]
(when (.exists f)
(if (:overwrite? opts)
(.delete f)
(throw (ex-info "File already exists! " {:file f}))))
(do (.writeToFile result f)
(str "Written to " f)))
:otherwise
result)))
(defn cleanup! [template]
(assert (instance? PreparedTemplate template))
(doto template
(.cleanup)))