about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorAlan Pearce2014-09-13 09:07:35 +0100
committerAlan Pearce2014-09-13 09:07:35 +0100
commit08f9c2b929f1a19e39a1cda2743da3837f3cc132 (patch)
treef13ddb4f0589066de6e3210b8ef015f545d34127 /src
parent672e6221dfd065bba69d097806f10ec7344b4fb9 (diff)
downloadmicroformats-08f9c2b929f1a19e39a1cda2743da3837f3cc132.tar.lz
microformats-08f9c2b929f1a19e39a1cda2743da3837f3cc132.tar.zst
microformats-08f9c2b929f1a19e39a1cda2743da3837f3cc132.zip
Add u-* parsing capability
Diffstat (limited to 'src')
-rw-r--r--src/microformats/parser.clj32
1 files changed, 27 insertions, 5 deletions
diff --git a/src/microformats/parser.clj b/src/microformats/parser.clj
index 94e8381..a083ea0 100644
--- a/src/microformats/parser.clj
+++ b/src/microformats/parser.clj
@@ -34,7 +34,7 @@
            :class
            split-classes))
 
-(defn get-property
+(defn get-p-property
   "Get the p-x property value of an element"
   [el]
   (case (:tag el)
@@ -45,17 +45,39 @@
     :input (-> el :attrs :value)
     (or (first (:content el)) "")))
 
+(defn get-u-property
+  "Get the u-x property value of an element"
+  [el]
+  (case (:tag el)
+    :a (-> el :attrs :href)
+    :area (-> el :attrs :href)
+    :img (-> el :attrs :src)
+    :object (-> el :attrs :data)
+    (get-p-property el)))
+
 (defn parse-p
   "Parse p-* classes within HTML element."
   [element]
-  (let [el (first (html/select element [(html/attr-starts :class "p-")]))
-        props (into [] ((classes-to-props "p-") (element-to-classes el)))]
-    (hash-map :properties (apply hash-map (first props) (repeat (count props) (get-property el))))))
+  (let [prop (get-p-property element)]
+    (into {} (r/map #(hash-map % prop) ((classes-to-props "p-") (element-to-classes element))))))
+
+(defn parse-u
+  "Parse u-* classes within HTML element"
+  [element]
+  (let [prop (get-u-property element)]
+    (into {} (r/map #(hash-map % prop) ((classes-to-props "u-") (element-to-classes element))))))
+
+(defn parse-children
+  "Parse element children for microformats"
+  [element]
+  (let [el (first (html/select element [(html/union [(html/attr-starts :class "p-")
+                                                     (html/attr-starts :class "u-")])]))]
+    (hash-map :properties (merge (parse-p el) (parse-u el)))))
 
 (defn parse-h
   "Parse h-* classes within a HTML document."
   [html]
-  (mapv parse-p (html/select html [(html/attr-starts :class "h-")])))
+  (mapv parse-children (html/select html [(html/attr-starts :class "h-")])))
 
 (defn parse
   "Parse a HTML string with microformats"