/
markup.lisp
168 lines (147 loc) · 5.23 KB
/
markup.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(in-package :cl-markup)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun map-group-if (pred list fn)
(loop
while list
for cur = (pop list)
for cur-res = (funcall pred cur)
append
(let ((res (nreverse
(loop with acc = (list cur)
while list
for x = (pop list)
if (eq cur-res (funcall pred x))
do (push x acc)
else do (progn (push x list)
(return acc))
finally (return acc)))))
(if cur-res (list (apply fn res)) res)))))
(defun escape-string (string)
(if *auto-escape*
(regex-replace-all (create-scanner "[&<>'\"]") string
#'(lambda (match)
(case (aref match 0)
(#\& "&")
(#\< "<")
(#\> ">")
(#\' "'")
(#\" """)
(t match)))
:simple-calls t)
string))
(defun ensure-string (val)
(if (stringp val)
val
(format nil "~A" val)))
(defun should-escape-p (val)
(not (and (stringp val)
(string= val (escape-string val)))))
(defmacro %escape-string-form (val)
(let ((val2 (gensym)))
`(let ((,val2 ,val))
(if (should-escape-p ,val2)
`(escape-string ,,val2)
,val2))))
(defun %dirty-string-form (form)
(cond
((consp form) (let ((res (gensym)))
`(let ((,res ,form))
(if (listp ,res) (format nil "~{~A~}" ,res)
,res))))
((null form) "")
((stringp form) (%escape-string-form form))
((symbolp form) `(escape-string (ensure-string ,form)))
(t (%escape-string-form (format nil "~A" form)))))
(defmacro raw (&rest forms)
`(let (*auto-escape*) ,@forms))
(defmacro esc (&rest forms)
`(let ((*auto-escape* t))
,@(loop for form in forms
collect (%escape-string-form form))))
(defmacro %write-strings (&rest strings)
(let ((s (gensym)))
(flet ((conv (strs) (map-group-if #'stringp strs
(lambda (&rest args)
(apply #'concatenate 'string args)))))
`(if *output-stream*
(progn
,@(loop for str in (conv strings)
collect `(write-string ,str *output-stream*)))
(with-output-to-string (,s)
,@(loop for str in (conv strings)
collect `(write-string ,str ,s)))))))
(defun tagp (form)
(and (consp form)
(keywordp (car form))))
(defun parse-tag (tag)
(values
(pop tag)
(loop while (and tag (keywordp (car tag)))
collect (pop tag)
collect (pop tag))
tag))
(defun attributes->string (attr-plist)
(and (consp attr-plist)
(butlast
(loop for (key val) on attr-plist by #'cddr
append `(,(concatenate 'string
(string-downcase key)
"=\"")
,(%dirty-string-form val)
"\""
" ")))))
(defun tag->string (tag)
(multiple-value-bind (name attr-plist body)
(parse-tag tag)
(nconc
(list (format nil "<~(~A~)" name))
(let ((attr-str (attributes->string attr-plist)))
(if attr-str (cons " " attr-str)))
(if body
(nconc (list ">")
(loop for elem in body
if (tagp elem)
append (tag->string elem)
else
collect (%dirty-string-form elem))
(list (format nil "</~(~A~)>" name)))
(if (eq *markup-language* :html)
(list ">")
(list " />"))))))
(defmacro render-tag (tag)
`(%write-strings
,@(tag->string tag)))
(defun doctype ()
(%write-strings
(case *markup-language*
(:xml "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
(:html "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">")
(:xhtml "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">")
(t ""))))
(defmacro with-markup-lang (lang &body body)
`(%write-strings
,(eval-when (:compile-toplevel :load-toplevel :execute)
(if lang (setq *markup-language* lang))
"")
,@(loop for b in body
append (eval b))))
(defmacro with-doctype (lang &body body)
`(prog1
(with-markup-lang ,lang
(list (doctype))
,@body)
(setq *markup-language* :xhtml)))
(defmacro markup (&rest tags)
`(with-markup-lang nil
(loop for tag in ',tags
append (tag->string tag))))
(defmacro html (&rest tags)
`(with-doctype :html
(tag->string (cons :html ',tags))))
(defmacro xhtml (&rest tags)
`(with-doctype :xhtml
(tag->string (cons :html ',tags))))
(defmacro xml (&rest tags)
`(with-doctype :xml
(loop for tag in ',tags
append (tag->string tag))))