From 8080d2b0044b348210622185b80c5f4514bb52e2 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Sat, 30 Aug 2025 16:18:44 +0300 Subject: Getting there ... --- src/dompa/core.clj | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'src/dompa/core.clj') diff --git a/src/dompa/core.clj b/src/dompa/core.clj index b97d5f2..d8fff41 100644 --- a/src/dompa/core.clj +++ b/src/dompa/core.clj @@ -4,10 +4,41 @@ [dompa.nodes :refer [coordinates->nodes]])) (defn html->nodes [html] - (let [coordinates (html->coordinates html) - nodes (coordinates->nodes html coordinates)] + (->> html + html->coordinates + (coordinates->nodes html))) + +(defn nodes->html [nodes] + (reduce + (fn [html node] + (cond + (= (-> node :name) :dompa/text) + (str html (-> node :value)) + + :else + (let [node-name (-> node :name name) + node-child-html (nodes->html (-> node :children))] + (str html "<" node-name ">" node-child-html "")))) + "" nodes)) +(defn traverse-nodes [nodes pred] + (reduce + (fn [updated-nodes node] + (if-let [updated-node (pred node)] + (let [children (traverse-nodes (-> updated-node :children) pred)] + (conj updated-nodes (assoc updated-node :children children))) + updated-nodes)) + [] + nodes)) + +(defn traverse-html [html pred] + (-> (html->nodes html) + (traverse-nodes pred) + nodes->html)) + (comment + (traverse-html "
asdasdhello
" #(when-not (= (-> % :name) :span) + %)) (html->coordinates "
helloasdasdasdadad
hello some text
another root element
") - (html->nodes "
helloasdasdasdadad
")) \ No newline at end of file + (html->nodes "
helloasdasdasdadad")) \ No newline at end of file -- cgit v1.2.3