diff options
author | Alan Pearce | 2014-09-24 13:23:30 +0100 |
---|---|---|
committer | Alan Pearce | 2014-09-24 13:25:04 +0100 |
commit | cdf600e89d0008ea42eaa60cc7b3abf0a18acff2 (patch) | |
tree | ea65a0da27fd440f876c1ec9d675d067bfabcc39 | |
parent | 388a6a07086c4217a907a8643b27693618c008f2 (diff) | |
download | microformats-cdf600e89d0008ea42eaa60cc7b3abf0a18acff2.tar.lz microformats-cdf600e89d0008ea42eaa60cc7b3abf0a18acff2.tar.zst microformats-cdf600e89d0008ea42eaa60cc7b3abf0a18acff2.zip |
Parse implied photo properties
-rw-r--r-- | src/microformats/parser.clj | 31 | ||||
-rw-r--r-- | test/microformats/parser_expectations.clj | 17 |
2 files changed, 39 insertions, 9 deletions
diff --git a/src/microformats/parser.clj b/src/microformats/parser.clj index f348f3b..ab14cd6 100644 --- a/src/microformats/parser.clj +++ b/src/microformats/parser.clj @@ -171,8 +171,8 @@ (r/filter (mf-names-from-class "h-")) (into []))) -(defn- imply-name - "Imply the name of an entity from the element" +(defn- parse-implied-name + "Get the implied name of an entity" [element] (case (:tag element) :abbr (-> element :attrs :title) @@ -188,25 +188,38 @@ (-> % :attrs :title) true (node-to-text (:content element))))) -(defn- imply-url +(defn- parse-implied-url [element] (case (:tag element) :a (-> element :attrs :href) nil)) -(defn- imply-photo +(defn- parse-implied-photo [element] (case (:tag element) :img (-> element :attrs :src) - nil)) + :object (-> element :attrs :data) + (cond-let + (first (html/select element [html/root :> [:img (html/but-node (html/attr-contains :class "h-")) html/only-of-type]])) + (-> % :attrs :src) + (first (html/select element [html/root :> [:object (html/but-node (html/attr-contains :class "h-")) html/only-of-type]])) + (-> % :attrs :data) + (first (html/select element [html/root :> html/only-child :> [:img (html/but-node (html/attr-contains :class "h-")) html/only-of-type]])) + (-> % :attrs :src) + (first (html/select element [html/root :> html/only-child :> [:object (html/but-node (html/attr-contains :class "h-")) html/only-of-type]])) + (-> % :attrs :data) + ))) + +(def empty-ish + #(not (str/blank? (first (second %))))) (defn parse-implied "Parse implied properties of a HTML element" [element] - (into {} (filter #(first (second %)) - {:name (list (imply-name element)) - :url (list (imply-url element)) - :photo (list (imply-photo element))}))) + (into {} (filter empty-ish + {:name (list (parse-implied-name element)) + :url (list (parse-implied-url element)) + :photo (list (parse-implied-photo element))}))) (defn get-mf-properties "Parse children of a microformat, returning a map of properties" diff --git a/test/microformats/parser_expectations.clj b/test/microformats/parser_expectations.clj index 30e05f1..639c417 100644 --- a/test/microformats/parser_expectations.clj +++ b/test/microformats/parser_expectations.clj @@ -152,6 +152,23 @@ (expect {:items [{:type ["h-card"] :properties {:name '("Example User")}}] :rels {}} (parse "<div class=\"h-card\"><p><abbr title=\"Example User\">Wrong</abbr></p></div>")) +(expect {:items [{:type ["h-card"] :properties {:photo '("http://example.com/me.png")}}] :rels {}} + (parse "<object class=\"h-card\" data=\"http://example.com/me.png\"></object>")) + +(expect {:items [{:type ["h-card"] :properties {:name '("Example User") + :photo '("http://example.com/me.png")}}] :rels {}} + (parse "<div class=\"h-card\"><img alt=\"Example User\" src=\"http://example.com/me.png\"></div>")) + +(expect {:items [{:type ["h-card"] :properties {:photo '("http://example.com/me.png")}}] :rels {}} + (parse "<div class=\"h-card\"><object data=\"http://example.com/me.png\"></object></div>")) + +(expect {:items [{:type ["h-card"] :properties {:photo '("http://example.com/me.png")}}] :rels {}} + (parse "<div class=\"h-card\"><div><object data=\"http://example.com/me.png\"></object></div></div>")) + +(expect {:items [{:type ["h-card"] :properties {:name '("Example User") + :photo '("http://example.com/me.png")}}] :rels {}} + (parse "<div class=\"h-card\"><div><img alt=\"Example User\" src=\"http://example.com/me.png\"></div></div>")) + (expect {:items [{:type ["h-adr"], :properties {:street-address '("665 3rd St."), |