]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/lys-to-tely.py
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scripts / build / lys-to-tely.py
1 #!@PYTHON@
2
3
4 '''
5 TODO:
6
7  * Add @nodes, split at sections?
8
9  * -o --output   listed in help is not implemented?!
10 '''
11
12
13 import sys
14 import os
15 import getopt
16 import re
17 import glob
18
19 program_name = 'lys-to-tely'
20
21 include_snippets = '@lysnippets'
22 fragment_options = 'printfilename,texidoc'
23 help_text = r"""Usage: %(program_name)s [OPTIONS]... LY-FILE...
24 Construct tely doc from LY-FILEs.
25
26 Options:
27  -h, --help                     print this help
28  -f, --fragment-options=OPTIONS use OPTIONS as lilypond-book fragment
29    options
30  -o, --output=NAME              write tely doc to NAME
31  -i, --input-filenames=NAME     read list of files from a file instead of stdin
32  -g, --glob-input=GLOB          a string which will be passed to glob.glob(GLOB)
33  -t, --title=TITLE              set tely doc title TITLE
34  -a, --author=AUTHOR            set tely author AUTHOR
35      --template=TEMPLATE        use TEMPLATE as Texinfo template file,
36    instead of standard template; TEMPLATE should contain a command
37    '%(include_snippets)s' to tell where to insert LY-FILEs.  When this
38    option is used, NAME and TITLE are ignored.
39 """
40
41 def help (text):
42     sys.stdout.write ( text)
43     sys.exit (0)
44
45 (options, files) = getopt.getopt (sys.argv[1:], 'f:hn:t:',
46                      ['fragment-options=', 'help', 'name=',
47                      'title=', 'author=', 'template=',
48                      'input-filenames=', 'glob-input='])
49
50 name = "ly-doc"
51 title = "Ly Doc"
52 author = "Han-Wen Nienhuys and Jan Nieuwenhuizen"
53 input_filename = ""
54 glob_input = ""
55 template = '''\input texinfo
56
57 @c This file was autogenerated
58 @c     from: %s
59 @c     by:   %s
60
61 @setfilename %%(name)s.info
62 @settitle %%(title)s
63
64 @documentencoding UTF-8
65 @iftex
66 @afourpaper
67 @end iftex
68
69 @finalout @c we do not want black boxes.
70
71 @c fool ls-latex
72 @ignore
73 @author %%(author)s
74 @title %%(title)s
75 @end ignore
76
77 @node Top, , , (dir)
78 @top %%(title)s
79
80 %s
81
82 @bye
83 ''' % (", ".join(files), sys.argv[0], include_snippets)
84
85 for opt in options:
86     o = opt[0]
87     a = opt[1]
88     if o == '-h' or o == '--help':
89         # We can't use vars () inside a function, as that only contains all
90         # local variables and none of the global variables! Thus we have to
91         # generate the help text here and pass it to the function...
92         help (help_text % vars ())
93     elif o == '-n' or o == '--name':
94         name = a
95     elif o == '-t' or o == '--title':
96         title = a
97     elif o == '-a' or o == '--author':
98         author = a
99     elif o == '-i' or o == '--input-filenames':
100         input_filename = a
101     elif o == '-p' or o == '--glob-input':
102         glob_input = a
103     elif o == '-f' or o == '--fragment-options':
104         fragment_options = a
105     elif o == '--template':
106         template = open (a, 'r').read ()
107     else:
108         raise Exception ('unknown option: ' + o)
109
110 html_file_re = re.compile ('.*\.i?html?$')
111 info_file_re = re.compile ('.*\.info$')
112 pdf_file_re = re.compile ('.*\.i?pdf$')
113 tex_file_re = re.compile ('.*\.i?(la)?tex$')
114 texi_file_re = re.compile ('.*\.i?te(ly|xi|xinfo)$')
115 xml_file_re = re.compile ('.*\.i?(xm|mx)l$')
116
117 def name2line (n):
118     if texi_file_re.match (n):
119         # We have a texi include file, simply include it:
120         s = r"@include %s" % os.path.basename (n)
121     elif (html_file_re.match (n) or info_file_re.match (n)
122           or pdf_file_re.match (n) or tex_file_re.match (n)):
123         s = r"""
124 @ifhtml
125 @html
126 <a href="%s">%s</a>
127 <br/>
128 @end html
129 @end ifhtml
130 """ % (os.path.basename (n), os.path.basename (n))
131
132     elif (xml_file_re.match (n)):
133         # Assume it's a MusicXML file -> convert, create image etc.
134         s = r"""
135 @ifhtml
136 @html
137 <a name="%s"></a>
138 @end html
139 @end ifhtml
140
141 @musicxmlfile[%s]{%s}
142 """ % (os.path.basename (n), fragment_options, n)
143
144     else:
145         # Assume it's a lilypond file -> create image etc.
146         s = r"""
147 @ifhtml
148 @html
149 <a name="%s"></a>
150 @end html
151 @end ifhtml
152
153 @lilypondfile[%s]{%s}
154 """ % (os.path.basename (n), fragment_options, n)
155     return s
156
157 if glob_input:
158     files = glob.glob(glob_input)
159 elif input_filename:
160     files = open(input_filename).read().split()
161
162 if files:
163     dir = os.path.dirname (name) or "."
164 # don't strip .tely extension, Documentation/snippets uses .itely
165     name = os.path.basename (name)
166     template = template % vars ()
167
168     s = "\n".join (map (name2line, files))
169     s = template.replace (include_snippets, s, 1)
170     f = "%s/%s" % (dir, name)
171     h = open (f, "w")
172     h.write (s)
173     h.close ()
174 else:
175     # not Unix philosophy, but hey, at least we notice when
176     # we don't distribute any .ly files.
177     sys.stderr.write ("No files specified. Doing nothing. Use -h to display usage.")