From cba956458645f10a83d9f44f40b30e422de8ec63 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 10 Sep 2014 19:03:56 +0100 Subject: Create test & implementation for p-name parsing --- project.clj | 3 ++- src/microformats/parser.clj | 18 ++++++++++++++++-- test/microformats/parser_test.clj | 7 ++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/project.clj b/project.clj index 3d3d3f5..a3b4995 100644 --- a/project.clj +++ b/project.clj @@ -3,4 +3,5 @@ :url "https://github.com/alanpearce/microformats" :license {:name "MIT License" :url "http://opensource.org/licenses/MIT"} - :dependencies [[org.clojure/clojure "1.6.0"]]) + :dependencies [[org.clojure/clojure "1.6.0"] + [enlive "1.1.5"]]) diff --git a/src/microformats/parser.clj b/src/microformats/parser.clj index c741549..c44803b 100644 --- a/src/microformats/parser.clj +++ b/src/microformats/parser.clj @@ -1,6 +1,20 @@ -(ns microformats.parser) +(ns microformats.parser + (:require [net.cgrand.enlive-html :as html])) + +;;; Turn string into stream + +(defn parse-p + "Parse p-* classes within HTML element." + [element] + (hash-map :properties (hash-map :name (first (:content (first (html/select element [:.p-name]))))))) + +(defn parse-h + "Parse h-* classes within a HTML document." + [html] + (mapv parse-p (html/select html [:.h-card]))) (defn parse "Parse a HTML string with microformats" [html] - {:items [] :rels {}}) + (let [document (html/html-snippet html)] + {:items (parse-h document) :rels {}})) diff --git a/test/microformats/parser_test.clj b/test/microformats/parser_test.clj index 5bdf9ad..9720ad2 100644 --- a/test/microformats/parser_test.clj +++ b/test/microformats/parser_test.clj @@ -1,8 +1,13 @@ (ns microformats.parser-test (:require [clojure.test :refer :all] - [microformats.core :refer :all])) + [microformats.parser :refer :all])) (deftest empty-document (testing "Empty HTML document should return an empty 'items' array and 'rels' hash." (is (= {:items [] :rels {}} (parse ""))))) + +(deftest parse-p-inner-text + (testing "Inner text of a p- property should be parsed") + (is (= {:items [{:properties {:name "Example User"}}] :rels {}} + (parse "

Example User

")))) -- cgit 1.4.1