2 (use-modules (ice-9 regex)
7 Todo: this is a quick hack; it makes more sense to define a GOOPS
8 class of a documentnode (similar to how
9 ; the documentation is generated.)
11 That is much cleaner: building the document, and dumping it to output
26 (define-class <xml-node> ()
27 (name #:init-value "" #:accessor node-name #:init-keyword #:name)
28 (value #:init-value "" #:accessor node-value #:init-keyword #:value)
29 (attributes #:init-value '()
30 #:accessor node-attributes
31 #:init-keyword #:attributes)
32 (children #:init-value '()
33 #:accessor node-children
34 #:init-keyword #:children))
38 (SequentialMusic . measure)
47 (define (assoc-get-default key alist default)
48 "Return value if KEY in ALIST, else DEFAULT."
49 (let ((entry (assoc key alist)))
50 (if entry (cdr entry) default)))
52 (define (musicxml-node->string node)
53 (let ((xml-name (assoc-get-default (node-name node) node-names #f)))
55 (if xml-name (open-tag xml-name '() '()) "")
56 (if (equal? (node-value node) "")
59 (apply string-append (map musicxml-node->string (node-children node))))
61 (if xml-name (close-tag xml-name) "")
62 (if xml-name "\n" ""))))
64 (define (xml-node->string node)
67 (open-tag (node-name node) (node-attributes node) '())
68 (if (equal? (node-value node) "")
70 (apply string-append (map xml-node->string (node-children node))))
73 (close-tag (node-name node))))
75 (define (musicxml-duration->xml-node d)
78 #:value (number->string (ash 1 (ly:duration-log d)))))
80 (define (duration->xml-node d)
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))))))
89 (define (musicxml-pitch->xml-node p)
96 #:value (list-ref '("C" "D" "E" "F" "G" "A" "B")
97 (ly:pitch-notename p)))
100 #:value (number->string (ly:pitch-octave p))))))
102 (define (pitch->xml-node p)
105 #:attributes `((octave . ,(ly:pitch-octave p))
106 (notename . ,(ly:pitch-notename p))
107 (alteration . ,(ly:pitch-alteration p)))))
109 (define (music->xml-node music)
110 (let* ((name (ly:get-mus-property music 'name))
111 (e (ly:get-mus-property music 'element))
112 (es (ly:get-mus-property music 'elements))
113 (mprops (ly:get-mutable-properties music))
114 (d (ly:get-mus-property music 'duration))
115 (p (ly:get-mus-property music 'pitch))
116 (ignore-props '(origin elements duration pitch element)))
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)) '())
131 "<?xml version=\"1.0\"?>
140 ;; as computed from input/trip.ly, by
141 ;; http://www.pault.com/pault/dtdgenerator/
143 ;; must recompute with larger, more serious piece, and probably
144 ;; manually add stuff
145 (define preliminary-dtd
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 >
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 >
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 >")
190 (error "assertion failed")))
192 (define (re-sub re to string)
193 (regexp-substitute/global #f re string 'pre to 'post))
195 (define (re-sub-alist string alist)
198 (re-sub (caar alist) (cdar alist)
199 (re-sub-alist string (cdr alist)))))
201 (define xml-entities-alist
208 (define (open-tag tag attrs exceptions)
209 (define (candidate? x)
210 (not (memq (car x) exceptions)))
212 (define (dump-attr sym-val)
223 (let ((s (call-with-output-string (lambda (port) (display val port)))))
224 (re-sub-alist s xml-entities-alist))
229 "<" (symbol->string tag)
230 (apply string-append (map dump-attr (filter candidate? attrs)))
233 (define (close-tag name)
234 (string-append "</" (symbol->string name) ">"))
236 (define-public (music-to-xml music port)
237 "Dump XML-ish stuff to PORT."
239 ;; dtd contains # -- This confuses tex during make web.
241 ;; (display (dtd-header) port)
243 (display (open-tag 'music '((type . score)) '()) port)
244 (display (xml-node->string (music->xml-node music)) port)
245 (display (close-tag 'music) port))
247 (define-public (music-to-musicxml music port)
248 "Dump MusicXML-ish stuff to PORT."
250 ;; dtd contains # -- This confuses tex during make web.
252 ;; (display (dtd-header) port)
254 (define pitch->xml-node musicxml-pitch->xml-node)
255 (define duration->xml-node musicxml-duration->xml-node)
257 (display (open-tag 'music '((type . score)) '()) port)
258 (display (musicxml-node->string (music->xml-node music)) port)
259 (display (close-tag 'music) port))