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