all repos — archive/microformats @ 11c75c3e47c4afdd419c13e68fa5e0f795daa715

Incomplete Clojure microformats library

Add support for parsing rel attributes

Alan Pearce
commit

11c75c3e47c4afdd419c13e68fa5e0f795daa715

parent

03bc73a64af103b388f50858ad725b9fc6417782

2 files changed, 35 insertions(+), 1 deletion(-)

jump to
M src/microformats/parser.cljsrc/microformats/parser.clj
@@ -32,6 +32,13 @@ :attrs
:class split-ws-attribute)) +(defn element-to-rels + "Get list of rels from an element" + [el] (-> el + :attrs + :rel + split-ws-attribute)) + (defn get-p-value "Get the p-x property value of an element" [el]
@@ -134,8 +141,26 @@ "Parse h-* classes within a HTML document."
[element] (mapv parse-children (html/select element [(html/attr-starts :class "h-")]))) +(defn parse-rel + "Parse rel attributes of an HTML link element" + [element] + (->> element + element-to-rels + (map keyword) + (map #(hash-map % [(-> element :attrs :href)])) + (into {}))) + +(defn select-rels + "Select linking HTML elements with rel attributes" + [html] (html/select html [[#{:a :link} (html/attr? :rel)]])) + +(defn parse-rels + "Parse rel attibutes of a set of HTML link elements" + [elements] + (apply merge-with into (map parse-rel (select-rels elements)))) + (defn parse "Parse a HTML string with microformats" [html] (let [document (html/html-snippet html)] - {:items (parse-h document) :rels {}})) + {:items (parse-h document) :rels (parse-rels document)}))
M test/microformats/parser_test.cljtest/microformats/parser_test.clj
@@ -121,3 +121,12 @@ (testing "Tags with e-* classes should have ther content parsed"
(are [ex in] (= ex (parse-e (first (html-snippet in)))) {:content {:html "Here is a load of <strong>embedded markup</strong>" :value "Here is a load of embedded markup"}} "<div class=\"e-content\">Here is a load of <strong>embedded markup</strong></div>"))) + +(deftest parse-rel-test + (testing "link and a tags with rel attributes should be parsed" + (are [ex in] (= ex (parse-rels (html-snippet in))) + {:author ["http://example.com/a"]} + "<a rel=\"author\" href=\"http://example.com/a\">author a</a>" + + {:author ["http://example.com/a" "http://example.com/b"]} + "<a rel=\"author\" href=\"http://example.com/a\">author a</a><a rel=\"author\" href=\"http://example.com/b\">author b</a>")))