After seeing the recent WiFi demo I got thinking: has anybody written anything similar to Hiccup in Clojure, in uLisp? Using lists to represent HTML.
I’ve written a basic one in Chika:
(fn list->xml m
(if (not (type m []))
(return m))
(do tag= (nth 0 m)
(str \< tag \>
(.. (map list->xml (sect m)))
"</" tag \>)))
(list->xml
["body"
["h1" "Welcome"]
["p" "This is " ["i" "very"] " exciting."]])
And I’ve tried porting it to uLisp (my first uLisp function ever!):
(defun list->xml (m)
(if (listp m)
(let* ((m (second m))
(tag (first m)))
(concatenate 'string
"<" tag ">"
(apply #'concatenate 'string
(mapcar list->xml (rest m)))
"</" tag ">"))
(princ-to-string m)))
(list->xml
(quote '("body"
'("h1" "Welcome")
'("p" "This is " '("i" "very") " exciting."))))
Both return "<body><h1>Welcome</h1><p>This is <i>very</i> exciting.</p></body>"
.
Would there be a better way to do the uLisp version?
And Hiccup supports things like hash maps for attributes (e.g. {:class "bold"}
), and I’m thinking perhaps that could be like '('("h1" "class" "bold") "Welcome") => "<h1 class="bold">Welcome</h1>"
.