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
|
;;; writer.scm -- Markup writers.
;;;
;;; Copyright 2005, 2006, 2008, 2009 Ludovic Court�s <ludo@gnu.org>
;;; Copyright 2003, 2004 Erick Gallesio - I3S-CNRS/ESSI <eg@essi.fr>
;;;
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
;;; USA.
(define-module (skribilo writer)
:export (<writer> writer? writer-options writer-ident
writer-before writer-action writer-after writer-class
invoke markup-writer markup-writer-get markup-writer-get*
lookup-markup-writer copy-markup-writer)
:use-module (skribilo utils syntax)
:autoload (srfi srfi-1) (find filter)
:autoload (srfi srfi-34) (raise)
:autoload (skribilo engine) (engine? engine-ident? default-engine)
:use-module (srfi srfi-35)
:use-module (skribilo condition)
:use-module (skribilo debug)
:use-module (skribilo ast)
:use-module (oop goops)
:use-module (ice-9 optargs))
(fluid-set! current-reader %skribilo-module-reader)
;;;
;;; Class definition.
;;;
(define-class <writer> ()
(ident :init-keyword :ident :init-value '??? :getter writer-ident)
(class :init-keyword :class :init-value 'unspecified
:getter writer-class)
(pred :init-keyword :pred :init-value 'unspecified)
(upred :init-keyword :upred :init-value 'unspecified)
(options :init-keyword :options :init-value '() :getter writer-options)
(verified? :init-keyword :verified? :init-value #f)
(validate :init-keyword :validate :init-value #f)
(before :init-keyword :before :init-value #f :getter writer-before)
(action :init-keyword :action :init-value #f :getter writer-action)
(after :init-keyword :after :init-value #f :getter writer-after))
(define (writer? obj)
(is-a? obj <writer>))
(define-method (write (obj <writer>) port)
(format port "#[~A (~A) ~A]"
(class-name (class-of obj))
(slot-ref obj 'ident)
(object-address obj)))
;;;
;;; Writer methods.
;;;
(define (invoke proc node e)
(with-debug 5 'invoke
(debug-item "e=" (engine-ident e))
(debug-item "node=" node " " (if (markup? node) (markup-markup node) ""))
(if (string? proc)
(display proc)
(if (procedure? proc)
(proc node e)))))
(define %using-vm?
;; #t if using Guile's VM.
(false-if-exception (resolve-module '(system vm program))))
(define (%procedure-arity proc)
(if (and %using-vm?
((@ (system vm program) program?) proc))
(car ((@ (system vm program) program-arity) proc))
(car (procedure-property proc 'arity))))
(define (make-writer-predicate markup predicate class)
(let* ((t2 (if class
(lambda (n e)
(equal? (markup-class n) class))
#f)))
(if predicate
(cond
((or (not (procedure? predicate))
(not (eq? (%procedure-arity predicate) 2)))
(raise (condition (&invalid-argument-error
(proc-name make-writer-predicate)
(argument predicate)))))
(else
(if (procedure? t2)
(lambda (n e)
(and (t2 n e) (predicate n e)))
predicate)))
t2)))
;;;
;;; `markup-writer'
;;;
(define* (markup-writer markup ;; #:optional (engine #f)
#:key (predicate #f) (class #f) (options '())
(validate #f)
(before #f)
(action 'unspecified)
(after #f)
#:rest engine)
;;; XXX: `lambda*' sucks and fails when both optional arguments and
;;; keyword arguments are used together. In particular, if ENGINE is not
;;; specified by the caller but other keyword arguments are specified, it
;;; will consider the value of ENGINE to be the first keyword found.
; (let ((e (or engine (default-engine))))
(let ((e (or (if (and (list? engine) (not (keyword? (car engine))))
(car engine)
#f)
(default-engine))))
(cond
((and (not (symbol? markup)) (not (eq? markup #t)))
(raise (condition (&invalid-argument-error
(proc-name 'markup-writer)
(argument markup)))))
((not (engine? e))
(raise (condition (&invalid-argument-error
(proc-name 'markup-writer)
(argument e)))))
((and (not predicate)
(not class)
(null? options)
(not before)
(eq? action 'unspecified)
(not after))
(raise (condition (&invalid-argument-error
(proc-name 'markup-writer)
(argument (list predicate class options
before action after))))))
(else
(let ((m (make-writer-predicate markup predicate class))
(ac (if (eq? action 'unspecified)
(lambda (n e)
((@ (skribilo output) output)
(markup-body n) e))
action)))
(engine-add-writer! e markup m predicate
options before ac after class validate))))))
;;;
;;; Finding a markup writer.
;;;
(define (lookup-markup-writer node e)
;; Find the writer that applies best to NODE. See also `markup-writer-get'
;; and `markup-writer-get*'.
(define (matching-writer writers)
(find (lambda (w)
(let ((pred (slot-ref w 'pred)))
(if (procedure? pred)
(pred node e)
#t)))
writers))
(let* ((writers (slot-ref e 'writers))
(node-writers (hashq-ref writers (markup-markup node) '()))
(delegate (slot-ref e 'delegate)))
(or (matching-writer node-writers)
(matching-writer (slot-ref e 'free-writers))
(and (engine? delegate)
(lookup-markup-writer node delegate)))))
(define* (markup-writer-get markup :optional engine :key (class #f) (pred #f))
;; Get a markup writer for MARKUP (a symbol) in ENGINE, with class CLASS
;; and user predicate PRED. [FIXME: Useless since PRED is a procedure and
;; therefore not comparable?]
(define (matching-writer writers)
(find (lambda (w)
(and (if class (equal? (writer-class w) class) #t)
(or (unspecified? pred)
(eq? (slot-ref w 'upred) pred))))
writers))
(let ((e (or engine (default-engine))))
(cond
((not (symbol? markup))
(raise (condition (&invalid-argument-error
(proc-name 'markup-writer-get)
(argument markup)))))
((not (engine? e))
(raise (condition (&invalid-argument-error
(proc-name 'markup-writer-get)
(argument e)))))
(else
(let* ((writers (slot-ref e 'writers))
(markup-writers (hashq-ref writers markup '()))
(delegate (slot-ref e 'delegate)))
(or (matching-writer markup-writers)
(and (engine? delegate)
(markup-writer-get markup delegate
:class class :pred pred))))))))
(define* (markup-writer-get* markup #:optional engine #:key (class #f))
;; Finds all writers, recursively going through the engine hierarchy, that
;; match MARKUP with optional CLASS attribute.
(define (matching-writers writers)
(filter (lambda (w)
(or (not class)
(equal? (writer-class w) class)))
writers))
(let ((e (or engine (default-engine))))
(cond
((not (symbol? markup))
(raise (condition (&invalid-argument-error
(proc-name 'markup-writer-get*)
(argument markup)))))
((not (engine? e))
(raise (condition (&invalid-argument-error
(proc-name 'markup-writer-get*)
(argument e)))))
(else
(let* ((writers (slot-ref e 'writers))
(delegate (slot-ref e 'delegate)))
(append (matching-writers writers)
(if (engine? delegate)
(markup-writer-get* markup delegate :class class)
'())))))))
(define* (copy-markup-writer markup old-engine ;; #:optional new-engine
:key (predicate 'unspecified)
(class 'unspecified)
(options 'unspecified)
(validate 'unspecified)
(before 'unspecified)
(action 'unspecified)
(after 'unspecified)
:rest args)
(define new-engine
;; XXX: Work around `lambda*' suckingness (see `markup-writer').
(and (not (null? args))
(car args)))
(let ((old (markup-writer-get markup old-engine))
(new-engine (or new-engine old-engine)))
(markup-writer markup new-engine
:pred (if (unspecified? predicate) (slot-ref old 'pred) predicate)
:class (if (unspecified? class) (slot-ref old 'class) class)
:options (if (unspecified? options) (slot-ref old 'options) options)
:validate (if (unspecified? validate) (slot-ref old 'validate) validate)
:before (if (unspecified? before) (slot-ref old 'before) before)
:action (if (unspecified? action) (slot-ref old 'action) action)
:after (if (unspecified? after) (slot-ref old 'after) after))))
;;; writer.scm ends here
|