-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml-s-exp.lisp
65 lines (54 loc) · 1.72 KB
/
html-s-exp.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(in-package :webhax)
(defvar *->html-list-handler*)
(defvar *->html-hash-handler*)
(defvar *->html-alist-handler*)
(defvar *->html-symbol-handler*)
(defvar *->html-misc-handler*)
(defvar *->html-main-handler*)
(defvar *->html-depth* 0)
(defun ->html (thing)
"Attempt to pleasantly display an s-expression as HTML"
(let ((*->html-depth* (1+ *->html-depth*)))
(funcall
(cond
((listp thing) *->html-list-handler*)
((hash-table-p thing) *->html-hash-handler*)
((symbolp thing) *->html-symbol-handler*)
(t *->html-misc-handler*))
thing)))
(defun symbol-handler (thing)
(html-out (str (capitalize-first thing))))
(defun misc-handler (thing)
(html-out (str thing)))
(defun list-handler (thing)
(if (null (remove-if #'consp thing)) ; Not guaranteed to be an alist!
(funcall *->html-alist-handler* thing)
(html-out
(:div
(dolist (th thing)
(htm (:div (funcall *->html-main-handler* th))))))))
(defun hash-handler (thing)
(html-out
(:table
(maphash
(lambda (k v)
(html-out
(:tr (:td
(funcall *->html-main-handler* k))
(:td
(funcall *->html-main-handler* v)))))
thing))))
(defun alist-handler (thing)
(html-out
(:table
(dolist (itm thing)
(htm (:tr (:td
(funcall *->html-main-handler* (car itm)))
(:td
(funcall *->html-main-handler* (cdr itm)))))))))
(setf *->html-list-handler* #'list-handler)
(setf *->html-hash-handler* #'hash-handler)
(setf *->html-alist-handler* #'alist-handler)
(setf *->html-symbol-handler* #'symbol-handler)
(setf *->html-misc-handler* #'misc-handler)
(setf *->html-main-handler* #'->html)