forked from ruricolist/spinneret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.lisp
221 lines (194 loc) · 6.94 KB
/
functions.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
(in-package #:spinneret)
(defpackage #:spinneret.tag
;; This package should not import any symbols.
(:use))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defparameter *tags-pkg* (find-package :spinneret.tag))
(defun tag-fn (tag &key intern)
(let ((tag (string tag)))
(if intern
(intern tag *tags-pkg*)
(find-symbol tag *tags-pkg*))))
(-> tag-open (string-designator) (simple-array character (*)))
(defun tag-open (tag)
(coerce (fmt "<~(~A~)" tag)
'(simple-array character (*))))
(-> tag-close (string-designator) (simple-array character (*)))
(defun tag-close (tag)
(if (void? tag)
""
(coerce (fmt "</~(~A~)>" tag)
'(simple-array character (*))))))
(defmacro define-tag (tag)
(let* ((inline? (inline? tag))
(paragraph? (paragraph? tag))
(fn-name (tag-fn tag :intern t))
(open (tag-open tag))
(close (tag-close tag))
(needs-close? (needs-close? tag)))
`(progn
(declaim (notinline ,fn-name))
(declaim (ftype (function (list function t t) (values))
,fn-name))
(defun ,fn-name (attrs body pre? empty?)
(let* ((html *html*)
(pretty *print-pretty*)
(style *html-style*)
(*pre* (or *pre* pre?))
(*depth* (1+ *depth*))
(*html-path* (cons ,(make-keyword tag) *html-path*)))
(declare (dynamic-extent *html-path*))
,(econd
(inline?
`(print-inline-tag html pretty style
,open ,(length open)
attrs
empty? body
,close
,needs-close?))
(paragraph?
`(print-par-tag html pretty style
,open attrs
empty? body
,close
,needs-close?))
(t
`(print-block-tag html pretty style
,open attrs
empty? body
,close
,needs-close?)))
(values))))))
(defmacro define-all-tags ()
`(progn
,@(loop for tag in (hash-table-keys *html5-elements*)
collect `(define-tag ,tag))))
;;; The auxiliary functions are block-compiled for speed.
(local*
(declaim (optimize (speed 3) (safety 0) (debug 0)
(compilation-speed 0)))
(declaim (inline close-inline close-block))
(defun open-block (html pretty open attrs)
(when pretty
(fresh-line html))
(write-string open html)
(when attrs
(if pretty
(format-attributes-pretty/block attrs html)
(format-attributes-plain attrs html)))
(write-char #\> html))
(defun open-par (html pretty open attrs)
(open-block html pretty open attrs))
(defun open-inline (html pretty open offset attrs)
(when pretty
(maybe-wrap offset html))
(write-string open html)
(when attrs
(if pretty
(format-attributes-pretty/inline attrs html)
(format-attributes-plain attrs html)))
(write-char #\> html))
(defun block-body (html body pretty)
(declare (type function body))
(when pretty
(elastic-newline html))
(let ((*indent* (1+ *depth*)))
(without-trailing-space
(funcall body)))
(when (and pretty (not *pre*))
(terpri html)))
(defun inline-body (body)
(declare (type function body))
(let ((*indent* (1+ *depth*)))
(without-trailing-space
(funcall body)))
(values))
(defun par-body (body)
(inline-body body))
(defun close-inline (html close needs-close?)
(when needs-close?
(write-string close html)))
(defun close-block (html close needs-close?)
(when needs-close?
(write-string close html)))
(defun close-par (html close needs-close?)
(when needs-close?
(write-string close html))
(elastic-newline html))
(defun print-inline-tag (html pretty style open offset attrs empty? body close needs-close?)
(when (eql style :tree)
(return-from print-inline-tag
(print-block-tag html pretty style open attrs empty? body close t)))
(open-inline html pretty open offset attrs)
(unless empty?
(inline-body body))
(close-inline html close needs-close?))
(defun print-par-tag (html pretty style open attrs empty? body close needs-close?)
(when (eql style :tree)
(return-from print-par-tag
(print-block-tag html pretty style open attrs empty? body close t)))
(open-par html pretty open attrs)
(unless empty?
(par-body body))
(close-par html close needs-close?))
(defun print-block-tag (html pretty style open attrs empty? body close needs-close?)
(when (eql style :tree)
(setq needs-close? t))
(open-block html pretty open attrs)
(unless empty?
(block-body html body pretty))
(close-block html close needs-close?))
(progn
(defun custom-elt-fn (open close attrs body empty?)
(print-block-tag *html*
*print-pretty*
*html-style*
open
attrs
empty?
body
close t))
(defun dynamic-tag* (tag attrs body empty?)
"Dynamically select a tag at runtime.
Note that TAG must be a known tag."
(mvlet* ((tag tag-attrs (dissect-tag tag))
(tag (or (and-let* ((kw (find-keyword (string-upcase tag)))
(tag (valid? kw))))
(error 'no-such-tag :name tag)))
(attrs (append tag-attrs attrs))
(open (tag-open tag))
;; Note that dynamic tags always print the closing tag --
;; not worth the effort to check.
(close (tag-close tag))
(*pre* (or *pre* (and (preformatted? tag) t)))
(*depth* (1+ *depth*))
(*html-path* (cons tag *html-path*))
(pretty *print-pretty*)
(style *html-style*))
(declare (dynamic-extent *html-path*))
(cond ((inline? tag)
(print-inline-tag
*html*
pretty style
open (length open) attrs
empty?
body
close t))
((paragraph? tag)
(print-par-tag
*html*
pretty style
open attrs
empty? body
close t))
(t
(print-block-tag
*html*
pretty style
open
attrs
empty?
(assure function body)
close t)))
(values)))
(define-all-tags)))