1 ;;;; (texinfo serialize) -- rendering stexinfo as texinfo
3 ;;;; Copyright (C) 2009 Free Software Foundation, Inc.
4 ;;;; Copyright (C) 2003,2004,2009 Andy Wingo <wingo at pobox dot com>
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 ;;Serialization of @code{stexi} to plain texinfo.
27 (define-module (texinfo serialize)
28 #:use-module (texinfo)
29 #:use-module (texinfo string-utils)
30 #:use-module (sxml transform)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-13)
33 #:export (stexi->texi))
35 (define (list-intersperse src-l elem)
36 (if (null? src-l) src-l
37 (let loop ((l (cdr src-l)) (dest (cons (car src-l) '())))
38 (if (null? l) (reverse dest)
39 (loop (cdr l) (cons (car l) (cons elem dest)))))))
41 ;; converts improper lists to proper lists.
42 (define (filter* pred l)
43 (let lp ((in l) (out '()))
47 (lp (cdr in) (if (pred (car in)) (cons (car in) out) out)))
49 (lp '() (if (pred in) (cons in out) out))))))
51 ;; (list* 'a '(b c) 'd '(e f g)) => '(a b c d e f g)
52 (define (list* . args)
53 (let* ((args (reverse args))
55 (let lp ((in (cdr args)) (out tail))
56 (cond ((null? in) out)
57 ((pair? (car in)) (lp (cdr in) (append (car in) out)))
58 ((null? (car in)) (lp (cdr in) out))
59 (else (lp (cdr in) (cons (car in) out)))))))
61 ;; Why? Well, because syntax-case defines `include', and carps about its
62 ;; wrong usage below...
63 (eval-when (eval load compile)
64 (define (include exp lp command type formals args accum)
69 " " command "@" accum)))
71 (define (empty-command exp lp command type formals args accum)
72 (list* " " command "@" accum))
74 (define (inline-text exp lp command type formals args accum)
75 (if (not (string=? command "*braces*")) ;; fixme :(
77 (append-map (lambda (x) (lp x '())) (reverse (cdr exp)))
78 "{" command "@" accum)
80 (append-map (lambda (x) (lp x '())) (reverse (cdr exp)))
83 (define (inline-args exp lp command type formals args accum)
92 (warn "Strange inline-args!" args))
94 (else (error "Invalid inline-args" args))))
96 (map (lambda (x) (assq-ref args x))
99 "{" command "@" accum))
101 (define (serialize-text-args lp formals args)
105 (map (lambda (arg) (append-map (lambda (x) (lp x '())) arg))
109 not (map (lambda (x) (assq-ref args x))
110 (reverse formals)))))
113 (define (eol-text-args exp lp command type formals args accum)
115 (serialize-text-args lp formals args)
116 " " command "@" accum))
118 (define (eol-text exp lp command type formals args accum)
120 (append-map (lambda (x) (lp x '()))
121 (reverse (if args (cddr exp) (cdr exp))))
122 " " command "@" accum))
124 (define (eol-args exp lp command type formals args accum)
129 (map (lambda (x) (assq-ref args x))
132 " " command "@" accum))
134 (define (environ exp lp command type formals args accum)
138 (append-map (lambda (x) (lp x '())) (reverse (cddr exp)))
139 "\n@c %**end of header\n\n"
140 (reverse (assq-ref args 'title)) "@settitle "
141 (or (and=> (assq-ref args 'filename)
143 (cons "\n" (reverse (cons "@setfilename " filename)))))
145 "\\input texinfo @c -*-texinfo-*-\n@c %**start of header\n"
148 (list* "\n\n" command "@end "
149 (let ((body (append-map (lambda (x) (lp x '()))
150 (reverse (if args (cddr exp) (cdr exp))))))
152 (eqv? (string-ref (car body)
153 (1- (string-length (car body))))
158 (serialize-text-args lp formals args)
159 " " command "@" accum))))
161 (define (table-environ exp lp command type formals args accum)
162 (list* "\n\n" command "@end "
163 (append-map (lambda (x) (lp x '()))
164 (reverse (if args (cddr exp) (cdr exp))))
166 (let* ((arg (if args (cadar args) ""))) ;; zero or one args
168 (list (symbol->string (car arg)) "@")
170 " " command "@" accum))
172 (define (wrap strings)
173 (fill-string (string-concatenate strings)
176 (define (paragraph exp lp command type formals args accum)
180 (append-map (lambda (x) (lp x '())) (reverse (cdr exp)))))
183 (define (item exp lp command type formals args accum)
184 (list* (append-map (lambda (x) (lp x '())) (reverse (cdr exp)))
188 (define (entry exp lp command type formals args accum)
189 (list* (append-map (lambda (x) (lp x '())) (reverse (cddr exp)))
191 (append-map (lambda (x) (lp x '())) (reverse (cdar args)))
195 (define (fragment exp lp command type formals args accum)
196 (list* "\n@c %end of fragment\n"
197 (append-map (lambda (x) (lp x '())) (reverse (cdr exp)))
198 "\n@c %start of fragment\n\n"
202 `((EMPTY-COMMAND . ,empty-command)
203 (INLINE-TEXT . ,inline-text)
204 (INLINE-ARGS . ,inline-args)
205 (EOL-TEXT . ,eol-text)
206 (EOL-TEXT-ARGS . ,eol-text-args)
207 (INDEX . ,eol-text-args)
208 (EOL-ARGS . ,eol-args)
210 (TABLE-ENVIRON . ,table-environ)
213 (PARAGRAPH . ,paragraph)
214 (FRAGMENT . ,fragment)
215 (#f . ,include))) ; support writing include statements
217 (define (serialize exp lp command type formals args accum)
218 ((or (assq-ref serializers type)
219 (error "Unknown command type" exp type))
220 exp lp command type formals args accum))
222 (define escaped-chars '(#\} #\{ #\@))
224 "Escapes any illegal texinfo characters (currently @{, @}, and @@)."
225 (let loop ((in (string->list str)) (out '()))
227 (apply string (reverse out))
228 (if (memq (car in) escaped-chars)
229 (loop (cdr in) (cons* (car in) #\@ out))
230 (loop (cdr in) (cons (car in) out))))))
232 (define (stexi->texi tree)
233 "Serialize the stexi @var{tree} into plain texinfo."
234 (string-concatenate-reverse
235 (let lp ((in tree) (out '()))
237 ((or (not in) (null? in)) out)
238 ((string? in) (cons (escape in) out))
240 (let ((command-spec (assq (car in) texi-command-specs)))
241 (if (not command-spec)
243 (warn "Unknown stexi command, not rendering" in)
247 (symbol->string (car in))
249 (filter* symbol? (cddr command-spec))
251 ((and (pair? (cdr in)) (pair? (cadr in))
254 ((not (cadr command-spec))
261 (error "Invalid stexi" in))))))
263 ;;; arch-tag: d3fa16ea-0bf7-4ec5-ab9f-3f08490f77f5