From cdf600e89d0008ea42eaa60cc7b3abf0a18acff2 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 24 Sep 2014 13:23:30 +0100 Subject: Parse implied photo properties --- src/microformats/parser.clj | 31 ++++++++++++++++++++++--------- 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 "
Wrong