]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/lys-to-tely.py
lys-to-tely.py: add --glob-input=
[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 @setfilename %%(name)s.info
57 @settitle %%(title)s
58
59 @documentencoding utf-8
60 @iftex
61 @afourpaper
62 @end iftex
63
64 @finalout @c we do not want black boxes.
65
66 @c fool ls-latex
67 @ignore
68 @author %%(author)s
69 @title %%(title)s
70 @end ignore
71
72 @node Top, , , (dir)
73 @top %%(title)s
74
75 %s
76
77 @bye
78 ''' % include_snippets
79
80 for opt in options:
81     o = opt[0]
82     a = opt[1]
83     if o == '-h' or o == '--help':
84         # We can't use vars () inside a function, as that only contains all
85         # local variables and none of the global variables! Thus we have to
86         # generate the help text here and pass it to the function...
87         help (help_text % vars ())
88     elif o == '-n' or o == '--name':
89         name = a
90     elif o == '-t' or o == '--title':
91         title = a
92     elif o == '-a' or o == '--author':
93         author = a
94     elif o == '-i' or o == '--input-filenames':
95         input_filename = a
96     elif o == '-p' or o == '--glob-input':
97         glob_input = a
98     elif o == '-f' or o == '--fragment-options':
99         fragment_options = a
100     elif o == '--template':
101         template = open (a, 'r').read ()
102     else:
103         raise Exception ('unknown option: ' + o)
104
105 texi_file_re = re.compile ('.*\.i?te(ly|xi)$')
106 html_file_re = re.compile ('.*\.i?htm(l)?$')
107 xml_file_re = re.compile ('.*\.i?(xm|mx)l$')
108 tex_file_re = re.compile ('.*\.i?(la)?tex$')
109 pdf_file_re = re.compile ('.*\.i?pdf$')
110
111 def name2line (n):
112     if texi_file_re.match (n):
113         # We have a texi include file, simply include it:
114         s = r"@include %s" % os.path.basename (n)
115     elif (html_file_re.match (n) or pdf_file_re.match (n) or
116           tex_file_re.match (n)):
117         s = r"""
118 @ifhtml
119 @html
120 <a href="%s">%s</a>
121 <br/>
122 @end html
123 @end ifhtml
124 """ % (os.path.basename (n), os.path.basename (n))
125
126     elif (xml_file_re.match (n)):
127         # Assume it's a MusicXML file -> convert, create image etc.
128         s = r"""
129 @ifhtml
130 @html
131 <a name="%s"></a>
132 @end html
133 @end ifhtml
134
135 @musicxmlfile[%s]{%s}
136 """ % (os.path.basename (n), fragment_options, n)
137
138     else:
139         # Assume it's a lilypond file -> create image etc.
140         s = r"""
141 @ifhtml
142 @html
143 <a name="%s"></a>
144 @end html
145 @end ifhtml
146
147 @lilypondfile[%s]{%s}
148 """ % (os.path.basename (n), fragment_options, n)
149     return s
150
151 if glob_input:
152     files = glob.glob(glob_input)
153 elif input_filename:
154     files = open(input_filename).read().splitlines()
155
156 if files:
157     dir = os.path.dirname (name) or "."
158 # don't strip .tely extension, Documentation/snippets uses .itely
159     name = os.path.basename (name)
160     template = template % vars ()
161
162     s = "\n".join (map (name2line, files))
163     s = template.replace (include_snippets, s, 1)
164     f = "%s/%s" % (dir, name)
165     sys.stderr.write ("%s: writing %s..." % (program_name, f))
166     h = open (f, "w")
167     h.write (s)
168     h.close ()
169     sys.stderr.write ('\n')
170 else:
171     # not Unix philosophy, but hey, at least we notice when
172     # we don't distribute any .ly files.
173     sys.stderr.write ("No files specified. Doing nothing")