Skip to content

Commit

Permalink
Merge branch 'release/0.3.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsantiago committed Mar 25, 2011
2 parents 307e69d + e8ef61b commit 24b9d2c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ current content.
* `set-attrs` - Adds the map argument to the node's attributes, overwriting
any that are already present.

####Transformer Combinators####

Just as there are selector combinators to create more complicated selectors
from simpler ones, it is also possible to create transformer combinators,
functions that take one or more other transformers and return a new
transformer based on the arguments.

Currently available transformer combinators are

* `accumulate` - Takes any number of transformers as argument and returns a
transformer that performs all of the transformations in order on its argument
node, using the output of the first transformation as the input to the second,
etc.

Performance
-----------
In my testing, Tinsel renders templates exactly as fast as the equivalent
Expand Down Expand Up @@ -299,7 +313,7 @@ Obtaining it

You can add

[tinsel "0.3.1"]
[tinsel "0.3.2"]

to cake's project.clj and run `cake deps`. Similar steps should work on
Leiningen too.
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject tinsel "0.3.1"
(defproject tinsel "0.3.2"
:description "Selector-based templates with Hiccup."
:dependencies [[clojure "1.2.0"]
[hiccup "0.3.4"]
Expand Down
13 changes: 13 additions & 0 deletions src/tinsel/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@
~'~attr-map)
(utils/contents node#))))

;;
;; Transformer Combinators
;;
;; These transformers take other transformers as arguments, returning a
;; compound transformer.

(defn accumulate
"Takes a sequence of transformers as argument and returns a transformer that
applies them all in order to a node, using the output of the previous as
the input to the next."
[& transformers]
(apply comp (reverse transformers)))


;;
;; Compiler
Expand Down
4 changes: 2 additions & 2 deletions src/tinsel/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[form]
(or (symbol? form)
(and (seq? form) ;; Macros return seqs, not lists.
(not= 'quote (first form)))))
(not= `quote (first form)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Adapted from hiccup/core.clj for compatibility.
Expand All @@ -45,7 +45,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn normalize-form
"Given a hiccup form (vector), recursively normalizes it using
"Given a hiccup form, recursively normalizes it using
normalize-element."
[form]
(if (string? form) ; Should we allow more here? Keywords?
Expand Down
20 changes: 20 additions & 0 deletions test/test_core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,26 @@
(is (= "<body><img src=\"http://example.com/img.jpg\" /></body>"
(set-attribute-template4 {:src "http://example.com/img.jpg"}))))

;;
;; Transformer Combinators
;;

(deftemplate accumulate-template1
[[:body [:div]]]
[arg-map]
(tag= :div)
(accumulate
(set-content [:p (:middle arg-map)])
(prepend-content [:p (:first arg-map)])
(append-content [:p (:last arg-map)])
(set-attrs {:id "paras"})))

(deftest test-accumulate-template
(is (= "<body><div id=\"paras\"><p>I'm first!</p><p>I'm the middle!</p><p>I'm last!</p></div></body>"
(accumulate-template1 {:first "I'm first!"
:middle "I'm the middle!"
:last "I'm last!"}))))

;;
;; Miscellaneous
;;
Expand Down

0 comments on commit 24b9d2c

Please sign in to comment.