Created
February 5, 2010 16:17
-
-
Save hchbaw/295917 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(in-ns 'user) | |
(require 'clojure.contrib.prxml) | |
(require 'clojure.test) | |
(require 'net.cgrand.enlive-html) | |
(in-ns 'net.cgrand.enlive-html) | |
(use '[clojure.contrib.prxml :only [prxml]]) | |
(use '[clojure.test :only [are is]]) | |
;; select オペレータを試します、それ用のソース | |
(def *hr* (with-in-str | |
(with-out-str | |
(prxml | |
[:html | |
[:head | |
[:title "The title"]] | |
[:body | |
[:h1 {:class "blah"} "HELLOWORLD"] | |
;; | |
[:h1 {:class "title" :name "hello" | |
:hello nil :lang "en-US" | |
:id "hello"} "Hello"] | |
;; | |
[:h1 {:class "blah"} "HELLOWORLD_ODD"] | |
[:p "いろは"] | |
[:p {:name "nioedo"} "にほへと"] | |
[:div | |
[:p "ちりぬるを"]] | |
[:div {:name "chirinuruwo"} | |
[:h1 "わかよ"]] | |
]])) | |
(html-resource *in*))) | |
(are [x y] (= x ((comp text first) (select *hr* y))) | |
"いろは" [:p] | |
"The title" [:title] | |
"HELLOWORLD" [:h1] | |
"HELLOWORLD" [[:h1 (but (attr-has :name "hello"))]] | |
"いろは" [[:p (pred #(= (:tag %) :p))]] | |
"いろは" [:p (text-pred (partial re-find #"^い+"))] | |
) | |
(is (= "chirinuruwo" | |
((comp :name :attrs last) (select *hr* [(has [:h1])])))) | |
(is (= ["HELLOWORLD" "HELLOWORLD_ODD"] | |
(map text (select *hr* [:body :> [:h1 (nth-child 2 1)]])))) | |
(is (= ["HELLOWORLD" "HELLOWORLD_ODD" "わかよ"] | |
(map text (select *hr* [[:h1 (nth-child 2 1)]])))) | |
(are [y] (= "Hello" ((comp text first) (select *hr* y))) ) | |
;; "Hello" を色々な方法で探します | |
(are [y] (= ["Hello" 1] ((juxt (comp text first) count) (select *hr* y))) | |
[(id= "hello")] | |
[(attr? :hello)] | |
[(attr= :class "title")] | |
[[:h1 (attr= :class "title")]] | |
[[:h1 (attr-has :name "hello")]] | |
[[:h1 (attr-starts :class "t")]] | |
[[:h1 (attr-ends :class "le")]] | |
[[:h1 (attr-contains :class "title" :name "hello")]] | |
[[:h1 (attr|= :lang "en")]] | |
[[:h1 (nth-child 2)]] | |
[[:h1 (nth-child 2 2)]] | |
[[:h1 (nth-child 2)]] | |
[[:h1 (nth-child 2 2)]] | |
[[:h1 (nth-last-child 2 -2)]] | |
[[:h1 (nth-last-child 3 0)]] | |
[[:h1 (nth-of-type 2 -2)]] | |
[[:h1 (nth-last-of-type 2 0)]] | |
[[:h1 (left :h1) (nth-child 2 0)]] | |
[[:h1 (right :h1) (nth-child 2)]] | |
[[:h1 (rights :div) (nth-child 2)]] | |
[:body :> [:h1 (nth-child 2)]] | |
[:body :> [:h1 (nth-child 2) any-node]]) | |
(is (= ["いろは" "にほへと"] (map text (select *hr* [[:p (lefts :h1)]])))) | |
(is (= (with-out-str | |
(prxml | |
[:html | |
[:head [:title "Hello World"]] | |
[:body | |
[:h1 {:foo "bar" :bar "foo"} "Hello World"] | |
[:div {:class "content"} | |
[:div | |
[:p "Hello Enlive!"]]]]])) | |
(sniptest (with-out-str | |
(prxml | |
[:html | |
[:head [:title "No title"]] | |
[:body | |
[:h1 {:class "removethis"} "This is H1"] | |
[:div {:class "content"}]]])) | |
[#{:title :h1}] (content "Hello World") | |
[:h1] (remove-attr :class) | |
[:h1] (do-> (set-attr :bar "foo") (set-attr :foo "bar")) | |
[:div.content] (html-content | |
(with-out-str | |
(prxml | |
[:div | |
[:p "Hello Enlive!"]])))))) | |
(in-ns 'net.cgrand.xml) | |
(struct element :k :foo) | |
(in-ns 'net.cgrand.enlive-html) | |
(use '[net.cgrand.xml :only [element]]) | |
(is (= (with-out-str | |
(prxml | |
[:div | |
[:ul {:class "dynamiclisting"} | |
[:li {:class "dynamiclistingitem"} "1"] | |
[:li {:class "dynamiclistingitem"} "2"] | |
[:li {:class "dynamiclistingitem"} "3"]] | |
[:ul {:class "static"} | |
[:li "A"] [:li "B"] [:li "C"]]])) | |
(sniptest (with-out-str | |
(prxml | |
[:div | |
[:ul {:class "dynamiclisting"} | |
[:li {:class "dynamiclistingitem"}]] | |
[:ul {:class "static"} | |
[:li "A"] [:li "B" [:li "C"]]]])) | |
[:ul.dynamiclisting :> :li] (clone-for [x [1 2 3]] | |
(content (str x)))))) | |
(is (= (with-out-str | |
(prxml | |
[:ul | |
[:li [:a {:href "hhref" :class "link"} "sstr"]] | |
[:li [:a {:href "hreff" :class "link"} "strr"]]])) | |
(sniptest (with-out-str (prxml [:ul [:li [:a {:class "link"}]]])) | |
[:ul :> :li] (clone-for | |
[[s h] [["sstr" "hhref"] ["strr" "hreff"]]] | |
[:a] (do-> (content (str s)) | |
(set-attr :href (str h))))))) | |
(is (= (with-out-str | |
(prxml | |
[:html | |
[:head [:title ""] [:style ""]] | |
[:body [:p "Hello World"]]])) | |
(sniptest (with-out-str (prxml [:abracadabra])) | |
[:abracadabra] (substitute (struct element :html)) | |
[:html] (append (struct element :body)) | |
[:body] (after (struct element :head)) | |
[:head] (do-> (append (struct element :title)) | |
(append (struct element :style))) | |
[:html] (move [:body] [:head] after) | |
[:body] (html-content | |
(with-out-str | |
(prxml | |
[:p "Hello World"])))))) | |
(is (= (with-out-str | |
(prxml | |
[:div {:class "wrap"} | |
[:p "texts"]])) | |
(sniptest (with-out-str | |
(prxml | |
[:div {:class "wrap0"} | |
[:div {:class "wrap1"} | |
[:div {:class "wrap2"} | |
[:p "texts"]]]])) | |
[#{:.wrap0 :.wrap1 :.wrap2}] unwrap | |
[:p] (wrap :div {:class "wrap"})))) | |
(defmacro deftemplxml | |
"`deftemplate' with vector XML as the source for." | |
[name args xml & forms] | |
`(deftemplate ~name (with-in-str | |
(with-out-str | |
(prxml ~xml)) | |
(vec (html-resource *in*))) | |
~args ~@forms)) | |
(deftemplxml foot [text] | |
[:html] | |
[:html] (content text)) | |
(are [x y] (= (with-out-str (prxml x)) (apply str y)) | |
[:html "Hello World"] (foot "Hello World") | |
[:html "HELLO WORLD"] (foot "HELLO WORLD")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment