src/microformats/parser.clj (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | (ns microformats.parser (:require [net.cgrand.enlive-html :as html] [clojure.zip :as z] [clojure.core.reducers :as r] [clojure.string :as str] [clojurewerkz.urly.core :as url])) (defmacro anacond [& clauses] (when clauses (list 'if-let ['% (first clauses)] (if (next clauses) (second clauses) (throw (IllegalArgumentException. "anacond requires an even number of forms"))) (cons 'anacond (next (next clauses)))))) (defn prefixed-by? [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 (prefixed-by? 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- 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- node-to-text "Turn a node into a text string" [content] (->> content html/texts (map #(str/replace % #"\s+" " ")) (apply str) str/trim)) (defn get-base-url "Find the base-url of a document." [document] (or (-> document (html/select [:head :> [:base (html/attr? :href)]]) first :attrs :href) "")) (defn normalise-url "Normalise a URL" [root url] (url/resolve (get-base-url root) url)) (defn get-value-class "Get the value class of elements" [elements] (str/join " " (into [] ((comp (r/map (partial apply str)) (r/map node-to-text) (r/map :content)) elements)))) (defn find-value-class "Find and get the value class of elements" [el] (when-let [values (seq (html/select el [html/root :> :.value]))] (get-value-class values))) (declare parse-h) (defn get-child-mf-properties [element] (assoc (parse-h element) :value (-> element :content node-to-text))) (defn remove-property-classes [element] (into {} (html/transform (list element) [html/root] (apply html/remove-class (filter (prefixed-by? "p-") (element-to-classes element)))))) (defn- find-child-mf "Find child property microformats of an element." [element] (when (-> element :attrs :class (.indexOf "h-") (>= 0)) (-> element remove-property-classes get-child-mf-properties))) (defn get-p-value "Get the p-x property value of an element" [el] (or (find-child-mf el) (str/trim (or (find-value-class el) (case (:tag el) :img (-> el :attrs :alt) :area (-> el :attrs :alt) :abbr (-> el :attrs :title) :data (-> el :attrs :value) :input (-> el :attrs :value) nil) (node-to-text (:content el)) "")))) (defn get-u-value "Get the u-x property value of an element" [el] (str/trim (or (find-value-class el) (case (:tag el) :a (-> el :attrs :href) :area (-> el :attrs :href) :img (-> el :attrs :src) :object (-> el :attrs :data) (get-p-value el)) (node-to-text (:content el)) ""))) (defn get-dt-value "Get the dt-x property value of an element" [el] (str/trim (or (find-value-class el) (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) nil) (node-to-text (:content el)) ""))) (defn get-e-value "Get the e-x propery value of an element" [el] (let [content (:content el)] (list {:html (apply str (node-to-html content)) :value (apply str (node-to-text content))}))) (defn parse-p "Parse p-* classes within HTML element." [element] (->> element element-to-classes ((classes-to-props "p-")) (r/map #(hash-map % (list (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 % (list (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 % (list (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- get-mf-names "Get the microformat names from an element" [element] (->> element element-to-classes (filter (prefixed-by? "h-")))) (defn- parse-implied-name "Get the implied name of an entity" [element] (case (:tag element) :abbr (-> element :attrs :title) :img (-> element :attrs :alt) (anacond (first (html/select element [html/root :> [:img html/only-child]])) (-> % :attrs :alt) (first (html/select element [html/root :> [:abbr html/only-child (html/attr? :title)]])) (-> % :attrs :title) (first (html/select element [html/root :> html/only-child :> [:img html/only-child]])) (-> % :attrs :alt) (first (html/select element [html/root :> html/only-child :> [:abbr html/only-child (html/attr? :title)]])) (-> % :attrs :title) true (node-to-text (:content element))))) (defn- parse-implied-url [element] (case (:tag element) :a (-> element :attrs :href) (if-let [% (first (html/select element [html/root :> [:a (html/attr? :href) html/only-of-type (html/but-node (html/attr-contains :class "h-"))]]))] (-> % :attrs :href)))) (defn- parse-implied-photo [element] (case (:tag element) :img (-> element :attrs :src) :object (-> element :attrs :data) (anacond (first (html/select element [html/root :> [:img (html/but-node (html/attr-contains :class "h-")) html/only-of-type]])) (-> % :attrs :src) (first (html/select element [html/root :> [:object (html/but-node (html/attr-contains :class "h-")) html/only-of-type]])) (-> % :attrs :data) (first (html/select element [html/root :> html/only-child :> [:img (html/but-node (html/attr-contains :class "h-")) html/only-of-type]])) (-> % :attrs :src) (first (html/select element [html/root :> html/only-child :> [:object (html/but-node (html/attr-contains :class "h-")) html/only-of-type]])) (-> % :attrs :data) ))) (def empty-ish #(not (str/blank? (first (second %))))) (defn parse-implied "Parse implied properties of a HTML element" [element] (into {} (filter empty-ish {:name (list (parse-implied-name element)) :url (list (parse-implied-url element)) :photo (list (parse-implied-photo element))}))) (defn- select-p [element] (html/select element [[(html/but-node #{:br :hr}) (html/attr-contains :class "p-")]])) (defn- select-u [element] (html/select element [[(html/but-node #{:br :hr}) (html/attr-contains :class "u-")]])) (defn- select-dt [element] (html/select element [[(html/but-node #{:br :hr}) (html/attr-contains :class "dt-")]])) (defn- select-e [element] (html/select element [[(html/but-node #{:br :hr}) (html/attr-contains :class "e-")]])) (defn get-mf-properties "Parse children of a microformat, returning a map of properties" [element] (let [cappend (partial merge-with concat)] (merge (parse-implied element) (apply cappend (map parse-p (select-p element))) (apply cappend (map parse-u (select-u element))) (apply cappend (map parse-dt (select-dt element))) (apply cappend (map parse-e (select-e element)))))) (defn parse-h "Parse h-* classes within a HTML element." [element] (hash-map :type (get-mf-names element) :properties (get-mf-properties element))) (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" [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] (or (apply merge-with into (map parse-rel (select-rels elements))) {})) (defn parse "Parse a HTML string with microformats" [html] (let [document (html/html-snippet (str/trim html))] {:items (some->> document first z/xml-zip (map-h parse-h)) :rels (parse-rels document)})) |