diff --git a/src/clj_jtwig/core.clj b/src/clj_jtwig/core.clj index 147095f..da17e95 100644 --- a/src/clj_jtwig/core.clj +++ b/src/clj_jtwig/core.clj @@ -1,6 +1,7 @@ (ns clj-jtwig.core "wrapper functions for working with JTwig from clojure" (:import (com.lyncode.jtwig JtwigTemplate JtwigContext JtwigModelMap) + (com.lyncode.jtwig.resource ClasspathJtwigResource) (com.lyncode.jtwig.tree.api Content) (java.io File FileNotFoundException ByteArrayOutputStream) (java.net URL)) @@ -56,9 +57,15 @@ (.compile))) (defn- compile-template-file [^File file] - (->> file - (new JtwigTemplate) - (.compile))) + (if (inside-jar? file) + (->> (.getPath file) + (get-jar-resource-filename) + (new ClasspathJtwigResource) + (new JtwigTemplate) + (.compile)) + (->> file + (new JtwigTemplate) + (.compile)))) (defn- newer? [^File file other-timestamp] (let [file-last-modified (get-file-last-modified file)] diff --git a/src/clj_jtwig/utils.clj b/src/clj_jtwig/utils.clj index 8fe7e66..1a3285c 100644 --- a/src/clj_jtwig/utils.clj +++ b/src/clj_jtwig/utils.clj @@ -9,6 +9,12 @@ ; the path of a file inside a jar looks something like "jar:file:/path/to/file.jar!/path/inside/jar/to/file" (.contains "jar!"))) +(defn get-jar-resource-filename [^String resource-filename] + (let [pos (.indexOf resource-filename "jar!")] + (if-not (= -1 pos) + (subs resource-filename (+ pos 5)) + resource-filename))) + (defn exists? [^File file] (if (inside-jar? file) ;; TODO: can't use File.exists() for this