]> git.donarmstrong.com Git - lilypond.git/blob - scm/document-translation.scm
5e867bb4c24bfc84193a90a41ca300522043d689
[lilypond.git] / scm / document-translation.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2000--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;                 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond 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
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19 (define (engraver-makes-grob? name-symbol grav)
20   (memq name-symbol (assoc 'grobs-created (ly:translator-description grav))))
21
22 (define (engraver-accepts-music-type? name-symbol grav)
23   (memq name-symbol (assoc 'events-accepted (ly:translator-description grav))))
24
25 (define (engraver-accepts-music-types? types grav)
26   (if (null? types)
27       #f
28       (or
29        (engraver-accepts-music-type? (car types) grav)
30        (engraver-accepts-music-types? (cdr types) grav))))
31
32 (define (engraver-doc-string engraver in-which-contexts)
33   (let* ((propsr (assoc-get 'properties-read (ly:translator-description engraver)))
34          (propsw (assoc-get 'properties-written (ly:translator-description engraver)))
35          (accepted  (assoc-get 'events-accepted (ly:translator-description engraver)))
36          (name-sym  (ly:translator-name engraver))
37          (name-str (symbol->string name-sym))
38          (desc (assoc-get 'description (ly:translator-description engraver)))
39          (grobs (engraver-grobs engraver)))
40
41     (string-append
42      desc
43      "\n\n"
44      (if (pair? accepted)
45          (string-append
46           "Music types accepted:\n\n"
47           (human-listify
48            (map ref-ify (sort (map symbol->string accepted) ly:string-ci<?))))
49          "")
50      "\n\n"
51      (if (pair? propsr)
52          (string-append
53           "Properties (read)"
54           (description-list->texi
55            (map (lambda (x) (property->texi 'translation x '()))
56                 (sort propsr ly:symbol-ci<?))
57            #t))
58          "")
59
60      (if (null? propsw)
61          ""
62          (string-append
63           "Properties (write)"
64           (description-list->texi
65            (map (lambda (x) (property->texi 'translation x '()))
66                 (sort propsw ly:symbol-ci<?))
67            #t)))
68      (if  (null? grobs)
69           ""
70           (string-append
71            "\n\nThis engraver creates the following layout object(s):\n\n"
72            (human-listify (map ref-ify (uniq-list (sort grobs ly:string-ci<?))))
73            "."))
74
75      "\n\n"
76
77      (if in-which-contexts
78          (let* ((layout-alist (ly:output-description $defaultlayout))
79                 (context-description-alist (map cdr layout-alist))
80                 (contexts
81                  (append-map
82                   (lambda (x)
83                     (let* ((context (assoc-get 'context-name x))
84                            (group (assq-ref x 'group-type))
85                            (consists (append
86                                       (if group
87                                           (list group)
88                                           '())
89                                       (assoc-get 'consists x))))
90                       (if (member name-sym consists)
91                           (list context)
92                           '())))
93                   context-description-alist))
94                 (context-list (human-listify (map ref-ify
95                                                   (sort
96                                                    (map symbol->string contexts)
97                                                    ly:string-ci<?)))))
98            (string-append
99             "@code{" name-str "} "
100             (if (equal? context-list "none")
101                 "is not part of any context"
102                 (string-append
103                  "is part of the following context(s): "
104                  context-list))
105             "."))
106          ""))))
107
108 ;; First level Engraver description
109 (define (engraver-doc grav)
110   (make <texi-node>
111     #:name (symbol->string (ly:translator-name grav))
112     #:text (engraver-doc-string grav #t)))
113
114 ;; Second level, part of Context description
115 (define name->engraver-table (make-hash-table 61))
116 (for-each
117  (lambda (x)
118    (hash-set! name->engraver-table (ly:translator-name x) x))
119  (ly:get-all-translators))
120
121 (define (find-engraver-by-name name)
122   "NAME is a symbol."
123   (hash-ref name->engraver-table name #f))
124
125 (define (document-engraver-by-name name)
126   "NAME is a symbol."
127
128   (let* ((eg (find-engraver-by-name name)))
129
130     (cons (string-append "@code{" (ref-ify (symbol->string name)) "}")
131           (engraver-doc-string eg #f))))
132
133 (define (document-property-operation op)
134   (let ((tag (car op))
135         (context-sym (cadr op))
136         (args (cddr op))
137         )
138
139     (cond
140      ((equal?  tag 'push)
141       (let*
142           ((value (car args))
143            (path (cdr args)))
144
145         (string-append
146          "@item Set "
147          (format #f "grob-property @code{~a} "
148                  (string-join (map symbol->string path) " "))
149          (format #f "in @ref{~a} to ~a."
150                  context-sym (scm->texi value))
151          "\n")))
152      ((equal? (object-property context-sym 'is-grob?) #t) "")
153      ((equal? tag 'assign)
154       (format #f "@item Set translator property @code{~a} to ~a.\n"
155               context-sym
156               (scm->texi (car args))))
157      )))
158
159
160 (define (context-doc context-desc)
161   (let* ((name-sym (assoc-get 'context-name context-desc))
162          (name (symbol->string name-sym))
163          (aliases (map symbol->string (assoc-get 'aliases context-desc)))
164          (desc (assoc-get 'description context-desc "(not documented"))
165          (accepts (assoc-get 'accepts context-desc))
166          (consists (assoc-get 'consists context-desc))
167          (props (assoc-get 'property-ops context-desc))
168          (grobs  (context-grobs context-desc))
169          (grob-refs (map ref-ify (sort grobs ly:string-ci<?))))
170
171     (make <texi-node>
172       #:name name
173       #:text
174       (string-append
175        desc
176        (if (pair? aliases)
177            (string-append
178             "\n\nThis context also accepts commands for the following context(s):\n\n"
179             (human-listify (sort aliases ly:string-ci<?))
180             ".")
181            "")
182
183        "\n\nThis context creates the following layout object(s):\n\n"
184        (human-listify (uniq-list grob-refs))
185        "."
186
187        (if (and (pair? props) (not (null? props)))
188            (let ((str (string-concatenate
189                        (sort (map document-property-operation props)
190                              ly:string-ci<?))))
191              (if (string-null? str)
192                  ""
193                  (string-append
194                   "\n\nThis context sets the following properties:\n\n"
195                   "@itemize @bullet\n"
196                   str
197                   "@end itemize\n")))
198            "")
199
200        (if (null? accepts)
201            "\n\nThis context is a `bottom' context; it cannot contain other contexts."
202            (string-append
203             "\n\nContext "
204             name
205             " can contain\n"
206             (human-listify (map ref-ify (sort (map symbol->string accepts)
207                                               ly:string-ci<?)))
208             "."))
209
210        (if (null? consists)
211            ""
212            (string-append
213             "\n\nThis context is built from the following engraver(s):"
214             (description-list->texi
215              (map document-engraver-by-name (sort consists ly:symbol-ci<?))
216              #t)))))))
217
218 (define (engraver-grobs grav)
219   (let* ((eg (if (symbol? grav)
220                  (find-engraver-by-name grav)
221                  grav)))
222     (if (eq? eg #f)
223         '()
224         (map symbol->string (assoc-get 'grobs-created (ly:translator-description eg))))))
225
226 (define (context-grobs context-desc)
227   (let* ((group (assq-ref context-desc 'group-type))
228          (consists (append
229                     (if group
230                         (list group)
231                         '())
232                     (assoc-get 'consists context-desc)))
233          (grobs (append-map engraver-grobs consists)))
234     grobs))
235
236 (define (all-contexts-doc)
237   (let* ((layout-alist
238           (sort (ly:output-description $defaultlayout)
239                 (lambda (x y) (ly:symbol-ci<? (car x) (car y)))))
240          (names (sort (map symbol->string (map car layout-alist)) ly:string-ci<?))
241          (contexts (map cdr layout-alist)))
242
243     (make <texi-node>
244       #:name "Contexts"
245       #:desc "Complete descriptions of all contexts."
246       #:children
247       (map context-doc contexts))))
248
249 (define all-engravers-list  (ly:get-all-translators))
250 (set! all-engravers-list
251       (sort all-engravers-list
252             (lambda (a b) (ly:string-ci<? (symbol->string (ly:translator-name a))
253                                           (symbol->string (ly:translator-name b))))))
254
255 (define (all-engravers-doc)
256   (make <texi-node>
257     #:name "Engravers and Performers"
258     #:desc "All separate engravers and performers."
259     #:text "See @ruser{Modifying context plug-ins}."
260     #:children
261     (map engraver-doc all-engravers-list)))
262
263 (define (translation-properties-doc-string lst)
264   (let* ((ps (sort (map symbol->string lst) ly:string-ci<?))
265          (sortedsyms (map string->symbol ps))
266          (propdescs
267           (map
268            (lambda (x) (property->texi 'translation  x '()))
269            sortedsyms))
270          (texi (description-list->texi propdescs #f)))
271     texi))
272
273 (define (translation-doc-node)
274   (make <texi-node>
275     #:name "Translation"
276     #:desc "From music to layout."
277     #:children
278     (list
279      (all-contexts-doc)
280      (all-engravers-doc)
281      (make <texi-node>
282        #:name "Tunable context properties"
283        #:desc "All tunable context properties."
284        #:text (translation-properties-doc-string
285                all-user-translation-properties))
286
287      (make <texi-node>
288        #:name "Internal context properties"
289        #:desc "All internal context properties."
290        #:text (translation-properties-doc-string
291                all-internal-translation-properties)))))