2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5
;; DESCRIPTION : conversion of Xml trees to TeXmacs trees
6
;; COPYRIGHT : (C) 2003 Joris van der Hoeven
8
;; This software falls under the GNU general public license version 3 or later.
9
;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
10
;; in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
12
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
(texmacs-module (convert tmml tmmltm)
15
(:use (convert tools tmconcat)))
17
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18
;; Provide the inverse functionality of tmmlout
19
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21
(define (xmlin-make tag attrs args impl)
22
(if (nnull? attrs) (set! attrs (list (cons '@ attrs))))
23
(if (and (nnull? args) (func? (car args) 'tm-attr))
25
(set! attrs (append attrs (list (car args))))
26
(set! args (cdr args))))
27
(if (and impl (not (and (== impl '!concat) (<= (length args) 1))))
28
(set! args (list (cons impl args))))
29
`(,tag ,@attrs ,@args))
31
(define (xmlin-special tag attrs l)
32
(with args (map xmlin (list-filter l (lambda (x) (nstring? x))))
33
(with doc? (list-or (map (lambda (x) (func? x 'tm-par)) args))
34
(xmlin-make tag attrs args (if doc? '!document #f)))))
36
(define (xmlin-unspace x first? last? preserve?)
37
(cond ((and (string? x) preserve?) x)
38
((string? x) (xml-unspace x first? last?))
41
(define (xmlin-unspace-args l first? preserve?)
43
((null? (cdr l)) (list (xmlin-unspace (car l) first? #t preserve?)))
44
(else (cons (xmlin-unspace (car l) first? #f preserve?)
45
(xmlin-unspace-args (cdr l) #f preserve?)))))
47
(define (xmlin-regular tag attrs* args*)
48
(let* ((search '(xml:space "preserve"))
49
(preserve? (in? search attrs*))
50
(attrs (list-filter attrs* (lambda (x) (!= x search))))
51
(args (xmlin-unspace-args args* #t preserve?)))
52
(set! args (list-filter args (lambda (x) (!= x ""))))
53
(if (and (null? args) (in? tag '(tm-arg tm-par))) (set! args '("")))
54
(xmlin-make tag attrs args '!concat)))
57
;(display* "[xmlin] " x "\n")
60
(attrs? (and (pair? (cdr x)) (func? (cadr x) '@)))
61
(attrs (if attrs? (cdadr x) '()))
62
(args (if attrs? (cddr x) (cdr x))))
63
(cond ((== tag '*TOP*)
64
(with r (xmlin-special tag attrs args)
65
(if (match? r '(*TOP* (*PI* xml :*) (TeXmacs (@ :*) :*)))
68
(TeXmacs ,(cadr (caddr r))
69
(!stacked ,@(cddr (caddr r)))))
70
(xmlin-regular tag attrs args))))
73
(xmlin-special tag attrs args))
74
((list-or (map (lambda (x) (func? x 'tm-arg)) args))
75
(xmlin-special tag attrs args))
76
((list-or (map (lambda (x) (func? x 'tm-par)) args))
77
(xmlin-special tag attrs args))
78
(else (xmlin-regular tag attrs args))))))
80
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
81
;; Provide the inverse functionality of tmxml
82
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84
(define tmmltm-current-version "9.9.9.9")
86
(define (tmmltm-string s)
87
(if (version-before? tmmltm-current-version "1.0.5.8")
91
(define (tmmltm-file version args)
92
(with old-version tmmltm-current-version
93
(set! tmmltm-current-version version)
94
(with r `(!file (document (TeXmacs ,version) ,@(map tmmltm args)))
95
(set! tmmltm-current-version old-version)
98
(define (tmmltm-document l)
99
(cons 'document (map (lambda (x) (tmmltm (cadr x))) l)))
101
(define (tmmltm-concat l)
102
(with r (tmconcat-simplify (map tmmltm l))
104
((null? (cdr r)) (car r))
105
(else (cons 'concat r)))))
107
(define (tmmltm-with x)
108
(with (tag attr arg) (tmmltm-regular (car x) (cdr x))
109
`(with ,@(cdr attr) ,arg)))
111
(define (tmmltm-ungroup-attrs attrs)
112
(if (null? attrs) attrs
113
(cons* (xml-name->tm (symbol->string (caar attrs)))
115
(tmmltm-ungroup-attrs (cdr attrs)))))
117
(define (tmmltm-attributed tag attrs args)
118
(cond ((null? attrs) (cons tag args))
119
((== (car attrs) "tm-dyn")
120
(tmmltm-attributed (string->symbol (xml-name->tm (cadr attrs)))
122
(cons (symbol->string tag) args)))
123
(else `(,tag (attr ,@attrs) ,@args))))
125
(define (tmmltm-arg x)
126
;(display* "x= " x "\n")
127
;(display* "test? " (func? x 'tm-arg 1) "\n")
128
(if (func? x 'tm-arg 1)
130
;(display* "y= " y "\n")
132
;(display* "r= " r "\n")
136
(define (tmmltm-args l)
139
(define (tmmltm-regular tag* args)
140
(with tag (string->symbol (xml-name->tm (symbol->string tag*)))
141
(cond ((and (pair? args) (func? (car args) '@))
142
(tmmltm-attributed tag
143
(tmmltm-ungroup-attrs (cdar args))
144
(tmmltm-args (cdr args))))
145
((and (pair? args) (func? (car args) 'tm-attr))
146
(tmmltm-attributed tag
147
(tmmltm-args (cdar args))
148
(tmmltm-args (cdr args))))
149
(else (cons tag (tmmltm-args args))))))
151
(tm-define (tmmltm x)
152
;(display* "[tmmltm] ") (write x) (display* "\n")
153
(cond ((string? x) (tmmltm-string x))
154
((and (func? x '*TOP*) (>= (length x) 3) (func? (caddr x) 'TeXmacs 2))
156
((func? x '*TOP*) (tmmltm-concat (cdr x)))
158
((func? x 'TeXmacs 2) (tmmltm-file (cadadr (cadr x)) (cdr (caddr x))))
159
((func? x '!document) (tmmltm-document (cdr x)))
160
((func? x '!concat) (tmmltm-concat (cdr x)))
161
((func? x 'with) (tmmltm-with x))
162
((and (func? x 'tm-sym 1) (string? (cadr x)))
163
(string-append "<" (cadr x) ">"))
164
(else (tmmltm-regular (car x) (cdr x)))))
166
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170
(tm-define (parse-tmml s)
171
(:type (-> string stree))
172
(:synopsis "Parse a TeXmacs XML document @s.")
173
(with raw-xml (parse-xml s)
174
;(display* "raw= " raw-xml "\n")
177
(tm-define (tmml->texmacs tmml)
178
(:type (-> stree stree))
179
(:synopsis "Convert an TeXmacs XML stree @s into TeXmacs.")
180
(with doc (tmmltm tmml)
181
(if (func? doc '!file 1)
182
(tree->stree (upgrade-tmml (cadr doc)))