2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4
;; MODULE : tmmlout.scm
5
;; DESCRIPTION : generation of Xml from scheme expressions
6
;; COPYRIGHT : (C) 2002 Joris van der Hoeven
8
;; This software falls under the GNU general public license and comes WITHOUT
9
;; ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for details.
10
;; If you don't have this file, write to the Free Software Foundation, Inc.,
11
;; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15
(texmacs-module (convert tmml tmmlout)
16
(:use (convert tools output))
17
(:export serialize-tmml))
19
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20
;; Determining output layout
21
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23
(define (tmmlout-big? doc)
24
(cond ((not (pair? doc)) #f)
25
((func? doc '!document) #t)
26
((func? doc 'tformat) #t)
27
((func? doc 'table) #t)
28
((func? doc 'collection) #t)
29
((func? doc 'associate) #t)
30
((func? doc 'tm-par) #t)
31
(else (list-or (map tmmlout-big? (cdr doc)))))
35
(define (tmmlout-preserve-one? x first? last?)
36
(cond ((func? x '!concat) (tmmlout-preserve? (cdr x) first? last?))
37
((not (string? x)) #f)
38
((and first? (string-starts? x " ")) #t)
39
((and last? (string-ends? x " ")) #t)
40
(else (>= (string-search-forwards " " 0 x) 0))))
42
(define (tmmlout-preserve? l first? last?)
44
(or (tmmlout-preserve-one? (car l) first? (and last? (null? (cdr l))))
45
(tmmlout-preserve? (cdr l) #f last?))))
47
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48
;; Outputting main flow
49
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51
(define (tmmlout-indent plus big? preserve?)
52
(cond (preserve? (noop))
53
(big? (output-indent plus) (output-lf))
56
(define (tmmlout-attr x)
57
;(display-err* "[tmmlout-attr] " x "\n")
58
(output-text " " (symbol->string (car x)) "=")
59
(output-verbatim "\"" (string-replace (cadr x) "\"" "\\\"") "\""))
61
(define (tmmlout-stacked-args l)
65
(if (not (null? (cdr l)))
69
(tmmlout-stacked-args (cdr l)))))
71
(define (tmmlout-args l big? preserve?)
72
;(display-err* "[tmmlout-args] " l ", " big? ", " preserve? "\n")
73
(cond ((null? l) (noop))
76
(output-verbatim (car l))
77
(output-text (car l)))
78
(tmmlout-args (cdr l) big? preserve?))
79
((func? (car l) '!concat)
80
(tmmlout-args (cdar l) #f preserve?)
81
(tmmlout-args (cdr l) big? preserve?))
82
((func? (car l) '!document)
83
(tmmlout-args (cdar l) big? preserve?)
84
(tmmlout-args (cdr l) big? preserve?))
85
((func? (car l) '!stacked)
86
(tmmlout-stacked-args (cdar l))
87
(tmmlout-args (cdr l) big? preserve?))
92
(not (string? (car l)))
93
(not (string? (cadr l))))
96
(if (func? (cadr l) 'tm-par) (output-lf))))
97
(tmmlout-args (cdr l) big? preserve?))))
99
(define (tmmlout-tag tag attrs args)
100
;(display-err* "[tmmlout-tag] " tag ", " attrs ", " args "\n")
101
(let* ((big? (tmmlout-big? (cons tag args)))
102
(preserve? (tmmlout-preserve? args #t #t)))
103
(if preserve? (set! attrs `((xml:space "preserve") ,@attrs)))
105
(output-text (symbol->string tag))
106
(for-each tmmlout-attr attrs)
107
(if (null? args) (output-text "/"))
109
(if (not (null? args))
111
(tmmlout-indent 2 big? preserve?)
112
(tmmlout-args args big? preserve?)
113
(tmmlout-indent -2 big? preserve?)
114
(output-text "</" (symbol->string tag) ">")))))
116
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117
;; Main output routines
118
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121
;(display-err* "[tmmlout] " x "\n")
122
(cond ((string? x) (output-text x))
125
(output-text "<?" (symbol->string (cadr x)) " " (caddr x) "?>"))
126
((and (pair? (cdr x)) (func? (cadr x) '@))
127
(tmmlout-tag (car x) (cdadr x) (cddr x)))
128
((func? x '!concat) (tmmlout-args (cdr x) #f #t))
129
((func? x '!document) (tmmlout-args (cdr x) #t #f))
130
((func? x '!stacked) (tmmlout-stacked-args (cdr x)))
131
((func? x '*TOP*) (tmmlout-stacked-args (cdr x)))
132
(else (tmmlout-tag (car x) '() (cdr x)))))
134
(define (serialize-tmml x)