about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2014-09-28 14:22:51 +0100
committerAlan Pearce2014-09-28 14:22:51 +0100
commited360808d94ea0e637881158b393da52827573e9 (patch)
treebe85d8a1c8bc238077e7fc0ea5da84c73ee653a2
parent1ae47c1809a657bbef2ba30061527c73d2bed562 (diff)
downloadmicroformats-ed360808d94ea0e637881158b393da52827573e9.tar.lz
microformats-ed360808d94ea0e637881158b393da52827573e9.tar.zst
microformats-ed360808d94ea0e637881158b393da52827573e9.zip
select-h: use zippers for easier recursion
That was far too easy.
-rw-r--r--src/microformats/parser.clj10
-rw-r--r--test/microformats/parser_expectations.clj29
2 files changed, 21 insertions, 18 deletions
diff --git a/src/microformats/parser.clj b/src/microformats/parser.clj
index cf75a27..cebfcc7 100644
--- a/src/microformats/parser.clj
+++ b/src/microformats/parser.clj
@@ -258,9 +258,11 @@
 
 (defn select-h
   "Select top-level h-* elements within a HTML element."
-  [element]
-  (html/select element [[(html/attr-contains :class "h-")
-                         (html/but (parents (html/attr? :class)))]]))
+  [loc]
+  (when (not (z/end? loc))
+    (if (some->> loc z/node first :attrs :class (re-matcher #"(?:^|\s) h-\w"))
+      (z/node loc)
+      (recur (z/next loc)))))
 
 (defn parse-rel
   "Parse rel attributes of an HTML link element"
@@ -284,4 +286,4 @@
   "Parse a HTML string with microformats"
   [html]
   (let [document (html/html-snippet html)]
-    {:items (mapv parse-h (select-h document)) :rels (parse-rels document)}))
+    {:items (mapv parse-h (some->> document z/xml-zip select-h)) :rels (parse-rels document)}))
diff --git a/test/microformats/parser_expectations.clj b/test/microformats/parser_expectations.clj
index 455a26f..5f8eeef 100644
--- a/test/microformats/parser_expectations.clj
+++ b/test/microformats/parser_expectations.clj
@@ -1,6 +1,7 @@
 (ns microformats.parser-expectations
   (:require [expectations :refer :all]
             [microformats.parser :refer :all]
+            [clojure.zip :as z]
             [net.cgrand.enlive-html :refer [html-snippet]]))
 
 (defn- snippet
@@ -25,25 +26,25 @@
              (#'microformats.parser/node-to-text)))
 
 (expect '({:tag :div :attrs {:class "h-card"}
-                   :content nil})
-                (select-h (snippet
-                           "<div class=\"h-card\"></div>")))
+           :content nil})
+        (select-h (z/xml-zip (html-snippet
+                              "<div class=\"h-card\"></div>"))))
 
 (expect '({:tag :div :attrs {:class "h-card"}
-                   :content ({:tag :a :attrs {:class "h-org"}
-                              :content nil})})
-                (select-h (snippet
-                           "<div class=\"h-card\"><a class=\"h-org\"></a></div>")))
+           :content ({:tag :a :attrs {:class "h-org"}
+                      :content nil})})
+        (select-h (z/xml-zip (html-snippet
+                              "<div class=\"h-card\"><a class=\"h-org\"></a></div>"))))
 
 (expect '({:tag :div :attrs {:class "h-card"}
-                   :content ("\n"
-                             {:tag :p :attrs nil
-                              :content ({:tag :a :attrs {:class "h-org"}
-                                         :content nil})}
-                             "\n")})
-        (select-h (snippet "<div class=\"h-card\">
+           :content ("\n"
+                     {:tag :p :attrs nil
+                      :content ({:tag :a :attrs {:class "h-org"}
+                                 :content nil})}
+                     "\n")})
+        (select-h (z/xml-zip (html-snippet "<div class=\"h-card\">
 <p><a class=\"h-org\"></a></p>
-</div>")))
+</div>"))))
 
 (expect {:name '("Name")}
         (parse-p (snippet "<p class=\"p-name\"><span class=\"value\">Name</span></p>")))