-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.lisp
executable file
·327 lines (276 loc) · 8.96 KB
/
html.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
;#!/usr/local/bin/sbcl --noinform
;#!/sw/bin/clisp
;#!/usr/local/bin/clisp
;;;; Hey, Emacs, this is a -*- Mode: Lisp; Syntax: Common-Lisp -*- file!
;;;;
;;;; Programming should be fun. Programs should be beautiful.
;;;; -- Paul Graham
;;;;
;;;; Name: html.lisp
;;;;
;;;; Started: Wed Oct 19 10:39:05 2005
;;;; Modifications:
;;;;
;;;; Purpose:
;;;;
;;;;
;;;;
;;;; Calling Sequence:
;;;;
;;;;
;;;; Inputs:
;;;;
;;;; Outputs:
;;;;
;;;; Example:
;;;;
;;;; Notes:
;;;;
;;;;
(load "utils.lisp")
(defpackage html (:use common-lisp))
(in-package html)
;;;;
;;;; HTML tag functions
;;;;
(defconstant background-color "#CCDDFF")
(defconstant text-color "#000000")
;;;
;;; Main paired-tag function. First element of content list may be optional
;;; list of attributes.
;;;
(defun make-tag (tag content)
(let ((attributes-list (if (listp (first content))
(pop content)
nil)))
(format nil "<~A~A>~A</~A>" tag
(make-attributes attributes-list)
(utils:join content)
tag)) )
;;;
;;; XHTML-compliant for standalone tags.
;;;
(defun make-solo-tag (tag content)
(let ((attributes-list (if (listp (first content))
(pop content)
nil)))
(format nil "<~A~A />" tag
(make-attributes attributes-list))) )
;;;
;;; Create string of attributes/values for an HTML tag. The attributes, if
;;; any, are expected to come in a list of keywords and values. A keyword
;;; is expected to be paired with a value string or number following it. If
;;; a keyword is followed by another keyword (or by nothing), the first is
;;; assumed to be one of the few standalone attributes such as CHECKED,
;;; SELECTED, or NOSHADE. In such a case, the value of the attribute is
;;; simply the attribute itself to comply with the XHTML standard.
;;;
;;; Any empty string following a keyword causes that keyword to be removed
;;; from the final attributes string.
;;;
;;; Example: '(:border 1 :nowrap :width "80%")
;;; -> "border=\"1\" nowrap=\"nowrap\" width=\"80%\""
;;;
(defun make-attributes (attributes-list &optional (attributes-string ""))
(cond ((null attributes-list) attributes-string)
((not (keywordp (first attributes-list))) 'missing-keyword)
((or (keywordp (second attributes-list)) ;No value to go with keyword.
(null (rest attributes-list)))
(make-attributes (rest attributes-list)
(concatenate 'string
attributes-string
(format nil
" ~(~A~)=\"~:*~(~A~)\""
(first attributes-list)))) )
(t
(make-attributes (cddr attributes-list)
(concatenate 'string
attributes-string
(if (equal (second attributes-list) "")
""
(format nil
" ~(~A~)=\"~A\""
(first attributes-list)
(second attributes-list)))) ))) )
; (defun h1 (&optional (text ""))
; (format nil "<h1>~A</h1>" text) )
(defun html (&rest content)
(make-tag "html" content) )
(defun head (&rest content)
(make-tag "head" content) )
(defun title (&rest content)
(make-tag "title" content) )
(defun meta (&rest content)
(make-solo-tag "meta" content) )
;;;
;;; Get comments correct here.
;;; Only add the comment strings if there is actual content for the tag.
;;; If the only arg is a list of attributes, don't add comments.
;;;
(defun style (&rest content)
(apply #'style-script "style" content))
(defun script (&rest content)
(apply #'style-script "script" content))
(let ((open-comment (format nil "<!--~%"))
(close-comment "//-->"))
(defun style-script (tag-name &rest content)
(cond ((listp (first content))
(if (rest content)
(let ((attributes (pop content)))
(push open-comment content)
(push attributes content))))
(content (push open-comment content)))
(make-tag tag-name (if (rest content)
(append content
(list close-comment))
content))))
(defun body (&rest content)
(cond ((listp (first content))
(unless (member :bgcolor (first content))
(push background-color (first content))
(push :bgcolor (first content)))
(unless (member :text (first content))
(push text-color (first content))
(push :text (first content)))
(make-tag "body" content))
(t (make-tag "body" (cons (list :bgcolor background-color
:text text-color)
content)))) )
(defun div (&rest content)
(make-tag "div" content) )
(defun span (&rest content)
(make-tag "span" content) )
;;;
;;; Table tags
;;;
; (defun table (&rest args)
; ; &key (border "" border-flag) (width "" width-flag)
; ; (height "" height-flag) (cellpadding "" pad-flag)
; ; (cellspacing "" space-flag) (align "" align-flag)
; ; (bgcolor "" bgcolor-flag) &allow-other-keys)
; (let ((border (or (second (member :border args)) ""))
; (width (or (second (member :width args)) ""))
; (height (or (second (member :height args)) ""))
; (cellpadding (or (second (member :cellpadding args)) ""))
; (cellspacing (or (second (member :cellspacing args)) ""))
; (align (or (second (member :align args)) ""))
; (bgcolor (or (second (member :bgcolor args)) "")))
; ; (border (if border-flag
; ; (format nil " border=\"~A\"" border)
; ; border))
; ; (width (if width-flag
; ; (format nil " width=\"~A\"" width)
; ; width))
; ; (height (if height-flag
; ; (format nil " height=\"~A\"" height)
; ; height))
; ; (cellpadding (if pad-flag
; ; (format nil " cellpadding=\"~A\"" cellpadding)
; ; cellpadding))
; ; (cellspacing (if space-flag
; ; (format nil " cellspacing=\"~A\"" cellspacing)
; ; cellspacing))
; ; (align (if align-flag
; ; (format nil " align=\"~A\"" align)
; ; align))
; ; (bgcolor (if bgcolor-flag
; ; (format nil " bgcolor=\"~A\"" bgcolor)
; ; bgcolor)))
; (format nil "<table~A~A~A~A~A~A~A>~A</table>" border width height
; cellpadding cellspacing align bgcolor args))
; (format t "~A~%" args))
; (defun table (&rest table-content)
; (format nil "<table~A>~A</table>"
; (join (make-attributes (remove-if-not #'listp table-content)))
; (join (remove-if #'listp table-content))) )
; (defun tr (&rest row-content)
; (format nil "<tr~A>~A</tr>"
; (join (make-attributes (remove-if-not #'listp row-content)))
; (join (remove-if #'listp row-content))) )
; (defun td (&rest cell-content)
; (format nil "<td~A>~A</td>"
; (join (make-attributes (remove-if-not #'listp cell-content)))
; (join (remove-if #'listp cell-content))) )
; (defun strong (&optional (text ""))
; (format nil "<strong>~A</strong>" text) )
(defun table (&rest content)
(make-tag "table" content) )
(defun tr (&rest content)
(make-tag "tr" content) )
(defun th (&rest content)
(make-tag "th" content) )
(defun td (&rest content)
(make-tag "td" content) )
(defun h1 (&rest content)
(make-tag "h1" content) )
(defun h2 (&rest content)
(make-tag "h2" content) )
(defun ul (&rest content)
(make-tag "ul" content) )
(defun li (&rest content)
(make-tag "li" content) )
(defun strong (&rest content)
(make-tag "strong" content) )
(defun em (&rest content)
(make-tag "em" content) )
(defun u (&rest content)
(make-tag "u" content))
(defun sup (&rest content)
(make-tag "sup" content) )
(defun sub (&rest content)
(make-tag "sub" content) )
(defun p (&rest content)
(make-tag "p" content) )
(defun a (&rest content)
(make-tag "a" content) )
(defun font (&rest content)
(make-tag "font" content) )
(defun img (&rest content)
(make-solo-tag "img" content) )
(defun hr (&rest content)
(make-solo-tag "hr" content) )
(defun br (&rest content)
(make-solo-tag "br" content) )
;;;
;;; !?????????????????
;;;
(defun -- (&rest content)
(make-solo-tag "!--" content) )
;;;
;;; Form tags
;;;
(defun form (&rest content)
(make-tag "form" content) )
(defun input (&rest content)
(make-solo-tag "input" content) )
; (defun start-form (&key (method "" method-flag) (action "" action-flag)
; (name "" name-flag))
; (let ((action (if action-flag
; (format nil " action=\"~A\"" action)
; action))
; (name (if name-flag
; (format nil " name=\"~A\"" name)
; name))
; (method (if method-flag
; (format nil " method=\"~A\"" method)
; method)))
; (format nil "<form~A~A~A>" name method action)) )
; (defun end-form ()
; (format nil "</form>") )
; (defun textfield (&key (name "" name-flag))
; (let ((name (if name-flag
; (format nil " name=\"~A\"" name)
; name)))
; (format nil "<input type=\"text\"~A>" name)) )
; (defun submit (&key (name "" name-flag) (value "" value-flag))
; (let ((name (if name-flag
; (format nil " name=\"~A\"" name)
; name))
; (value (if value-flag
; (format nil " value=\"~A\"" value)
; value)))
; (format nil "<input type=\"submit\"~A~A>" name value)) )
; (defun textfield (&rest content)
; (make-solo-tag "input" (cons '(type "text") content)) )
; (defun submit (&rest content)
; (make-solo-tag "input" (cons '(type "submit") content)) )