Commits

taka2ru committed 12d3fdf

import sources.

  • Participants
  • Parent commits 6310c9c

Comments (0)

Files changed (4)

+# enlive-utils
+
+Utilities for Enlive HTML template system.
+
+## Usage
+
+### Localizing HTML document
+
+transformation "localize-document"
+
+;; define messages (jp.taka2ru/i18n)
+(gen-resource [:message :ja]
+  :hello "Konnichiwa"
+  :good_morning "Ohayo"
+  :price "Korewa %d Yen desu.")
+
+(deftemplate test-page1
+  (java.io.StringReader.
+    (str "<html><body><p message=\"hello\"></p>"
+         "<p message=\"good_morning\"></p></body></html>"))
+  []
+  [:html] (localize-document :message :ja))
+
+=> "<html><body><p>Konnichiwa</p><p>Ohayo</p></body></html>"
+
+### Formatting HTML document
+
+transformation "format-content"
+
+(deftemplate test-page2
+  (java.io.StringReader.
+    "<html><body><p>This is %d dollars.</p></body></html>")
+  [price]
+  [:p] (format-content price))
+
+=> "<html><body><p>This is 5000 dollars.</p></body></html>"
+
+## License
+
+Copyright (C) 2011 Tetsuya Takatsuru
+
+Distributed under the Eclipse Public License, the same as Clojure.
+(defproject jp.taka2ru/enlive-utils "0.0.1-SNAPSHOT"
+  :description "Utilities for Template system Enlive"
+  :dependencies [[org.clojure/clojure "1.2.1"]
+                 [enlive "1.0.0"]
+                 [jp.taka2ru/i18n "0.0.1"]])
+

src/enlive_utils/core.clj

+(ns enlive-utils.core
+  (:use net.cgrand.enlive-html
+        i18n.core))
+
+(defn attr-some [attr f]
+  (pred #(when-let [vs (attr-values % attr)] (some f vs))))
+
+(defn localize-content [resname locale]
+  (fn [nodes]
+    (let [v (some #(resource resname locale %) (attr-values nodes :message))]
+      (assoc nodes :content [v]))))
+
+(defn localize-document [resname locale]
+  (let [reskeys (set (map name (resource-keys resname locale)))]
+    (transformation
+      [(attr-some :message reskeys)]
+      (do-> (localize-content resname locale)
+            (remove-attr :message)))))
+
+(defn format-content [& args]
+  #(let [[old-content] (:content %)]
+     (if (string? old-content)
+       (assoc % :content [(apply format old-content args)]))))
+

test/enlive_utils/test/core.clj

+(ns enlive-utils.test.core
+  (:use [enlive-utils.core]
+        net.cgrand.enlive-html
+        i18n.core)
+  (:use [clojure.test]))
+
+(gen-resource [:message :ja]
+  :hello "Konnichiwa"
+  :good_morning "Ohayo"
+  :price "Korewa %d Yen desu.")
+
+(deftemplate test-page1
+  (java.io.StringReader.
+    (str "<html><body><p message=\"hello\"></p>"
+         "<p message=\"good_morning\"></p></body></html>"))
+  []
+  [:html] (localize-document :message :ja))
+
+(deftest test-localization
+  (is (= "<html><body><p>Konnichiwa</p><p>Ohayo</p></body></html>"
+         (apply str (test-page1)))))
+
+(deftemplate test-page2
+  (java.io.StringReader.
+    "<html><body><p message=\"price\"></p></body></html>")
+  [price]
+  [:html] (localize-document :message :ja)
+  [:p] (format-content price))
+
+(deftest test-formatting
+  (is (= "<html><body><p>Korewa 5000 Yen desu.</p></body></html>"
+         (apply str (test-page2 5000)))))
+