]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/lys-to-tely.py
7a8684f1949b9cf50849ea65dcc2436d030319f7
[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 html_file_re = re.compile ('.*\.i?html?$')
106 info_file_re = re.compile ('.*\.info$')
107 pdf_file_re = re.compile ('.*\.i?pdf$')
108 tex_file_re = re.compile ('.*\.i?(la)?tex$')
109 texi_file_re = re.compile ('.*\.i?te(ly|xi|xinfo)$')
110 xml_file_re = re.compile ('.*\.i?(xm|mx)l$')
111
112 def name2line (n):
113     if texi_file_re.match (n):
114         # We have a texi include file, simply include it:
115         s = r"@include %s" % os.path.basename (n)
116     elif (html_file_re.match (n) or info_file_re.match (n)
117           or pdf_file_re.match (n) or tex_file_re.match (n)):
118         s = r"""
119 @ifhtml
120 @html
121 <a href="%s">%s</a>
122 <br/>
123 @end html
124 @end ifhtml
125 """ % (os.path.basename (n), os.path.basename (n))
126
127     elif (xml_file_re.match (n)):
128         # Assume it's a MusicXML file -> convert, create image etc.
129         s = r"""
130 @ifhtml
131 @html
132 <a name="%s"></a>
133 @end html
134 @end ifhtml
135
136 @musicxmlfile[%s]{%s}
137 """ % (os.path.basename (n), fragment_options, n)
138
139     else:
140         # Assume it's a lilypond file -> create image etc.
141         s = r"""
142 @ifhtml
143 @html
144 <a name="%s"></a>
145 @end html
146 @end ifhtml
147
148 @lilypondfile[%s]{%s}
149 """ % (os.path.basename (n), fragment_options, n)
150     return s
151
152 if glob_input:
153     files = glob.glob(glob_input)
154 elif input_filename:
155     files = open(input_filename).read().split()
156
157 if files:
158     dir = os.path.dirname (name) or "."
159 # don't strip .tely extension, Documentation/snippets uses .itely
160     name = os.path.basename (name)
161     template = template % vars ()
162
163     s = "\n".join (map (name2line, files))
164     s = template.replace (include_snippets, s, 1)
165     f = "%s/%s" % (dir, name)
166     h = open (f, "w")
167     h.write (s)
168     h.close ()
169 else:
170     # not Unix philosophy, but hey, at least we notice when
171     # we don't distribute any .ly files.
172     sys.stderr.write ("No files specified. Doing nothing")