diff options
author | Alan Pearce | 2014-09-15 21:14:09 +0100 |
---|---|---|
committer | Alan Pearce | 2014-09-15 21:14:09 +0100 |
commit | ad7798bf10d8cc64f28a6bcdd1e83e1ae77cc14e (patch) | |
tree | 7a323e0c52672b6f783e51a4a51fb970d0f48ee2 | |
parent | c2d85f7328e7d08de1405638748d3c78ce833cc3 (diff) | |
download | microformats-ad7798bf10d8cc64f28a6bcdd1e83e1ae77cc14e.tar.lz microformats-ad7798bf10d8cc64f28a6bcdd1e83e1ae77cc14e.tar.zst microformats-ad7798bf10d8cc64f28a6bcdd1e83e1ae77cc14e.zip |
Implement some basic value-class parsing
-rw-r--r-- | src/microformats/parser.clj | 23 | ||||
-rw-r--r-- | test/microformats/parser_expectations.clj | 6 |
2 files changed, 26 insertions, 3 deletions
diff --git a/src/microformats/parser.clj b/src/microformats/parser.clj index ac7aef2..2778cc2 100644 --- a/src/microformats/parser.clj +++ b/src/microformats/parser.clj @@ -39,10 +39,25 @@ :rel split-ws-attribute)) +(defn get-value-class + "Get the value class of elements" + [elements] + (str/join " " (into [] ((comp (r/map (partial apply str)) + (r/map html/texts) + (r/map :content)) + elements)))) + +(defn find-value-class + "Find and get the value class of elements" + [el] + (when-let [values (seq (html/select el [html/root :> :.value]))] + (get-value-class values))) + (defn get-p-value "Get the p-x property value of an element" [el] - (or (case (:tag el) + (or (find-value-class el) + (case (:tag el) :img (-> el :attrs :alt) :area (-> el :attrs :alt) :abbr (-> el :attrs :title) @@ -55,7 +70,8 @@ (defn get-u-value "Get the u-x property value of an element" [el] - (or (case (:tag el) + (or (find-value-class el) + (case (:tag el) :a (-> el :attrs :href) :area (-> el :attrs :href) :img (-> el :attrs :src) @@ -67,7 +83,8 @@ (defn get-dt-value "Get the dt-x property value of an element" [el] - (or (case (:tag el) + (or (find-value-class el) + (case (:tag el) :time (-> el :attrs :datetime) :ins (-> el :attrs :datetime) :del (-> el :attrs :datetime) diff --git a/test/microformats/parser_expectations.clj b/test/microformats/parser_expectations.clj index 0602119..9c8ce1d 100644 --- a/test/microformats/parser_expectations.clj +++ b/test/microformats/parser_expectations.clj @@ -12,6 +12,12 @@ (expect "Foo <strong>bar</strong>" (apply str (#'microformats.parser/node-to-html '("Foo " {:tag :strong, :attrs nil, :content ("bar")})))) +(expect {:name "Name"} + (parse-p (first (html-snippet "<p class=\"p-name\"><span class=\"value\">Name</span></p>")))) + +(expect {:name "Name Endname"} + (parse-p (first (html-snippet "<p class=\"p-name\"><span class=\"value\">Name</span> (this should not be included) <span class=\"value\">Endname</span></p>")))) + (expect {:name "Example User"} (parse-p (first (html-snippet "<p class=\"p-name\">Example User</p>")))) |