Skip to content

Instantly share code, notes, and snippets.

@humorless
Created August 28, 2023 10:19
Show Gist options
  • Save humorless/39463ddd3ac9470b20c9978ff04c72dc to your computer and use it in GitHub Desktop.
Save humorless/39463ddd3ac9470b20c9978ff04c72dc to your computer and use it in GitHub Desktop.
Demo of treewalk - prewalk
[:customer-invoice
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Reference-number: invoice 1"]
[:p.flex-auto "status: unreleased"]]
[:customer-invoice-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 1"]
[:p.flex-auto "Invoice: invoice 1"]
[:p.flex-auto "Amount: 2000"]]
[:customer-invoice-tax-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 1"]
[:p.flex-auto "Amount: 200"]]
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 2"]
[:p.flex-auto "Amount: 200"]]
[:customer-invoice-tax-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 3"]
[:p.flex-auto "Amount: 200"]]]]]]
(ns myproject
"FIXME: my new org.corfield.new/scratch project."
(:require [clojure.walk :as walk]))
(def lines
(list {:reference-number "invoice 1"
:status "unreleased"}
(list {:invoice "invoice 1"
:account "100"
:desp "customer invoice line"
:tax-amount 2000
:id 1}
(list {:invoice "inovice 1"
:tax-amount 200
:id 1}
{:invoice "inovice 1"
:tax-amount 200
:id 2}
(list {:invoice "inovice 1"
:tax-amount 200
:id 3})))))
(defn hiccup-tag
[ele]
(cond
(some? (:reference-number ele)) :customer-invoice
(some? (:desp ele)) :customer-invoice-line
:else :customer-invoice-tax-line))
(defmulti hiccupize
(fn [x] (hiccup-tag x)))
(defmethod hiccupize :customer-invoice
[{:keys [reference-number status]}]
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto (str "Reference-number: " (or reference-number "- id -"))]
[:p.flex-auto (str "status: " status)]])
(defmethod hiccupize :customer-invoice-line
[{:keys [invoice tax-amount id]}]
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto (str "Id: " (or id "- id -"))]
[:p.flex-auto (str "Invoice: " (or invoice "- invoice -"))]
[:p.flex-auto (str "Amount: " tax-amount)]])
(defmethod hiccupize :customer-invoice-tax-line
[{:keys [tax-amount id]}]
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto (str "Id: " (or id "- id -"))]
[:p.flex-auto (str "Amount: " tax-amount)]])
(defn f
[x]
(cond
(list? x) (let [ele (first x)
tag (hiccup-tag ele)]
(vec (conj x tag)))
(map? x) (hiccupize x)
:else x))
(walk/prewalk f lines)
(defn -main
"Invoke me with clojure -M -m myproject"
[& args]
(println "-main with" args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment