(ns microformats.parser (:require [net.cgrand.enlive-html :as html] [clojure.core.reducers :as r] [clojure.string :as str])) (defn mf-names-from-class "Get microformat classnames from a class attribute" [prefix] #(.startsWith % prefix)) (defn remove-mf-prefix "Remove microformats prefixes from a class attribute" [prefix] #(apply str (drop (count prefix) %))) (defn- split-ws-attribute "Split a whitespace-separated attribute." [class] (str/split class #"\s+")) (defn classes-to-props "Convert class list to list of microformat property keywords" [prefix] (comp (r/map keyword) (r/map (remove-mf-prefix prefix)) (r/filter (mf-names-from-class prefix)))) (defn element-to-classes "Get list of classes from an element" [el] (some-> el :attrs :class split-ws-attribute)) (defn element-to-rels "Get list of rels from an element" [el] (-> el :attrs :rel split-ws-attribute)) (defn get-p-value "Get the p-x property value of an element" [el] (or (case (:tag el) :img (-> el :attrs :alt) :area (-> el :attrs :alt) :abbr (-> el :attrs :title) :data (-> el :attrs :value) :input (-> el :attrs :value) nil) (first (:content el)) "")) (defn get-u-value "Get the u-x property value of an element" [el] (or (case (:tag el) :a (-> el :attrs :href) :area (-> el :attrs :href) :img (-> el :attrs :src) :object (-> el :attrs :data) (get-p-value el)) (first :content el) "")) (defn get-dt-value "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- node-to-html "Turn a node into a list of HTML strings" [el] (map #(if (string? %) % (apply str (persistent! (html/emit-tag % (transient []))))) el)) (defn get-e-value "Get the e-x propery value of an element" [el] (let [content (:content el)] {:html (apply str (node-to-html content)) :value (apply str (html/texts content))})) (defn parse-p "Parse p-* classes within HTML element." [element] (->> element element-to-classes ((classes-to-props "p-")) (r/map #(hash-map % (get-p-value element))) (into {}))) (defn parse-u "Parse u-* classes within HTML element" [element] (->> element element-to-classes ((classes-to-props "u-")) (r/map #(hash-map % (get-u-value element))) (into {}))) (defn parse-dt "Parse dt-* classes within HTML element" [element] (->> element element-to-classes ((classes-to-props "dt-")) (r/map #(hash-map % (get-dt-value element))) (into {}))) (defn parse-e "Parse e-* classes within HTML element" [element] (->> element element-to-classes ((classes-to-props "e-")) (r/map #(hash-map % (get-e-value element))) (into {}))) (defn parse-children "Parse element children for microformats" [element] (let [el (first (html/select element {(html/attr-starts :class "p-") (html/attr-starts :class "u-") (html/attr-starts :class "dt-") (html/attr-starts :class "e-")}))] (hash-map :properties (merge ((juxt parse-p parse-u parse-dt parse-e)))))) (defn parse-h "Parse h-* classes within a HTML document." [element] (mapv parse-children (html/select element [(html/attr-starts :class "h-")]))) (defn parse-rel "Parse rel attributes of an HTML link element" [element] (->> element element-to-rels (map keyword) (map #(hash-map % [(-> element :attrs :href)])) (into {}))) (defn select-rels "Select linking HTML elements with rel attributes" [html] (html/select html [[#{:a :link} (html/attr? :rel)]])) (defn parse-rels "Parse rel attibutes of a set of HTML link elements" [elements] (apply merge-with into (map parse-rel (select-rels elements)))) (defn parse "Parse a HTML string with microformats" [html] (let [document (html/html-snippet html)] {:items (parse-h document) :rels (parse-rels document)}))