From cd8e3249f0e41372a2748ee8554e431d152cea01 Mon Sep 17 00:00:00 2001 From: unwox Date: Tue, 26 Nov 2024 15:35:35 +0600 Subject: init --- vendor/html.fnl | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 vendor/html.fnl (limited to 'vendor/html.fnl') diff --git a/vendor/html.fnl b/vendor/html.fnl new file mode 100644 index 0000000..753bbb9 --- /dev/null +++ b/vendor/html.fnl @@ -0,0 +1,32 @@ +(local entity-replacements {"&" "&" ; must be first! + "<" "<" + ">" ">" + "\"" """}) + +(local entity-search + (.. "[" (table.concat (icollect [k (pairs entity-replacements)] k)) "]")) + +(fn escape [s] + (assert (= (type s) :string)) + (s:gsub entity-search entity-replacements)) + +(fn tag [tag-name attrs self-closing?] + (assert (= (type attrs) "table") (.. "Missing attrs table: " tag-name)) + (let [attr-str (table.concat (icollect [k v (pairs attrs)] + (if (= v true) k + (.. k "=\"" (escape v)"\""))) " ")] + (.. "<" tag-name " " attr-str (if self-closing? " />" ">")))) + +(fn render [document allow-no-escape?] + (if (= (type document) :string) + (escape document) + (and allow-no-escape? (= (. document 1) :NO-ESCAPE)) + (. document 2) + (let [[tag-name attrs & body] document + self-closing? (= 0 (# body))] + (.. (tag tag-name attrs self-closing?) + (table.concat (icollect [_ element (ipairs body)] + (render element allow-no-escape?)) " ") + (if (not self-closing?) (.. "") ""))))) + +{ :render render } -- cgit v1.2.3