all repos — archive/microformats @ 08f9c2b929f1a19e39a1cda2743da3837f3cc132

Incomplete Clojure microformats library

Add u-* parsing capability

Alan Pearce
commit

08f9c2b929f1a19e39a1cda2743da3837f3cc132

parent

672e6221dfd065bba69d097806f10ec7344b4fb9

2 files changed, 48 insertions(+), 5 deletions(-)

jump to
M src/microformats/parser.cljsrc/microformats/parser.clj
@@ -34,7 +34,7 @@ :attrs
:class split-classes)) -(defn get-property +(defn get-p-property "Get the p-x property value of an element" [el] (case (:tag el)
@@ -45,17 +45,39 @@ :data (-> el :attrs :value)
:input (-> el :attrs :value) (or (first (:content el)) ""))) +(defn get-u-property + "Get the u-x property value of an element" + [el] + (case (:tag el) + :a (-> el :attrs :href) + :area (-> el :attrs :href) + :img (-> el :attrs :src) + :object (-> el :attrs :data) + (get-p-property el))) + (defn parse-p "Parse p-* classes within HTML element." [element] - (let [el (first (html/select element [(html/attr-starts :class "p-")])) - props (into [] ((classes-to-props "p-") (element-to-classes el)))] - (hash-map :properties (apply hash-map (first props) (repeat (count props) (get-property el)))))) + (let [prop (get-p-property element)] + (into {} (r/map #(hash-map % prop) ((classes-to-props "p-") (element-to-classes element)))))) + +(defn parse-u + "Parse u-* classes within HTML element" + [element] + (let [prop (get-u-property element)] + (into {} (r/map #(hash-map % prop) ((classes-to-props "u-") (element-to-classes element)))))) + +(defn parse-children + "Parse element children for microformats" + [element] + (let [el (first (html/select element [(html/union [(html/attr-starts :class "p-") + (html/attr-starts :class "u-")])]))] + (hash-map :properties (merge (parse-p el) (parse-u el))))) (defn parse-h "Parse h-* classes within a HTML document." [html] - (mapv parse-p (html/select html [(html/attr-starts :class "h-")]))) + (mapv parse-children (html/select html [(html/attr-starts :class "h-")]))) (defn parse "Parse a HTML string with microformats"
M test/microformats/parser_test.cljtest/microformats/parser_test.clj
@@ -36,3 +36,24 @@ "<div class=\"h-card\"><br class=\"p-name\"/></div>"
{:items [{:properties {:name ""}}] :rels {}} "<div class=\"h-card\"><hr class=\"p-name\"/></div>"))) + +(deftest parse-u-elements + (testing "Tags should have their values parsed as a u-* value" + (are [ex in] (= ex (parse in)) + {:items [{:properties {:url "http://example.com"}}] :rels {}} + "<div class=\"h-card\"><a class=\"u-url\" href=\"http://example.com\">Awesome example website</a></div>" + + {:items [{:properties {:photo "http://example.com/someimage.png"}}] :rels {}} + "<div class=\"h-card\"><img class=\"u-photo\" src=\"http://example.com/someimage.png\"></div>" + + {:items [{:properties {:photo "http://example.com/someimage.png"}}] :rels {}} + "<map class=\"h-card\"><area class=\"u-photo\" href=\"http://example.com/someimage.png\"></area></map>" + + {:items [{:properties {:photo "http://example.com/someimage.png"}}] :rels {}} + "<div class=\"h-card\"><object class=\"u-photo\" data=\"http://example.com/someimage.png\"></object></div>" + + {:items [{:properties {:photo "http://example.com/someimage.png"}}] :rels {}} + "<div class=\"h-card\"><abbr class=\"u-photo\" title=\"http://example.com/someimage.png\"></abbr></div>" + + {:items [{:properties {:photo "http://example.com/someimage.png"}}] :rels {}} + "<div class=\"h-card\"><data class=\"u-photo\" value=\"http://example.com/someimage.png\"></data></div>")))