summaryrefslogtreecommitdiff
path: root/vendor/html.fnl
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/html.fnl')
-rw-r--r--vendor/html.fnl31
1 files changed, 31 insertions, 0 deletions
diff --git a/vendor/html.fnl b/vendor/html.fnl
new file mode 100644
index 0000000..689f416
--- /dev/null
+++ b/vendor/html.fnl
@@ -0,0 +1,31 @@
+(local entity-replacements {"&" "&" ; must be first!
+ "<" "&lt;"
+ ">" "&gt;"
+ "\"" "&quot;"})
+
+(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]
+ (assert (= (type attrs) "table") (.. "Missing attrs table: " tag-name))
+ (let [attr-str (table.concat (icollect [k v (pairs attrs)]
+ (if (= v true) k
+ (.. k "=\"" v"\""))) " ")]
+ (.. "<" tag-name " " attr-str">")))
+
+(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]
+ (.. (tag tag-name attrs)
+ (table.concat (icollect [_ element (ipairs body)]
+ (render element allow-no-escape?)) " ")
+ "</" tag-name ">"))))
+
+{ :render render }