Created
April 7, 2013 07:12
-
-
Save dbushenko/5329405 to your computer and use it in GitHub Desktop.
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
(ns laser-test.core | |
(:require [me.raynes.laser :as l] | |
[clojure.java.io :refer [file]] | |
[hickory.zip :refer [hickory-zip]])) | |
(def html (l/parse (file "test1.html"))) | |
(println | |
(l/document html) | |
) | |
(println | |
(l/document html | |
(l/element= :p) (l/content "Hi, I'm a paragraph")) | |
) | |
(println | |
(l/document html | |
(l/element= :p) | |
(l/content (l/node :a | |
:attrs {:href "http://ya.ru"} | |
:content "Link"))) | |
) | |
;; Selectors are just functions that take a location in the tree, do something with it, and return truthy or falsey. | |
#_(defn element= [element] | |
(fn [loc] | |
(= element (-> loc | |
clojure.zip/node | |
:tag)))) | |
(let [selector (l/element= :a)] | |
(selector (hickory-zip {:type :element :tag :a}))) | |
(let [selector (l/element= :a)] | |
(selector (hickory-zip {:type :element :tag :div}))) | |
;; attr= | |
(println | |
(l/document html | |
(l/attr= :href "http://google.com") | |
(l/content "New content")) | |
) | |
;; class= | |
(println | |
(l/document html | |
(l/class= "cl2") | |
(l/content "Contains class cl2")) | |
) | |
;; id= | |
(println | |
(l/document html | |
(l/id= "goog_id") | |
(l/content "Found goog_id")) | |
) | |
;; and | |
(println | |
(l/document html | |
(l/and (l/element= :a) | |
(l/class= "cl2") | |
(l/class= "cl3")) | |
(l/content "And tested")) | |
) | |
;; or | |
(println | |
(l/document html | |
(l/or (l/class= "cl3") | |
(l/class= "cl4")) | |
(l/content "Or tested")) | |
) | |
;; sequence | |
(println | |
(l/document html | |
(l/class= "cl3") (l/content "Cl3 found") | |
(l/class= "cl4") (l/content "Cl4 found")) | |
) | |
;; Transformers are even simpler than selectors! They are just functions that take a node (a map, just like we showed in the node section above) and return a node, a seq of nodes, a string, or nil. | |
#_(defn attr [attr value] | |
(fn [node] (assoc-in node [:attrs attr] value))) | |
;; attr | |
(println | |
(l/document html | |
(l/class= "cl3") | |
(l/attr :id "new_id")) | |
) | |
;; add-class | |
(println | |
(l/document html | |
(l/class= "cl3") | |
(l/add-class "cl4")) | |
) | |
;; remove-class | |
(println | |
(l/document html | |
(l/class= "cl3") | |
(l/remove-class "cl1")) | |
) | |
;; content | |
(println | |
(l/document html | |
(l/class= "cl3") | |
(l/content "<pre></pre>")) | |
) | |
;; unescaped | |
(println | |
(l/document html | |
(l/class= "cl3") | |
(l/content (l/unescaped "<pre></pre>"))) | |
) | |
(def node1 (l/node :a | |
:attrs {:href "http://ya.ru"} | |
:content "Link")) | |
;; insert :left | |
(println | |
(l/document html | |
(l/class= "cl3") | |
(l/insert :left | |
node1)) | |
) | |
;; insert :right | |
(println | |
(l/document html | |
(l/class= "cl3") | |
(l/insert :right | |
node1)) | |
) | |
;; remove | |
(println | |
(l/document html | |
(l/class= "cl3") | |
(l/remove)) | |
) | |
;; sequence | |
;; | |
(def links ["https://lurkmore.to" | |
"https://bash.im" | |
"https://ithappens.ru"]) | |
(println | |
(l/document html | |
(l/class= "cl3") | |
#(for [link links] | |
(-> % | |
(assoc-in [:attrs :href] link) | |
(assoc :content [link])))) | |
) | |
(println | |
(l/document html | |
(l/element= "ul") | |
(l/content | |
(l/unescaped | |
(reduce str | |
(for [link links] | |
(str "\n<li>" link "</li>")))))) | |
) | |
;; composing transformets | |
(println | |
(l/document html | |
(l/class= "cl3") | |
(comp (l/attr :id "new_id") | |
(l/content "Composed function"))) | |
) | |
;; defdocument | |
(l/defdocument test1-doc (file "test1.html") [content] | |
(l/element= :ul) (l/content content)) | |
(println | |
(test1-doc "Hello!!") | |
) | |
;; optional vector of bindings | |
(l/defdocument test2-doc (file "test1.html") [content] | |
[new-content (str "**" content "**")] | |
(l/element= :ul) (l/content new-content)) | |
(println | |
(test2-doc "Hello!!") | |
) | |
;; deffragment | |
(l/defragment frag (file "fragment.html") [content] | |
(l/element= :li) (l/content content)) | |
(println | |
(frag "Hello") | |
) | |
(println | |
(test1-doc (frag "abc")) | |
) | |
(println | |
(test1-doc [(frag "abc") (frag "def")]) | |
) | |
(println | |
(test1-doc | |
(for [link links] | |
(frag link))) | |
) | |
;; select | |
;; Select nodes that match one of the selectors. | |
(def goog | |
(first | |
(l/select html | |
(l/and | |
(l/element= :a) | |
(l/id= "goog_id"))))) | |
(l/defragment frag2 goog [content] | |
(l/element= :a) (comp | |
(l/content content) | |
(l/id ""))) | |
(println | |
(test1-doc | |
(for [link links] | |
(-> link | |
frag2 | |
frag))) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment