]> git.donarmstrong.com Git - lilypond.git/blob - scm/to-xml.scm
5f2828a3704089de71308354bbc80c975d98d4a6
[lilypond.git] / scm / to-xml.scm
1 ;;;; to-xml.scm -- dump parse tree as xml
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2003--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 ;;;;                 Jan Nieuwenhuizen <janneke@gnu.org>
7
8 (use-modules (ice-9 regex)
9              (srfi srfi-1)
10              (oop goops))
11
12 "
13 Todo: this is a quick hack; it makes more sense to define a GOOPS
14 class of a documentnode (similar to how
15 ; the documentation is generated.)
16
17 That is much cleaner: building the document, and dumping it to output
18 is then separated.
19
20
21    foo = \\score { ... }
22
23    #(as-xml foo)
24
25    <score>
26      <music></music>
27      <layoutoutput>
28      </layoutoutput>
29    </score>
30 "
31
32 (define-class <xml-node> ()
33   (name #:init-value "" #:accessor node-name #:init-keyword #:name)
34   (value #:init-value "" #:accessor node-value #:init-keyword #:value)
35   (attributes #:init-value '()
36               #:accessor node-attributes
37               #:init-keyword #:attributes)
38   (children #:init-value '()
39             #:accessor node-children
40             #:init-keyword #:children))
41
42 (define node-names
43   '((NoteEvent . note)
44     (SequentialMusic . measure)
45
46     ;;ugh
47     (pitch . pitch)
48     (duration . duration)
49     (octave . octave)
50     (step . step)))
51
52 (define (musicxml-node->string node)
53   (let ((xml-name (assoc-get (node-name node) node-names #f)))
54     (string-append
55      (if xml-name (open-tag xml-name '() '()) "")
56      (if (equal? (node-value node) "")
57          (string-append
58           (if xml-name "\n" "")
59           (apply string-append (map musicxml-node->string (node-children node))))
60          (node-value node))
61      (if xml-name (close-tag xml-name) "")
62      (if xml-name "\n" ""))))
63
64 (define (xml-node->string node)
65   (string-append
66    "\n"
67    (open-tag (node-name node) (node-attributes node) '())
68    (if (equal? (node-value node) "")
69        (string-append
70         (apply string-append (map xml-node->string (node-children node))))
71        (node-value node))
72    "\n"
73    (close-tag (node-name node))))
74
75 (define (musicxml-duration->xml-node d)
76   (make <xml-node>
77     #:name 'duration
78     #:value (number->string (ash 1 (ly:duration-log d)))))
79
80 (define (duration->xml-node d)
81   (make <xml-node>
82     #:name 'duration
83     ;; #:value (number->string (ash 1 (ly:duration-log d)))))
84     #:attributes `((log . ,(ly:duration-log d))
85                    (dots . ,(ly:duration-dot-count d))
86                    (numer . ,(car (ly:duration-factor d)))
87                    (denom . ,(cdr (ly:duration-factor d))))))
88
89 (define (musicxml-pitch->xml-node p)
90   (make <xml-node>
91     #:name 'pitch
92     #:children
93     (list
94      (make <xml-node>
95        #:name 'step
96        #:value (list-ref  '("C" "D" "E" "F" "G" "A" "B")
97                           (ly:pitch-notename p)))
98      (make <xml-node>
99        #:name 'octave
100        #:value (number->string (ly:pitch-octave p))))))
101
102 (define (pitch->xml-node p)
103   (make <xml-node>
104     #:name 'pitch
105     #:attributes `((octave . ,(ly:pitch-octave p))
106                    (notename . ,(ly:pitch-notename p))
107                    (alteration . ,(ly:pitch-alteration p)))))
108
109 (define (music->xml-node music)
110   (let* ((name (ly:music-property music 'name))
111          (e (ly:music-property music 'element))
112          (es (ly:music-property music 'elements))
113          (mprops (ly:music-mutable-properties music))
114          (d (ly:music-property music 'duration))
115          (p (ly:music-property music 'pitch))
116          (ignore-props '(origin elements duration pitch element)))
117     
118     (make <xml-node>
119       #:name name
120       #:children
121       (apply
122        append
123        (if (ly:pitch? p) (list (pitch->xml-node p)) '())
124        (if (ly:duration? d) (list (duration->xml-node d)) '())
125        (if (pair? es) (map music->xml-node es) '())
126        (if (ly:music? e) (list (music->xml-node e)) '())
127        '()))))
128
129 (define (dtd-header)
130   (string-append
131    "<?xml version=\"1.0\"?>
132 <!DOCTYPE MUSIC ["
133    preliminary-dtd
134    "
135 ]>
136
137 "))
138
139
140 ;; as computed from input/trip.ly, by
141 ;; http://www.pault.com/pault/dtdgenerator/
142
143 ;; must recompute with larger, more serious piece, and probably
144 ;; manually add stuff
145 (define preliminary-dtd
146   "
147 <!ELEMENT duration EMPTY >
148 <!ATTLIST duration denom ( 1 | 3 | 5 ) #REQUIRED >
149 <!ATTLIST duration dots ( 0 | 1 ) #REQUIRED >
150 <!ATTLIST duration log ( 0 | 1 | 2 | 3 | 4 ) #REQUIRED >
151 <!ATTLIST duration numer ( 1 | 4 ) #REQUIRED >
152
153 <!ELEMENT music ( duration | music | pitch )* >
154 <!ATTLIST music articulation-type ( lheel | ltoe | marcato | rheel | rtoe | staccato | tenuto ) #IMPLIED >
155 <!ATTLIST music change-to-id NMTOKEN #IMPLIED >
156 <!ATTLIST music change-to-type NMTOKEN #IMPLIED >
157 <!ATTLIST music context-id CDATA #IMPLIED >
158 <!ATTLIST music context-type ( PianoStaff | Score | Staff | Timing | Voice ) #IMPLIED >
159 <!ATTLIST music denominator NMTOKEN #IMPLIED >
160 <!ATTLIST music direction ( 0 | 1 ) #IMPLIED >
161 <!ATTLIST music force-accidental CDATA #IMPLIED >
162 <!ATTLIST music grob-property NMTOKEN #IMPLIED >
163 <!ATTLIST music grob-value CDATA #IMPLIED >
164 <!ATTLIST music iterator-ctor CDATA #IMPLIED >
165 <!ATTLIST music label NMTOKEN #IMPLIED >
166 <!ATTLIST music last-pitch CDATA #IMPLIED >
167 <!ATTLIST music numerator NMTOKEN #IMPLIED >
168 <!ATTLIST music penalty NMTOKEN #IMPLIED >
169 <!ATTLIST music pitch-alist CDATA #IMPLIED >
170 <!ATTLIST music pop-first CDATA #IMPLIED >
171 <!ATTLIST music repeat-count NMTOKEN #IMPLIED >
172 <!ATTLIST music span-direction ( -1 | 1 ) #IMPLIED >
173 <!ATTLIST music span-type NMTOKEN #IMPLIED >
174 <!ATTLIST music symbol NMTOKEN #IMPLIED >
175 <!ATTLIST music text NMTOKEN #IMPLIED >
176 <!ATTLIST music text-type NMTOKEN #IMPLIED >
177 <!ATTLIST music type NMTOKEN #REQUIRED >
178 <!ATTLIST music value CDATA #IMPLIED >
179
180 <!ELEMENT pitch EMPTY >
181 <!ATTLIST pitch alteration ( 0 | 1 ) #REQUIRED >
182 <!ATTLIST pitch notename ( 0 | 1 | 2 | 3 | 4 | 5 | 6 ) #REQUIRED >
183 <!ATTLIST pitch octave ( -1 | -2 | 0 | 1 ) #REQUIRED >")
184
185
186 ;; should use macro
187 (define (assert x)
188   (if x
189       #t
190       (ly:error (_ "assertion failed"))))
191
192 (define (re-sub re to string)
193   (regexp-substitute/global #f re string 'pre to 'post))
194
195 (define (re-sub-alist string alist)
196   (if (null? alist)
197       string
198       (re-sub (caar alist) (cdar alist)
199               (re-sub-alist string (cdr alist)))))
200
201 (define xml-entities-alist
202   '(("\"" . "&quot;")
203     ("<" . "&lt;")
204     (">" . "&gt;")
205     ("'" . "&apos;")
206     ("&" . "&amp;")))
207
208 (define (open-tag tag attrs exceptions)
209   (define (candidate? x)
210     (not (memq (car x) exceptions)))
211   
212   (define (dump-attr sym-val)
213     (let* ((sym (car sym-val))
214            (val (cdr sym-val)))
215       
216       (string-append
217        "\n   "
218        (symbol->string sym)
219        "=\""
220        (let ((s (call-with-output-string (lambda (port) (display val port)))))
221          (re-sub-alist s xml-entities-alist))
222        "\"")))
223
224   (string-append
225    "<" (symbol->string tag)
226    (apply string-append (map dump-attr (filter candidate? attrs)))
227    ">"))
228
229 (define (close-tag name)
230   (string-append "</" (symbol->string name) ">"))
231
232 (define-public (music-to-xml music port)
233   "Dump XML-ish stuff to PORT."
234
235   ;; dtd contains # -- This confuses tex during make web.
236   ;;
237   ;;  (display (dtd-header) port)
238   
239   (display (open-tag 'music '((type . score)) '()) port)
240   (display (xml-node->string (music->xml-node music)) port)
241   (display (close-tag 'music) port))
242
243 (define-public (music-to-musicxml music port)
244   "Dump MusicXML-ish stuff to PORT."
245
246   ;; dtd contains # -- This confuses tex during make web.
247   ;;
248   ;;  (display (dtd-header) port)
249
250   (define pitch->xml-node musicxml-pitch->xml-node)
251   (define duration->xml-node musicxml-duration->xml-node)
252   
253   (display (open-tag 'music '((type . score)) '()) port)
254   (display (musicxml-node->string (music->xml-node music)) port)
255   (display (close-tag 'music) port))
256