Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(ns nsfw.templating | ||
"Dependency management for web templates." | ||
(:require [clojure.zip :as zip])) | ||
|
||
(defn has-tag? | ||
"DFS for tag." | ||
[tag template] | ||
(loop [ptr (zip/vector-zip template)] | ||
(cond | ||
(zip/end? ptr) false | ||
(= tag (zip/node ptr)) true | ||
:else (recur (zip/next ptr))))) | ||
|
||
(def has-html? (partial has-tag? :html)) | ||
|
||
(defn is-stylesheet-link? [node] | ||
(and (coll? node) | ||
(= :link (first node)) | ||
(map? (second node)) | ||
(= "stylesheet" (get (second node) :rel)))) | ||
|
||
(defn is-style-tag? [node] | ||
(and (coll? node) | ||
(= :style (first node)))) | ||
|
||
(defn is-css-tpl? [tpl] | ||
(or (is-stylesheet-link? tpl) | ||
(is-style-tag? tpl))) | ||
|
||
(defn is-css-ptr? [ptr] | ||
(is-css-tpl? (zip/node ptr))) | ||
|
||
(defn is-js-tpl? [node] | ||
(and (coll? node) | ||
(= :script (first node)) | ||
(map? (second node)) | ||
(= "text/javascript" (get (second node) :type)))) | ||
|
||
(defn is-js-ptr? [ptr] | ||
(is-js-tpl? (zip/node ptr))) | ||
|
||
|
||
(defn collect-tags [tag-pred tpl] | ||
(loop [ptr (zip/vector-zip tpl) | ||
css-coll []] | ||
(if (zip/end? ptr) | ||
{:template (zip/root ptr) :css-coll css-coll} | ||
(recur (if (tag-pred ptr) | ||
(zip/next (zip/remove ptr)) | ||
(zip/next ptr)) | ||
(if (tag-pred ptr) | ||
(conj css-coll (zip/node ptr)) | ||
css-coll))))) | ||
|
||
(def collect-css (partial collect-tags is-css-ptr?)) | ||
|
||
(def collect-js (partial collect-tags is-js-ptr?)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(ns nsfw.test.templating | ||
(:use nsfw.templating :reload) | ||
(:use clojure.test) | ||
(:require [clojure.zip :as zip])) | ||
|
||
(deftest test-has-html? | ||
(are [res template] (= res (has-html? template)) | ||
true [:html [:div.foo "bar"]] | ||
true [:div.bar [:html [:head [:title "foobar"]]]] | ||
false [:div [:div [:div]]])) | ||
|
||
(deftest test-is-css-tpl? | ||
(are [res template] (= res (is-css-tpl? template)) | ||
true [:link {:rel "stylesheet"}] | ||
true [:style "p { color: blue; }"] | ||
false [:foo [:link {:rel "stylesheet"}]])) | ||
|
||
(deftest test-is-js-tpl? | ||
(are [res template] (= res (is-js-tpl? template)) | ||
true [:script {:type "text/javascript"}] | ||
false [:script])) | ||
|
||
(deftest test-collect-css | ||
(let [res (collect-css [:html | ||
[:head | ||
[:link {:rel "stylesheet" :href "foo.css"}] | ||
[:link {:rel "stylesheet" :href "bar.css"}]]])] | ||
(is (= [[:link {:rel "stylesheet" :href "foo.css"}] | ||
[:link {:rel "stylesheet" :href "bar.css"}]] | ||
(:css-coll res))))) | ||
|
||
|