about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2014-09-13 10:00:49 +0100
committerAlan Pearce2014-09-13 10:01:42 +0100
commite8a96393c8e6c2b5f94e43e58680ee90a2b5a6b0 (patch)
treeff0973aa59bf040b6e2dde48acd33b960f3e0294
parent1ebede4bbab4fc1315cc0bf5fdc402a29eab34e1 (diff)
downloadmicroformats-e8a96393c8e6c2b5f94e43e58680ee90a2b5a6b0.tar.lz
microformats-e8a96393c8e6c2b5f94e43e58680ee90a2b5a6b0.tar.zst
microformats-e8a96393c8e6c2b5f94e43e58680ee90a2b5a6b0.zip
Add basic support for dt-* parsing
-rw-r--r--src/microformats/parser.clj22
-rw-r--r--test/microformats/parser_test.clj30
2 files changed, 51 insertions, 1 deletions
diff --git a/src/microformats/parser.clj b/src/microformats/parser.clj
index 75f7de9..f89ed8f 100644
--- a/src/microformats/parser.clj
+++ b/src/microformats/parser.clj
@@ -57,6 +57,19 @@
       (first :content el)
       ""))
 
+(defn get-dt-property
+  "Get the dt-x property value of an element"
+  [el]
+  (or (case (:tag el)
+        :time (-> el :attrs :datetime)
+        :ins  (-> el :attrs :datetime)
+        :del  (-> el :attrs :datetime)
+        :abbr (-> el :attrs :title)
+        :data (-> el :attrs :value)
+        :input (-> el :attrs :value))
+      (first (:content el))
+      ""))
+
 (defn parse-p
   "Parse p-* classes within HTML element."
   [element]
@@ -69,11 +82,18 @@
   (let [prop (get-u-property element)]
     (into {} (r/map #(hash-map % prop) ((classes-to-props "u-") (element-to-classes element))))))
 
+(defn parse-dt
+  "Parse dt-* classes within HTML element"
+  [element]
+  (let [prop (get-dt-property element)]
+    (into {} (r/map #(hash-map % prop) ((classes-to-props "dt-") (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-")])]))]
+                                                     (html/attr-starts :class "u-")
+                                                     (html/attr-starts :class "dt-")])]))]
     (hash-map :properties (merge (parse-p el) (parse-u el)))))
 
 (defn parse-h
diff --git a/test/microformats/parser_test.clj b/test/microformats/parser_test.clj
index 0f712de..63c7708 100644
--- a/test/microformats/parser_test.clj
+++ b/test/microformats/parser_test.clj
@@ -70,3 +70,33 @@
 
          {:photo "http://example.com/someimage.png"}
          "<data class=\"u-photo\">http://example.com/someimage.png</data>")))
+
+(deftest parse-dt-elements
+  (testing "Tags with dt-* classes should have their values parsed"
+    (are [ex in] (= ex (parse-dt (first (html-snippet in))))
+         {:start "2012-08-05T14:50"}
+         "<time class=\"dt-start\" datetime=\"2012-08-05T14:50\"></time>"
+
+         {:start "2012-08-05T14:50"}
+         "<time class=\"dt-start\">2012-08-05T14:50</time>"
+
+         {:start "2012-08-05T14:50"}
+         "<ins class=\"dt-start\" datetime=\"2012-08-05T14:50\"></ins>"
+
+         {:end "2012-08-05T18:00"}
+         "<del class=\"dt-end\" datetime=\"2012-08-05T18:00\"></del>"
+
+         {:start "2012-08-05T14:50"}
+         "<abbr class=\"dt-start\" title=\"2012-08-05T14:50\"></abbr>"
+
+         {:start "2012-08-05T14:50"}
+         "<abbr class=\"dt-start\">2012-08-05T14:50</abbr>"
+
+         {:start "2012-08-05T14:50"}
+         "<data class=\"dt-start\" value=\"2012-08-05T14:50\"></data>"
+
+         {:start "2012-08-05T14:50"}
+         "<data class=\"dt-start\">2012-08-05T14:50</data>"
+
+         {:start "2012-08-05T14:50"}
+         "<input class=\"dt-start\" value=\"2012-08-05T14:50\">")))