]> git.donarmstrong.com Git - lilypond.git/blob - scm/document-markup.scm
35347ae15083c0fac3c47c416c05857f88d4b37b
[lilypond.git] / scm / document-markup.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 1998--2015 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
20 (define (doc-markup-function-properties func)
21   (let ((properties (hashq-ref markup-functions-properties func))
22         (prop-strings (list)))
23     (for-each (lambda (prop-spec)
24                 (set! prop-strings
25                       (if (list? prop-spec)
26                           ;; either (prop value) or (prop)
27                           (cons (if (null? (cdr prop-spec))
28                                     (format #f "@item @code{~a}\n" (car prop-spec))
29                                     (format #f "@item @code{~a} (~a)\n"
30                                             (car prop-spec)
31                                             (let ((default (cadr prop-spec)))
32                                               (if (and (list? default)
33                                                        (null? default))
34                                                   "'()"
35                                                   default))))
36                                 prop-strings)
37                           ;; a markup command: get its properties
38                           ;; FIXME: avoid cyclical references
39                           (append (doc-markup-function-properties prop-spec)
40                                   prop-strings))))
41               (or properties (list)))
42     prop-strings))
43
44 (define (doc-markup-function func)
45   (let* ((full-doc (procedure-documentation func))
46          (match-args (and full-doc (string-match "^\\([^)]*\\)\n" full-doc)))
47          (arg-names (if match-args
48                         (with-input-from-string (match:string match-args) read)
49                         (circular-list "arg")))
50          (doc-str (if match-args (match:suffix match-args) full-doc))
51          (f-name (symbol->string (procedure-name  func)))
52          (c-name (regexp-substitute/global #f "-markup(-list)?$" f-name  'pre "" 'post))
53          (sig (object-property func 'markup-signature))
54          (sig-type-names (map type-name sig))
55          (signature-str
56           (string-join
57            (map (lambda (x y)
58                   (format #f "@var{~a} (~a)" x y))
59                 arg-names  sig-type-names)
60            " " )))
61
62     (string-append
63      "\n\n@item @code{\\" c-name "} " signature-str
64      "\n@funindex \\" c-name "\n"
65      "\n@cindex \\" c-name "\n"
66      (if (string? doc-str)
67          doc-str
68          "")
69      (let ((prop-strings (doc-markup-function-properties func)))
70        (if (null? prop-strings)
71            "\n"
72            (string-append "\n\n\nUsed properties:\n@itemize\n"
73                           (string-concatenate prop-strings)
74                           "@end itemize\n"))))))
75
76 (define (markup-function<? a b)
77   (ly:string-ci<? (symbol->string (procedure-name a)) (symbol->string (procedure-name b))))
78
79 (define (markup-category-doc-node category)
80   (let* ((category-string (symbol->string category))
81          (category-name (string-capitalize
82                          (regexp-substitute/global
83                           #f "-" category-string 'pre " " 'post)))
84          (markup-functions (hash-fold (lambda (markup-function dummy functions)
85                                         (cons markup-function functions))
86                                       '()
87                                       (hashq-ref markup-functions-by-category
88                                                  category))))
89     (make <texi-node>
90       #:appendix #t
91       #:name category-name
92       #:desc ""
93       #:text (string-append
94               "@table @asis"
95               (string-concatenate
96                (map doc-markup-function
97                     (sort markup-functions markup-function<?)))
98               "\n@end table"))))
99
100 (define (markup-doc-node)
101   (make <texi-node>
102     #:appendix #t
103     #:name "Text markup commands"
104     #:desc ""
105     #:text "The following commands can all be used inside @code{\\markup @{ @}}."
106     #:children (let* (;; when a new category is defined, update `ordered-categories'
107                       (ordered-categories '(font align graphic music instrument-specific-markup accordion-registers other))
108                       (raw-categories (hash-fold (lambda (category functions categories)
109                                                    (cons category categories))
110                                                  (list)
111                                                  markup-functions-by-category))
112                       (categories (append ordered-categories
113                                           (filter (lambda (cat)
114                                                     (not (memq cat ordered-categories)))
115                                                   raw-categories))))
116                  (map markup-category-doc-node categories))))
117
118 (define (markup-list-doc-string)
119   (string-append
120    "@table @asis"
121    (string-concatenate
122     (map doc-markup-function
123          (sort (hash-fold (lambda (markup-list-function dummy functions)
124                             (cons markup-list-function functions))
125                           '()
126                           markup-list-functions)
127                markup-function<?)))
128    "\n@end table"))