html.fnl (918B)
1 (local {: string-stream } (require :lib)) 2 3 (fn to-html [el out-stream] 4 (if (not out-stream) 5 (let [s (string-stream)] (to-html el s) (s:to-string)) 6 (if (= (type el) :string) 7 (out-stream:write el) 8 (= (type el) :number) 9 (out-stream:write (tostring el)) 10 (let [[tag & content] el 11 (name attrstring) 12 (case tag 13 [name attrs] 14 (values name (accumulate [s "" 15 name value (pairs attrs)] 16 (.. s " " (string.format "%s=%q" name value)))) 17 name 18 (values name ""))] 19 (out-stream:write (string.format "<%s%s>" name attrstring)) 20 (each [_ child (ipairs content)] 21 (to-html child out-stream)) 22 (out-stream:write (string.format "</%s>" name)))))) 23 24 { : to-html }