Parse properties for child microformats
2 files changed, 40 insertions(+), 10 deletions(-)
M src/microformats/parser.clj → src/microformats/parser.clj
@@ -79,19 +79,38 @@ [el] (when-let [values (seq (html/select el [html/root :> :.value]))] (get-value-class values))) +(declare parse-h) + +(defn get-child-mf-properties + [element] + (assoc (parse-h element) :value (-> element :content node-to-text))) + +(defn remove-property-classes + [element] + (into {} (html/transform (list element) [html/root] + (apply html/remove-class (filter (prefixed-by? "p-") + (element-to-classes element)))))) + +(defn- find-child-mf + "Find child property microformats of an element." + [element] + (when (-> element :attrs :class (.indexOf "h-") (>= 0)) + (-> element remove-property-classes get-child-mf-properties))) + (defn get-p-value "Get the p-x property value of an element" [el] - (str/trim (or (find-value-class el) - (case (:tag el) - :img (-> el :attrs :alt) - :area (-> el :attrs :alt) - :abbr (-> el :attrs :title) - :data (-> el :attrs :value) - :input (-> el :attrs :value) - nil) - (node-to-text (:content el)) - ""))) + (or (find-child-mf el) + (str/trim (or (find-value-class el) + (case (:tag el) + :img (-> el :attrs :alt) + :area (-> el :attrs :alt) + :abbr (-> el :attrs :title) + :data (-> el :attrs :value) + :input (-> el :attrs :value) + nil) + (node-to-text (:content el)) + "")))) (defn get-u-value "Get the u-x property value of an element"
M test/microformats/parser_expectations.clj → test/microformats/parser_expectations.clj
@@ -244,3 +244,14 @@ (expect {:items [{:type ["h-card" "h-org"] :properties {:name '("Example")}}] :rels {}} (parse "<p class=\"h-card h-org\">Example</p>")) + +(expect {:items [{:type ["h-card"] + :properties {:name '("John Doe") + :org '({:value "Example" + :type ["h-card" "h-org"] + :properties {:name ("Example")}})}}] + :rels {}} + (parse "<div class=\"h-card\"> +<span class=\"p-name\">John Doe</span> +<span class=\"p-org h-card h-org\">Example</span> +</div>"))