Add full support for implied names
2 files changed, 37 insertions(+), 1 deletion(-)
M src/microformats/parser.clj → src/microformats/parser.clj
@@ -3,6 +3,16 @@ (:require [net.cgrand.enlive-html :as html] [clojure.core.reducers :as r] [clojure.string :as str])) +(defmacro cond-let + [& clauses] + (when clauses + (list 'if-let ['x (first clauses)] + (if (next clauses) + (second clauses) + (throw (IllegalArgumentException. + "cond-let requires an even number of forms"))) + (cons 'cond-let (next (next clauses)))))) + (defn mf-names-from-class "Get microformat classnames from a class attribute" [prefix]@@ -165,8 +175,18 @@ (defn- imply-name "Imply the name of an entity from the element" [element] (case (:tag element) + :abbr (-> element :attrs :title) :img (-> element :attrs :alt) - (node-to-text (:content element)))) + (cond-let + (first (html/select element [html/root :> [:img html/only-child]])) + (-> x :attrs :alt) + (first (html/select element [html/root :> [:abbr html/only-child (html/attr? :title)]])) + (-> x :attrs :title) + (first (html/select element [html/root :> html/only-child :> [:img html/only-child]])) + (-> x :attrs :alt) + (first (html/select element [html/root :> html/only-child :> [:abbr html/only-child (html/attr? :title)]])) + (-> x :attrs :title) + true (node-to-text (:content element))))) (defn- imply-url [element]
M test/microformats/parser_expectations.clj → test/microformats/parser_expectations.clj
@@ -136,6 +136,22 @@ (expect {:items [{:type ["h-card"] :properties {:name '("Example User") :photo '("http://example.com/me.png")}}] :rels {}} (parse "<img class=\"h-card\" alt=\"Example User\" src=\"http://example.com/me.png\"></img>")) + +(expect {:items [{:type ["h-card"] :properties {:name '("Example User")}}] :rels {}} + (parse "<abbr class=\"h-card\" title=\"Example User\">Incorrect</abbr>")) + +(expect {:items [{:type ["h-card"] :properties {:name '("Example User")}}] :rels {}} + (parse "<p class=\"h-card\"><img alt=\"Example User\"></p>")) + +(expect {:items [{:type ["h-card"] :properties {:name '("Example User")}}] :rels {}} + (parse "<p class=\"h-card\"><abbr title=\"Example User\">Wrong </abbr></p>")) + +(expect {:items [{:type ["h-card"] :properties {:name '("Example User")}}] :rels {}} + (parse "<div class=\"h-card\"><div><img alt=\"Example User\"></div></div>")) + +(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-adr"], :properties {:street-address '("665 3rd St."),