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