aboutsummaryrefslogtreecommitdiff
path: root/src/stklos/runtime.stk
blob: 58d0d456d603ada575bd4f0b5686efe3cf3d8548 (plain)
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
;;;;
;;;; runtime.stk	-- Skribe runtime system
;;;; 
;;;; Copyright � 2003-2004 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
;;;; USA.
;;;; 
;;;;           Author: Erick Gallesio [eg@essi.fr]
;;;;    Creation date: 13-Aug-2003 18:47 (eg)
;;;; Last file update: 15-Nov-2004 14:03 (eg)
;;;;

(define-module  SKRIBE-RUNTIME-MODULE
  (import SKRIBE-DEBUG-MODULE SKRIBE-VERIFY-MODULE SKRIBE-RESOLVE-MODULE
	  SKRIBE-OUTPUT-MODULE SKRIBE-EVAL-MODULE)

  (export ;; Utilities
   	  strip-ref-base ast->file-location string-canonicalize

	  ;; Markup functions
	  markup-option markup-option-add! markup-output

	  ;; Container functions
	  container-env-get

	  ;; Images
	  convert-image

	  ;; String writing
	  make-string-replace

	  ;; AST
	  ast->string
	  )

;;;; ======================================================================
;;;;
;;;;				U T I L I T I E S   		     
;;;;
;;;; ======================================================================
(define skribe-load 'function-defined-below)


;;FIXME:  Remonter cette fonction 
(define (strip-ref-base file)
  (if (not (string? *skribe-ref-base*))
      file
      (let ((l (string-length *skribe-ref-base*)))
	(cond
	  ((not (> (string-length file) (+ l 2)))
	   file)
	  ((not (substring=? file *skribe-ref-base* l))
	   file)
	  ((not (char=? (string-ref file l) (file-separator)))
	   file)
	  (else
	   (substring file (+ l 1) (string-length file)))))))
 

(define (ast->file-location ast)
   (let ((l (ast-loc ast)))
     (if (location? l)
	 (format "~a:~a:" (location-file l) (location-line l))
	 "")))

;; FIXME: Remonter cette fonction 
(define (string-canonicalize old)
   (let* ((l (string-length old))
	  (new (make-string l)))
      (let loop ((r 0)
		 (w 0)
		 (s #f))
	 (cond
	    ((= r l)
	     (cond
		((= w 0)
		 "")
		((char-whitespace? (string-ref new (- w 1)))
		 (substring new 0 (- w 1)))
		((= w r)
		 new)
		(else
		 (substring new 0 w))))
	    ((char-whitespace? (string-ref old r))
	     (if s
		 (loop (+ r 1) w #t)
		 (begin
		    (string-set! new w #\-)
		    (loop (+ r 1) (+ w 1) #t))))
	    ((or (char=? (string-ref old r) #\#)
		 (>= (char->integer (string-ref old r)) #x7f))
	     (string-set! new w #\-)
	     (loop (+ r 1) (+ w 1) #t))
	    (else
	     (string-set! new w (string-ref old r))
	     (loop (+ r 1) (+ w 1) #f))))))


;;;; ======================================================================
;;;;
;;;;   			M A R K U P S   F U N C T I O N S
;;;;
;;;; ======================================================================
;;; (define (markup-output markup
;; 		       :optional (engine    #f)
;; 		       :key 	 (predicate #f)
;; 		       		 (options  '())
;; 				 (before    #f)
;; 				 (action    #f)
;; 				 (after     #f))
;;   (let ((e (or engine (use-engine))))
;;     (cond
;;       ((not (is-a? e <engine>))
;;           (skribe-error 'markup-writer "illegal engine" e))
;;       ((and (not before)
;; 	    (not action)
;; 	    (not after))
;;           (%find-markup-output e markup))
;;       (else
;; 	  (let ((mp (if (procedure? predicate)
;; 			(lambda (n e) (and (is-markup? n markup) (predicate n e)))
;; 			(lambda (n e) (is-markup? n markup)))))
;; 	    (engine-output e markup mp options
;; 			   (or before (slot-ref e 'default-before))
;; 			   (or action (slot-ref e 'default-action))
;; 			   (or after  (slot-ref e 'default-after))))))))

(define (markup-option m opt)
  (if (markup? m)
      (let ((c (assq opt (slot-ref m 'options))))
	(and (pair? c) (pair? (cdr c))
	     (cadr c)))
      (skribe-type-error 'markup-option "Illegal markup: " m "markup")))


(define (markup-option-add! m opt val)
  (if (markup? m)
      (slot-set! m 'options (cons (list opt val)
				  (slot-ref m 'options)))
      (skribe-type-error 'markup-option "Illegal markup: " m "markup")))

;;;; ======================================================================
;;;;
;;;;   				C O N T A I N E R S
;;;;
;;;; ======================================================================
(define (container-env-get m key)
  (let ((c (assq key (slot-ref m 'env))))
    (and (pair? c) (cadr c))))


;;;; ======================================================================
;;;;
;;;;				I M A G E S
;;;;
;;;; ======================================================================
(define (builtin-convert-image from fmt dir)
  (let* ((s  (suffix from))
	 (f  (string-append (prefix (basename from)) "." fmt))
	 (to (string-append dir "/" f)))   ;; FIXME:
    (cond
      ((string=? s fmt)
       to)
      ((file-exists? to)
       to)
      (else 
       (let ((c (if (string=? s "fig")
		    (string-append "fig2dev -L " fmt " " from " > " to)
		    (string-append "convert " from " " to))))
	 (cond
	   ((> *skribe-verbose* 1)
	    (format (current-error-port) "  [converting image: ~S (~S)]" from c))
	   ((> *skribe-verbose* 0)
	    (format (current-error-port) "  [converting image: ~S]" from)))
	 (and (zero? (system c))
	      to))))))

(define (convert-image file formats)
  (let ((path (find-path file (skribe-image-path))))
    (if (not path)
	(skribe-error 'convert-image
		      (format "Can't find `~a' image file in path: " file)
		      (skribe-image-path))
	(let ((suf (suffix file)))
	  (if (member suf formats)
	      (let* ((dir (if (string? *skribe-dest*)
			      (dirname *skribe-dest*)
			      #f)))
		(if dir
		    (let ((dest (basename path)))
		      (copy-file path (make-path dir dest))
		      dest)
		    path))
	      (let loop ((fmts formats))
		(if (null? fmts)
		    #f
		     (let* ((dir (if (string? *skribe-dest*)
				     (dirname *skribe-dest*)
				     "."))
			    (p (builtin-convert-image path (car fmts) dir)))
		       (if (string? p)
			   p
			   (loop (cdr fmts)))))))))))

;;;; ======================================================================
;;;;
;;;;	      		S T R I N G - W R I T I N G
;;;;
;;;; ======================================================================

;; 
;; (define (%make-html-replace)
;;   ;; Ad-hoc version for HTML, a little bit faster than the
;;   ;; make-general-string-replace define later (particularily if there
;;   ;; is nothing to replace since, it does not allocate a new string
;;   (let ((specials (string->regexp "&|\"|<|>")))
;;     (lambda (str)
;;       (if (regexp-match specials str)
;; 	  (begin
;; 	    (let ((out (open-output-string)))
;; 	      (dotimes (i (string-length str))
;; 		(let ((ch (string-ref str i)))
;; 		  (case ch
;; 		    ((#\") (display "&quot;" out))
;; 		    ((#\&) (display "&amp;" out))
;; 		    ((#\<) (display "&lt;" out))
;; 		    ((#\>) (display "&gt;" out))
;; 		    (else  (write-char ch out)))))
;; 	      (get-output-string out)))
;; 	  str))))


(define (%make-general-string-replace lst)
  ;; The general version
  (lambda (str)
    (let ((out (open-output-string)))
      (dotimes (i (string-length str))
	(let* ((ch  (string-ref str i))
	       (res (assq ch lst)))
	  (display (if res (cadr res) ch) out)))
      (get-output-string out))))


(define (make-string-replace lst)
  (let ((l (sort lst (lambda (r1 r2) (char<? (car r1) (car r2))))))
    (cond
      ((equal? l '((#\" "&quot;") (#\& "&amp;") (#\< "&lt;") (#\> "&gt;")))
         string->html)
      (else
         (%make-general-string-replace lst)))))




;;;; ======================================================================
;;;;
;;;;   				O P T I O N S
;;;;
;;;; ======================================================================

;;NEW ;;
;;NEW ;; GET-OPTION
;;NEW ;; 
;;NEW (define (get-option obj key)
;;NEW   ;; This function either searches inside an a-list or a markup.
;;NEW   (cond
;;NEW     ((pair? obj)   (let ((c (assq key obj)))
;;NEW 		     (and (pair? c) (pair? (cdr c)) (cadr c))))
;;NEW     ((markup? obj) (get-option (slot-ref obj 'option*) key))
;;NEW     (else          #f)))
;;NEW 
;;NEW ;;
;;NEW ;; BIND-OPTION!
;;NEW ;;
;;NEW (define (bind-option! obj key value)
;;NEW   (slot-set! obj 'option* (cons (list key value)
;;NEW 				(slot-ref obj 'option*))))
;;NEW 
;;NEW 
;;NEW ;;
;;NEW ;; GET-ENV
;;NEW ;;
;;NEW (define (get-env obj key)
;;NEW   ;;  This function either searches inside an a-list or a container
;;NEW   (cond
;;NEW     ((pair? obj) 	(let ((c (assq key obj)))
;;NEW 			  (and (pair? c) (cadr c))))
;;NEW     ((container? obj)   (get-env (slot-ref obj 'env) key))
;;NEW     (else		#f)))
;;NEW 




;;;; ======================================================================
;;;;
;;;;   				    A S T 
;;;;
;;;; ======================================================================

(define-generic ast->string)


(define-method ast->string ((ast <top>))     "")
(define-method ast->string ((ast <string>))  ast)
(define-method ast->string ((ast <number>))  (number->string ast))

(define-method ast->string ((ast <pair>))
  (let ((out (open-output-string)))
    (let Loop ((lst ast))
      (cond
	((null? lst)
	   (get-output-string out))
	(else
	   (display (ast->string (car lst)) out)
	   (unless (null? (cdr lst))
	     (display #\space out))
	   (Loop (cdr lst)))))))

(define-method ast->string ((ast <node>))
  (ast->string (slot-ref ast 'body)))


;;NEW ;;
;;NEW ;; AST-PARENT
;;NEW ;;
;;NEW (define (ast-parent n)
;;NEW   (slot-ref n 'parent))
;;NEW 
;;NEW ;;
;;NEW ;; MARKUP-PARENT
;;NEW ;;
;;NEW (define (markup-parent m)
;;NEW   (let ((p (slot-ref m 'parent)))
;;NEW     (if (eq? p 'unspecified)
;;NEW 	(skribe-error 'markup-parent "Unresolved parent reference" m)
;;NEW 	p)))
;;NEW 
;;NEW 
;;NEW ;;
;;NEW ;; MARKUP-DOCUMENT
;;NEW ;;
;;NEW (define (markup-document m)
;;NEW   (let Loop ((p m)
;;NEW 	     (l #f))
;;NEW     (cond
;;NEW       ((is-markup? p 'document)           p)
;;NEW       ((or (eq? p 'unspecified) (not p))  l)
;;NEW       (else			          (Loop (slot-ref p 'parent) p)))))
;;NEW 
;;NEW ;;
;;NEW ;; MARKUP-CHAPTER
;;NEW ;;
;;NEW (define (markup-chapter m)
;;NEW   (let loop ((p m)
;;NEW 	     (l #f))
;;NEW     (cond
;;NEW       ((is-markup? p 'chapter)           p)
;;NEW       ((or (eq? p 'unspecified) (not p)) l)
;;NEW       (else				 (loop (slot-ref p 'parent) p)))))
;;NEW 
;;NEW 
;;NEW ;;;; ======================================================================
;;NEW ;;;;
;;NEW ;;;;   				H A N D L E S
;;NEW ;;;;
;;NEW ;;;; ======================================================================
;;NEW (define (handle-body h)
;;NEW   (slot-ref h 'body))
;;NEW 
;;NEW 
;;NEW ;;;; ======================================================================
;;NEW ;;;;
;;NEW ;;;; 				F I N D
;;NEW ;;;;
;;NEW ;;;; ======================================================================
;;NEW (define (find pred obj)
;;NEW   (with-debug 4 'find
;;NEW     (debug-item "obj=" obj)
;;NEW     (let loop ((obj (if (is-a? obj <container>) (container-body obj) obj)))
;;NEW       (cond
;;NEW 	((pair? obj)
;;NEW 	 (apply append (map (lambda (o) (loop o)) obj)))
;;NEW 	((is-a? obj <container>)
;;NEW 	 (debug-item "loop=" obj " " (slot-ref obj 'ident))
;;NEW 	 (if (pred obj)
;;NEW 	     (list (cons obj (loop (container-body obj))))
;;NEW 	     '()))
;;NEW 	(else
;;NEW 	 (if (pred obj)
;;NEW 	     (list obj)
;;NEW 	     '()))))))
;;NEW        

;;NEW ;;;; ======================================================================
;;NEW ;;;;
;;NEW ;;;; 		M A R K U P   A R G U M E N T   P A R S I N G
;;NEW ;;;
;;NEW ;;;; ======================================================================
;;NEW (define (the-body opt)
;;NEW   ;; Filter out the options
;;NEW   (let loop ((opt* opt)
;;NEW 	     (res '()))
;;NEW     (cond
;;NEW       ((null? opt*)
;;NEW        (reverse! res))
;;NEW       ((not (pair? opt*))
;;NEW        (skribe-error 'the-body "Illegal body" opt))
;;NEW       ((keyword? (car opt*))
;;NEW        (if (null? (cdr opt*))
;;NEW 	   (skribe-error 'the-body "Illegal option" (car opt*))
;;NEW 	   (loop (cddr opt*) res)))
;;NEW       (else
;;NEW        (loop (cdr opt*) (cons (car opt*) res))))))
;;NEW 
;;NEW 
;;NEW 
;;NEW (define (the-options opt+ . out)
;;NEW   ;; Returns an list made of options.The OUT argument contains 
;;NEW   ;; keywords that are filtered out.                                  
;;NEW   (let loop ((opt* opt+)
;;NEW 	     (res '()))
;;NEW     (cond
;;NEW       ((null? opt*)
;;NEW        (reverse! res))
;;NEW       ((not (pair? opt*))
;;NEW        (skribe-error 'the-options "Illegal options" opt*))
;;NEW       ((keyword? (car opt*))
;;NEW        (cond
;;NEW 	 ((null? (cdr opt*))
;;NEW 	  (skribe-error 'the-options "Illegal option" (car opt*)))
;;NEW 	 ((memq (car opt*) out)
;;NEW 	  (loop (cdr opt*) res))
;;NEW 	 (else
;;NEW 	  (loop (cdr opt*)
;;NEW 		(cons (list (car opt*) (cadr opt*)) res)))))
;;NEW       (else
;;NEW        (loop (cdr opt*) res)))))
;;NEW 


)