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