commit 6c14fd9a14f9dc091527a6b52cceafbe0eaeca5f Author: gered Date: Thu Oct 2 18:45:40 2014 -0400 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e91d482 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +.DS_Store +/target +/classes +/checkouts +pom.xml +pom.xml.asc +*.jar +*.class +/.lein-* +/.nrepl-port +.settings/ +.project +.classpath +.idea/ +*.iml +*.ipr +*.iws \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..183be55 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2014, Gered King All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ff64577 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# clj-pebble + +A wrapper for the [Pebble Java Templating Engine](http://www.mitchellbosecke.com/pebble) to make usage in Clojure +applications simple. + +## Usage + +TODO + +## License + +Distributed under the BSD 3-Clause License (the same as Pebble). + +See `LICENSE` for full details. diff --git a/project.clj b/project.clj new file mode 100644 index 0000000..0c5e3bc --- /dev/null +++ b/project.clj @@ -0,0 +1,10 @@ +(defproject clj-pebble "0.1.0-SNAPSHOT" + :description "Clojure wrapper for the Pebble Java templating engine." + :url "https://github.com/gered/clj-pebble" + :license {:name "BSD 3-Clause License" + :url "http://opensource.org/licenses/BSD-3-Clause"} + :dependencies [[org.clojure/clojure "1.6.0"] + [com.mitchellbosecke/pebble "1.0.0"]] + :profiles {:dev {:dependencies [[pjstadig/humane-test-output "0.6.0"]] + :injections [(require 'pjstadig.humane-test-output) + (pjstadig.humane-test-output/activate!)]}}) diff --git a/src/clj_pebble/core.clj b/src/clj_pebble/core.clj new file mode 100644 index 0000000..3acb370 --- /dev/null +++ b/src/clj_pebble/core.clj @@ -0,0 +1,35 @@ +(ns clj-pebble.core + (:import (java.io StringWriter) + (com.mitchellbosecke.pebble PebbleEngine) + (com.mitchellbosecke.pebble.loader DelegatingLoader ClasspathLoader FileLoader StringLoader) + (com.mitchellbosecke.pebble.template PebbleTemplate)) + (:require [clojure.walk :refer [stringify-keys]])) + +(defonce file-loader + (DelegatingLoader. + [(ClasspathLoader.) + (FileLoader.)])) + +(defonce string-loader + (StringLoader.)) + +(defn- make-pebble-engine [] + (PebbleEngine. file-loader)) + +(defonce engine (atom (make-pebble-engine))) + +(defn create-engine! [] + (reset! engine (make-pebble-engine))) + +(defn prepare-context-map [context] + (if context + (stringify-keys context) + {})) + +(defn render [^String template-source & [context]] + (.setLoader @engine string-loader) + (if-let [^PebbleTemplate compiled-template (.getTemplate @engine template-source)] + (let [writer (StringWriter.) + context (prepare-context-map context)] + (.evaluate compiled-template writer context) + (.toString writer)))) diff --git a/test/clj_pebble/core_test.clj b/test/clj_pebble/core_test.clj new file mode 100644 index 0000000..8e8f553 --- /dev/null +++ b/test/clj_pebble/core_test.clj @@ -0,0 +1,7 @@ +(ns clj-pebble.core-test + (:require [clojure.test :refer :all] + [clj-pebble.core :refer :all])) + +(deftest a-test + (testing "FIXME, I fail." + (is (= 0 1))))