Skip to content

Commit

Permalink
added support for PPTX files (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos authored Oct 21, 2018
1 parent 1243b55 commit 47ea515
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
18 changes: 13 additions & 5 deletions src/stencil/process.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@
:type :xml
:executable (:executable m)}))

(defmethod prepare-template :docx [suffix ^InputStream stream]
(defn- prepare-zipped-xml-files [suffix ^InputStream stream]
(assert (some? suffix))
(assert (instance? InputStream stream))
(let [zip-dir (FileHelper/createNonexistentTempFile "stencil-" (str suffix ".zip.contents"))]
(with-open [zip-stream stream] ;; FIXME: maybe not deleted immediately
(ZipHelper/unzipStreamIntoDirectory zip-stream zip-dir))
(let [xml-files (for [w (.list (File. zip-dir "word"))
:when (.endsWith (str w) ".xml")]
(str "word/" w))
(let [files (fn [dir] (for [w (concat (.list (File. zip-dir (str dir))))
:when (.endsWith (str w) ".xml")]
(str dir "/" w)))
xml-files (concat (files "word") (files "ppt/slides"))
execs (zipmap xml-files (map #(->executable (File. zip-dir (str %))) xml-files))]
;; TODO: maybe make it smarter by loading only important xml files
;; such as document.xml and footers/headers
Expand All @@ -54,6 +55,9 @@
:when (:dynamic? v)]
[k (:executable v)]))})))

(defmethod prepare-template :docx [suffix stream] (prepare-zipped-xml-files suffix stream))
(defmethod prepare-template :pptx [suffix stream] (prepare-zipped-xml-files suffix stream))

(defn- run-executable-and-return-writer
"Returns a function that writes output to its output-stream parameter"
[executable function data]
Expand All @@ -68,7 +72,7 @@

(defmulti do-eval-stream (comp :type :template))

(defmethod do-eval-stream :docx [{:keys [template data function]}]
(defn- handle-zipped-xml-files [{:keys [template data function]}]
(assert (:zip-dir template))
(assert (:exec-files template))
(let [data (into {} data)
Expand Down Expand Up @@ -98,6 +102,10 @@
{:stream input-stream
:format :docx}))

(defmethod do-eval-stream :docx [template] (handle-zipped-xml-files template))

(defmethod do-eval-stream :pptx [template] (handle-zipped-xml-files template))

(defmethod do-eval-stream :xml [{:keys [template data function] :as input}]
(assert (:executable template))
(let [data (into {} data)
Expand Down
20 changes: 16 additions & 4 deletions test/stencil/api_test.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
(ns stencil.api-test
(:require [stencil.api :refer :all]))

(comment (def template-1 (prepare "/home/erdos/Joy/stencil/test-resources/test-control-conditionals.docx"))
(comment ;; try to prepare then render a DOCX file

(defn render-template-1 [output-file data]
(render! template-1 data :output output-file))
(def template-1 (prepare "/home/erdos/Joy/stencil/test-resources/test-control-conditionals.docx"))

(render-template-1 "/tmp/output-3.docx" {"customerName" "John Doe"}))
(defn render-template-1 [output-file data]
(render! template-1 data :output output-file))

(render-template-1 "/tmp/output-3.docx" {"customerName" "John Doe"}))

(comment ;; try to prepare then render a PPTX presentation file

(def template-2 (prepare "/home/erdos/example-presentation.pptx"))

(defn render-template-2 [output-file data]
(render! template-2 data :output output-file))

(render-template-2 "/tmp/output-7.pptx"
{"customerName" "John Doe" "x" "XXX" "y" "yyyy"}))

0 comments on commit 47ea515

Please sign in to comment.