all repos — archive/microformats @ a4735ff01cdb3ac784820b0510bf07a33a561f1b

Incomplete Clojure microformats library

Allow base-url to be overridden by parse argument

Alan Pearce
commit

a4735ff01cdb3ac784820b0510bf07a33a561f1b

parent

fe2e6c08a4f8d42317fe8e97a62e061209d4ed4d

2 files changed, 25 insertions(+), 3 deletions(-)

jump to
M src/microformats/parser.cljsrc/microformats/parser.clj
@@ -387,8 +387,24 @@ "Parse rel attibutes of a set of HTML link elements"
[locs] (or (apply merge-with into (map parse-rel (select-rels locs))) {})) +(defprotocol node + (to-node [html])) + +(extend-protocol node + String + (to-node [html] + (some->> html str/trim html/html-snippet))) + +(extend-protocol node + clojure.lang.LazySeq + (to-node [html] + html)) + (defn parse "Parse a HTML string with microformats" - [html] - (let [document (some->> html str/trim html/html-snippet with-base-url (map z/xml-zip) first)] - {:items (some-> document walk) :rels (parse-rels document)})) + ([html] + (let [document (some->> html to-node with-base-url)] + (parse document (get-base-url document)))) + ([html base-url] + (let [document (some->> html to-node first (with-base-url base-url) z/xml-zip)] + {:items (some-> document walk) :rels (parse-rels document)})))
M test/microformats/parser_expectations.cljtest/microformats/parser_expectations.clj
@@ -305,6 +305,12 @@ (parse "<html><head><base href=\"http://example.com/\"></head><body>
<div class=\"h-card\"><img class=\"u-photo\" alt=\"Example User\" src=\"me.png\"></div> </body></html>")) +(expect-focused {:items '({:type ("h-card") :properties {:name ("Example User") + :photo ("http://not-example.com/me.png")}}) :rels {}} + (parse "<html><head><base href=\"http://example.com/\"></head><body> +<div class=\"h-card\"><img class=\"u-photo\" alt=\"Example User\" src=\"me.png\"></div> +</body></html>" "http://not-example.com/")) + (expect {:items '({:type ("h-card"), :properties {:photo ("http://blog.mozilla.org/press/files/2012/04/mitchell-baker.jpg"), :url ("http://blog.lizardwrangler.com/" "https://twitter.com/MitchellBaker"),