aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribilo/prog.scm
blob: 3c881b51b22ff4c9fa3130b8494635c84a978ea0 (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
;;; prog.scm  --  All the stuff for the prog markup
;;; -*- coding: iso-8859-1 -*-
;;;
;;; Copyright 2006, 2007, 2009  Ludovic Court�s  <ludo@gnu.org>
;;; Copyright 2003  Erick Gallesio - I3S-CNRS/ESSI <eg@essi.fr>
;;;
;;;
;;; This file is part of Skribilo.
;;;
;;; Skribilo 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 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Skribilo 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 Skribilo.  If not, see <http://www.gnu.org/licenses/>.

(define-module (skribilo prog)
  :use-module (ice-9 regex)
  :use-module (srfi srfi-1)
  :use-module (srfi srfi-11)

  :use-module (skribilo lib)  ;; `new'
  :use-module (skribilo ast)
  :use-module (skribilo utils syntax)

  :export (make-prog-body resolve-line))

(skribilo-module-syntax)


;;;
;;; Bigloo compatibility.
;;;

(define pregexp-match 	string-match)
(define pregexp-replace (lambda (rx str what)
			  (regexp-substitute/global #f rx str
						    'pre what 'post)))
(define pregexp-quote   regexp-quote)


(define node-body-set! markup-body-set!)

;;;
;;; FIXME: Tout le module peut se factoriser
;;;        d�finir en bigloo  node-body-set


;*---------------------------------------------------------------------*/
;*    resolve-line ...                                                 */
;*---------------------------------------------------------------------*/
(define (resolve-line doc id)
  (document-lookup-node doc id))

;*---------------------------------------------------------------------*/
;*    extract-string-mark ...                                          */
;*---------------------------------------------------------------------*/
(define (extract-string-mark line regexp)
  (let ((match (pregexp-match regexp line)))
    (if match
        (values (match:substring match 1)
                (pregexp-replace regexp line ""))
        (values #f line))))

;*---------------------------------------------------------------------*/
;*    extract-mark ...                                                 */
;*    -------------------------------------------------------------    */
;*    Extract the prog mark from a line.                               */
;*---------------------------------------------------------------------*/
(define (extract-mark line regexp)
   (cond
      ((not regexp)
       (values #f line))
      ((string? line)
       (extract-string-mark line regexp))
      ((list? line)
       (let loop ((ls line)
		  (res '()))
	  (if (null? ls)
	      (values #f line)
	      (let-values (((m l)
                            (extract-mark (car ls) regexp)))
		 (if (not m)
		     (loop (cdr ls) (cons l res))
		     (values m (append (reverse! res) (cons l (cdr ls)))))))))
      ((node? line)
       (let-values (((m l)
                     (extract-mark (node-body line) regexp)))
	  (if (not m)
	      (values #f line)
	      (begin
		 (node-body-set! line l)
		 (values m line)))))
      (else
       (values #f line))))

;*---------------------------------------------------------------------*/
;*    split-line ...                                                   */
;*---------------------------------------------------------------------*/
(define (split-line line)
   (cond
      ((string? line)
       (let ((l (string-length line)))
	  (let loop ((r1 0)
		     (r2 0)
		     (res '()))
	     (cond
		((= r2 l)
		 (if (= r1 r2)
		     (reverse! res)
		     (reverse! (cons (substring line r1 r2) res))))
		((char=? (string-ref line r2) #\Newline)
		 (loop (+ r2 1)
		       (+ r2 1)
		       (if (= r1 r2)
			   (cons 'eol res)
			   (cons* 'eol (substring line r1 r2) res))))
		(else
		 (loop r1
		       (+ r2 1)
		       res))))))
      ((list? line)
       (let loop ((ls line)
		  (res '()))
	  (if (null? ls)
	      res
	      (loop (cdr ls) (append res (split-line (car ls)))))))
      (else
       (list line))))

;*---------------------------------------------------------------------*/
;*    flat-lines ...                                                   */
;*---------------------------------------------------------------------*/
(define (flat-lines lines)
   (concatenate (map split-line lines)))

;*---------------------------------------------------------------------*/
;*    collect-lines ...                                                */
;*---------------------------------------------------------------------*/
(define (collect-lines lines)
   (let loop ((lines (flat-lines lines))
	      (res '())
	      (tmp '()))
      (cond
	 ((null? lines)
	  (reverse! (cons (reverse! tmp) res)))
	 ((eq? (car lines) 'eol)
	  (cond
	     ((null? (cdr lines))
	      (reverse! (cons (reverse! tmp) res)))
	     ((and (null? res) (null? tmp))
	      (loop (cdr lines)
		    res
		    '()))
	     (else
	      (loop (cdr lines)
		    (cons (reverse! tmp) res)
		    '()))))
	 (else
	  (loop (cdr lines)
		res
		(cons (car lines) tmp))))))

;*---------------------------------------------------------------------*/
;*    make-prog-body ...                                               */
;*---------------------------------------------------------------------*/
(define (make-prog-body src lnum-init ldigit mark)
   (let* ((regexp (and mark
		       (format #f "~a([-a-zA-Z_][-0-9a-zA-Z_]+)"
			       (pregexp-quote mark))))
	  (src (cond
		  ((not (pair? src)) (list src))
		  ((and (pair? (car src)) (null? (cdr src))) (car src))
		  (else src)))
	  (lines (collect-lines src))
	  (lnum (if (integer? lnum-init) lnum-init 1))
	  (s (number->string (+ (if (integer? ldigit)
				    (max lnum (expt 10 (- ldigit 1)))
				    lnum)
				(length lines)))))
     (let loop ((lines lines)
                (lnum lnum)
                (res '()))
	 (if (null? lines)
	     (reverse! res)
             (let-values (((m l)
                           (extract-mark (car lines) regexp)))
		(let* ((line-ident (symbol->string (gensym "&prog-line")))
		       (n (new markup
			     (markup  '&prog-line)
			     (ident   (or m line-ident))
                             (options `((:number ,(and lnum-init lnum))))
			     (body    l))))
 		   (loop (cdr lines)
 			 (+ lnum 1)
 			 (cons n res))))))))