summaryrefslogtreecommitdiff
path: root/src/dompa/nodes.clj
blob: 9b30be3855645b01bd98c145ab238a014c10dec3 (plain)
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
(ns dompa.nodes
  (:require [clojure.string :as str]
            [dompa.coordinates :as coordinates]))

(defn- html->node-name [html]
  (if (str/starts-with? html "<")
    (-> html
        (str/split #"[\s\>]")
        first
        (str/replace #"[\<\>\/]" "")
        keyword)
    :text-node))

(defn- html->node-attrs [html])



(defn coordinates->nodes
  [html coordinates]
  (when (seq coordinates)
    (let [sorted-coordinates (sort-by first coordinates)
          [parent-from parent-to] (first sorted-coordinates)
          children (coordinates/children sorted-coordinates [parent-from parent-to])
          remaining (coordinates/without-children sorted-coordinates [parent-from parent-to])
          node-html (subs html parent-from (inc parent-to))]
      (cons {:value (subs html parent-from (inc parent-to))
             :name (html->node-name node-html)
             :attrs (html->node-attrs node-html)
             :children (coordinates->nodes html children)}
            (coordinates->nodes html remaining)))))