From c773b522ebafbdfc9019898a83d74503700352e5 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 28 Sep 2014 20:57:02 +0100 Subject: Use lists instead of vectors Allows for some simplification. --- src/microformats/parser.clj | 19 +++--- test/microformats/parser_expectations.clj | 107 ++++++++++++++++-------------- 2 files changed, 65 insertions(+), 61 deletions(-) diff --git a/src/microformats/parser.clj b/src/microformats/parser.clj index 8d5bb10..ba07fcf 100644 --- a/src/microformats/parser.clj +++ b/src/microformats/parser.clj @@ -188,8 +188,7 @@ [element] (->> element element-to-classes - (r/filter (prefixed-by? "h-")) - (into []))) + (filter (prefixed-by? "h-")))) (defn- parse-implied-name "Get the implied name of an entity" @@ -270,13 +269,13 @@ (hash-map :type (get-mf-names element) :properties (get-mf-properties element))) -(defn select-h - "Select top-level h-* elements within a HTML element." - [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 map-h + "Map fn to top-level h-* elements within a HTML element." + [fn loc] + (if (some->> loc z/node :attrs :class (re-matcher #"(?:^|\s) h-\w")) + (list (fn (z/node loc))) + (when (not (z/end? loc)) + (recur fn (z/next loc))))) (defn parse-rel "Parse rel attributes of an HTML link element" @@ -300,4 +299,4 @@ "Parse a HTML string with microformats" [html] (let [document (html/html-snippet (str/trim html))] - {:items (mapv parse-h (some->> document z/xml-zip select-h)) :rels (parse-rels document)})) + {:items (some->> document first z/xml-zip (map-h parse-h)) :rels (parse-rels document)})) diff --git a/test/microformats/parser_expectations.clj b/test/microformats/parser_expectations.clj index b22e3a9..1c6d4f1 100644 --- a/test/microformats/parser_expectations.clj +++ b/test/microformats/parser_expectations.clj @@ -27,14 +27,19 @@ (expect '({:tag :div :attrs {:class "h-card"} :content nil}) - (select-h (z/xml-zip (html-snippet - "
")))) + (map-h identity (z/xml-zip (snippet + "")))) + +(expect '({:tag :div :attrs {:class "h-card"} + :content nil}) + (map-h identity (z/xml-zip (snippet + "Example User
Example User
Example User
Example User
")) -(expect {:items [{:type ["h-card"] :properties {:name '("Example User") :url '("http://example.com")}}] :rels {}} +(expect {:items '({:type ("h-card") :properties {:name ("Example User") :url ("http://example.com")}}) :rels {}} (parse "Example User")) -(expect {:items [{:type ["h-card"] :properties {:name '("Example User") - :photo '("http://example.com/me.png")}}] :rels {}} +(expect {:items '({:type ("h-card") :properties {:name ("Example User") + :photo ("http://example.com/me.png")}}) :rels {}} (parse "")) -(expect {:items [{:type ["h-card"] :properties {:name '("Example User")}}] :rels {}} +(expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "Incorrect")) -(expect {:items [{:type ["h-card"] :properties {:name '("Example User")}}] :rels {}} +(expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "")) -(expect {:items [{:type ["h-card"] :properties {:name '("Example User")}}] :rels {}} +(expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "Wrong
")) -(expect {:items [{:type ["h-card"] :properties {:name '("Example User")}}] :rels {}} +(expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "Wrong
Example
@@ -214,9 +219,9 @@ My BlogExample
@@ -225,14 +230,14 @@665 3rd St. @@ -243,16 +248,16 @@ U.S.A.
")) -(expect {:items [{:type ["h-card" "h-org"] - :properties {:name '("Example")}}] +(expect {:items '({:type ("h-card" "h-org") + :properties {:name ("Example")}}) :rels {}} (parse "Example
")) -(expect {:items [{:type ["h-card"] - :properties {:name '("John Doe") - :org '({:value "Example" - :type ["h-card" "h-org"] - :properties {:name ("Example")}})}}] +(expect {:items '({:type ("h-card") + :properties {:name ("John Doe") + :org ({:value "Example" + :type ("h-card" "h-org") + :properties {:name ("Example")}})}}) :rels {}} (parse "