-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrun.lisp
481 lines (430 loc) · 15.5 KB
/
run.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
;;;; Functions used at run time or compile time.
(in-package #:spinneret)
(defun fast-format (stream control-string &rest args)
"Like `format', but bind `*print-pretty*' to nil."
(declare (dynamic-extent args))
(let ((*print-pretty* nil))
(format stream "~?" control-string args)))
(define-compiler-macro fast-format (&whole call stream control-string &rest args)
(if (stringp control-string)
(if (equalp control-string "~a")
(destructuring-bind (arg) args
`(princ ,arg ,stream))
`(fast-format ,stream (formatter ,control-string) ,@args))
call))
(-> call-without-trailing-space (function) t)
(defun call-without-trailing-space (fn)
(let ((*pending-space* nil))
(funcall fn)))
(defmacro without-trailing-space (&body body)
(with-thunk (body)
`(call-without-trailing-space ,body)))
(-> call-with-space (function) t)
(defun call-with-space (fn)
(flush-space)
(multiple-value-prog1 (funcall fn)
(buffer-space)))
(defmacro with-space (&body body)
(with-thunk (body)
`(call-with-space ,body)))
(declaim (inline buffer-space flush-space cancel-space))
(defun buffer-space ()
(setf *pending-space* t))
(defun flush-space ()
(when *pending-space*
(setf *pending-space* nil)
(unless (or *pre* *suppress-inserted-spaces*)
(write-char #\Space *html*))))
(defun cancel-space ()
(when *pending-space*
(setf *pending-space* nil)))
(defconstructor escaped-string
(value string))
(defmacro catch-output (arg &environment env)
(labels ((print-escaped (x)
`(html ,(escaped-string (escape-string x))))
(punt (&optional (x arg))
`(html ,x))
(rec (arg)
(typecase arg
(null nil)
(string (print-escaped arg))
(escaped-string (punt))
((or character number
;; not symbol, because not evaluated.
keyword (member t nil))
(print-escaped (princ-to-string arg)))
(t (multiple-value-bind (val constant?)
(eval-if-constant arg env)
(if (and constant?
(not (equal val arg)))
(rec val)
(punt)))))))
(rec arg)))
;;; Why not use a wrapper function for `html' to avoid generic
;;; dispatch on built-in types? Simple: we want users who write
;;; methods on `html' to be able to call `html' within their methods.
(defgeneric html (object)
(:method (object)
(declare (ignore object))
(values))
(:method ((nada null))
(values))
(:method :around ((nada null))
(values))
(:documentation "Handle writing OBJECT as HTML (for side-effects only)."))
(define-compiler-macro html (object)
`(locally (declare (notinline html))
,(if (typep object '(or string character number null keyword escaped-string))
`(html ,object)
(with-unique-names (temp)
`(let ((,temp ,object))
(and ,temp (html ,temp)))))))
(defmethod html :around (object)
(declare (ignore object))
(with-space
(call-next-method))
(values))
(defmethod html :around ((string string))
(when (string^= " " string)
(cancel-space))
(call-next-method)
(values))
(defmethod html ((string string))
(if *print-pretty*
(fill-text (escape-string string) t)
(escape-to-stream string #'escape-string-char *html*)))
(defmethod html :around ((string escaped-string))
(let ((string (escaped-string-value string)))
(when (or (string^= " " string)
;; Don't insert spaces before punctuation.
(and (not (alexandria:emptyp string))
(not (alpha-char-p (alexandria:first-elt string)))))
(cancel-space)))
(call-next-method)
(values))
(defmethod html ((string escaped-string))
(let ((string (escaped-string-value string)))
(if *print-pretty*
(fill-text string t)
(write-string string *html*))))
(defmethod html ((char character))
(if-let (escape (escape-string-char char))
(write-string escape *html*)
(write-char char *html*)))
(defmethod html ((n number))
(fast-format *html* "~d" n))
(defmethod html ((sym symbol))
(fast-format *html* "~a" sym))
(defun call/words (thunk string)
"Function that implements `do-words'."
(fbind (thunk)
(let ((window (make-array 0
:element-type (array-element-type string)
:adjustable t
:displaced-to string
:displaced-index-offset 0)))
(loop with len = (length string)
for left = 0 then (+ right 1)
for right = (or (position-if #'whitespace string :start left) len)
unless (= left right)
do (adjust-array window (- right left)
:displaced-to string
:displaced-index-offset left)
;; NB In terms of *words*, this might seem wrong: the
;; remainder of the string might just be whitespace.
;; However, this is the behavior we want: the presence
;; of trailing whitespace *should* be preserved.
(thunk window (= right len) (= left 0))
until (= right len)))))
(define-do-macro do-words ((var at-end? first-iteration? string &optional return)
&body body)
(with-thunk (body var at-end? first-iteration?)
`(call/words ,body ,string)))
(defun maybe-wrap (&optional (offset 0) (stream *html*))
(when *print-pretty*
(let* ((indent (get-indent))
(fill *fill-column*)
(goal (+ fill indent))
(col (+ offset (html-stream-column stream))))
(when (> col goal)
(terpri stream)))))
(defun fill-text (string &optional safe?
&aux (html *html*)
(pretty? *print-pretty*)
(pre? *pre*))
(check-type string string)
(cond
((= (length string) 0))
(pre?
(let ((stream (html-stream.base-stream html)))
(write-string string stream)))
(pretty?
(let* ((start-col (get-indent))
(fill *fill-column*)
(goal (+ fill start-col)))
(when (eql *html-style* :tree)
(fresh-line html))
(when (whitespace (aref string 0))
(write-char #\Space html))
(flet ((wrap () (terpri html)))
(declare (inline wrap))
(do-words (word at-end? first-iteration? string)
(let* ((word (if safe? word (escape-string word)))
(len (length word)))
(cond
;; Don't wrap after the opening of an inline element
;; unless whitespace is already present.
((and first-iteration?
(inline? (car *html-path*)))
(when (and (> (length word) 0)
(whitespacep (aref word 0)))
(wrap))
(write-string word html))
((> len fill)
(wrap)
(write-string word html)
(wrap))
((> (+ len (html-stream-column html))
goal)
(wrap)
(write-string word html))
(t (write-string word html))))
(unless at-end?
(write-char #\Space html))))))
(t
(with-space
(if safe?
(write-string string *html*)
(escape-to-stream string #'escape-string-char *html*)))))
(values))
(defun format-attribute-value (value)
(cond ((equal value "") "\"\"")
((keywordp value) (string-downcase value))
((eql value t) "true")
(t value)))
(defun format-attributes-with (attrs print-boolean print-value)
"Format ATTRS, uses the unary function PRINT-BOOLEAN to print
Boolean attributes, and the binary function PRINT-VALUE to print
ordinary attributes."
(fbind (print-boolean print-value)
(let ((seen '()))
;; Ensure that the leftmost keyword has priority,
;; as in function lambda lists.
(labels ((seen? (name)
(declare (optimize speed)
(symbol name))
(or (memq name seen)
(progn
(push name seen)
nil)))
(format-attr (attr value)
(unless (or (null value) (seen? attr))
(if (boolean? attr)
(print-boolean attr)
(let ((value (format-attribute-value value)))
(print-value attr value)))))
(dynamic-attrs (attrs)
(doplist (a v attrs)
(format-attr a (escape-value v)))))
(declare (inline seen?))
(doplist (attr value attrs)
(if (eql attr :attrs)
(dynamic-attrs value)
(format-attr attr value)))))))
(defun format-attributes-plain (attrs &optional (stream *html*))
(flet ((format-boolean (attr)
(format stream " ~(~a~)" attr))
(format-value (attr value)
(format stream " ~(~a~)=~a" attr value)))
(declare (dynamic-extent #'format-boolean #'format-value))
(format-attributes-with attrs #'format-boolean #'format-value)))
(defgeneric html-length (x)
(:documentation "The length of X when printed as an HTML string.
This is provided so you can give Spinneret the information it needs to
make reasonable decisions about line wrapping.")
(:method ((x t)) 0))
(defun html-length* (x)
(typecase x
((eql t) 4)
(string (length x))
(symbol (length (symbol-name x)))
(character 1)
(integer
(eif (zerop x) 1
(let ((x (abs x))
;; Single precision is not enough.
(base (coerce *print-base* 'double-float)))
(1+ (floor (log x base))))))
(otherwise
(assure unsigned-byte (html-length x)))))
(defun format-attributes-pretty/inline (attrs &optional (stream *html*))
(declare (stream stream))
(let* ((start-col (get-indent))
(fill *fill-column*)
(goal (+ start-col fill)))
(fbind* ((too-long?
(if *print-pretty*
(lambda (len)
(> (+ len (html-stream-column stream))
goal))
(constantly nil)))
(print-prefix
(lambda (len attr)
(let ((prefix (if (too-long? len) #\Newline #\Space)))
(write-char prefix stream)
;; XXX Work around
;; <https://abcl.org/trac/ticket/166>
#+abcl (write-string (string-downcase attr) stream)
#-abcl (format stream "~(~a~)" attr))))
(print-boolean
(lambda (attr)
(let ((len (length (symbol-name attr))))
;; No valid attribute is longer than 80. (I
;; suppose a data attribute could be.)
(print-prefix len attr))))
(print-attr
(lambda (attr value)
(let ((len (+ (length (symbol-name attr))
1 ;for the equals sign
(html-length* value))))
(print-prefix len attr))
(write-char #\= stream)
(format stream "~a" value))))
(declare (dynamic-extent #'print-prefix #'print-boolean #'print-attr))
(format-attributes-with attrs
#'print-boolean
#'print-attr))))
(defun format-attributes-pretty/block (attrs &optional (stream *html*))
(declare (html-stream stream))
(let ((*fill-column* (truncate *fill-column* 2))
;; Force the attributes to line up.
(*indent* (1+ (html-stream-column stream))))
(format-attributes-pretty/inline attrs stream)))
(defun escape-value (value)
(if (or (eq value t)
(eq value nil)
(keywordp value))
value
(let ((string
(if (typep value 'escaped-string)
(escaped-string-value value)
(escape-attribute-value
(princ-to-string value)))))
(if (needs-quotes? string)
(concatenate 'string "\"" string "\"")
string))))
(defun format-text (control-string &rest args)
(when (and *print-pretty* (not *pre*))
(terpri *html*))
(fill-text (format nil "~?" control-string args) t)
(values))
(defun xss-escape (arg)
"Possibly escape ARG for use with FORMAT.
We don't want to leave ourselves open to XSS, but we also want to be
able to use directives like ~c, ~d, ~{~} &c."
(typecase arg
((or number character symbol)
arg)
(list
(mapcar #'xss-escape arg))
(t
(escape-to-string arg))))
(defun make-doctype (&rest args)
(declare (ignore args))
(if *interpret*
(doctype)
`(doctype)))
(defun doctype (&rest args)
(declare (ignore args))
(format *html* "<!DOCTYPE html>~%"))
(defun make-comment (text)
(if *interpret*
(comment text nil)
`(comment ,(if (stringp text)
(escape-comment text)
text)
,(stringp text))))
(defun comment (text safe? &aux (html *html*))
(if *print-pretty*
(let ((*depth* (+ *depth* 1)))
(format html "~&~v,0T<!-- " *depth*)
(fill-text (if safe?
text
(escape-comment text))
t)
(format html " -->~%"))
(progn
(write-string "<!-- " html)
(write-string
(if safe?
text
(escape-comment text))
html)
(write-string " -->" html)))
(values))
(defun make-cdata (text)
(if *interpret*
(cdata text nil)
`(cdata ,(if (stringp text)
(escape-cdata text)
text)
,(stringp text))))
(defun cdata (text safe? &aux (html *html*))
(write-string cdata-start html)
(write-string (if safe?
text
(escape-cdata text))
html)
(write-string cdata-end html)
(values))
(defun make-html (&rest args)
(let ((lang (if *interpret* *html-lang* '*html-lang*)))
(multiple-value-bind (attrs body)
(parse-leading-keywords args)
(if (getf attrs :lang)
`(:html ,@args)
`(:html
:lang ,lang
,@attrs
,@body)))))
(defun make-head (&rest args)
(let ((charset (if *interpret* *html-charset* '*html-charset*)))
(multiple-value-bind (attrs body)
(parse-leading-keywords args)
(declare (ignore attrs))
(let ((meta-charset
(some (lambda (elt)
(trivia:match elt
((list :meta :charset _)
elt)))
body)))
(if meta-charset
`(:head ,@args)
`(:head
(:meta :charset ,charset)
,@args))))))
(defun write-raw (&rest args)
(if *interpret*
(let ((*pre* t))
(dolist (arg args)
(fill-text arg t))
nil)
`(let ((*pre* t))
,@(loop for arg in args
collect `(fill-text ,arg t))
nil)))
(-> heading-depth () (integer 1 6))
(defun heading-depth ()
"Return the current dynamic heading depth.
This follows the convention for the XHTML <h/> element, where the top
level is level 1, inside the first section is level 2, and so forth."
(clamp (1+ (count :section *html-path*)) 1 6))
(defun heading-depth-heading ()
(ecase (heading-depth)
(1 :h1)
(2 :h2)
(3 :h3)
(4 :h4)
(5 :h5)
(6 :h6)))