]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/lys-to-tely.py
Clean up buildscripts
[lilypond.git] / scripts / build / lys-to-tely.py
1 #!@PYTHON@
2
3
4 '''
5 TODO:
6
7  * Add @nodes, split at sections?
8
9 '''
10
11
12 import sys
13 import os
14 import getopt
15 import re
16
17 program_name = 'lys-to-tely'
18
19 include_snippets = '@lysnippets'
20 fragment_options = 'printfilename,texidoc'
21 help_text = r"""Usage: %(program_name)s [OPTIONS]... LY-FILE...
22 Construct tely doc from LY-FILEs.
23
24 Options:
25  -h, --help                     print this help
26  -f, --fragment-options=OPTIONS use OPTIONS as lilypond-book fragment
27    options
28  -o, --output=NAME              write tely doc to NAME
29  -t, --title=TITLE              set tely doc title TITLE
30      --template=TEMPLATE        use TEMPLATE as Texinfo template file,
31    instead of standard template; TEMPLATE should contain a command
32    '%(include_snippets)s' to tell where to insert LY-FILEs.  When this
33    option is used, NAME and TITLE are ignored.
34 """
35
36 def help (text):
37     sys.stdout.write ( text)
38     sys.exit (0)
39
40 (options, files) = getopt.getopt (sys.argv[1:], 'f:hn:t:',
41                      ['fragment-options=', 'help', 'name=', 'title=', 'template='])
42
43 name = "ly-doc"
44 title = "Ly Doc"
45 template = '''\input texinfo
46 @setfilename %%(name)s.info
47 @settitle %%(title)s
48
49 @documentencoding utf-8
50 @iftex
51 @afourpaper
52 @end iftex
53
54 @finalout @c we do not want black boxes.
55
56 @c fool ls-latex
57 @ignore
58 @author Han-Wen Nienhuys and Jan Nieuwenhuizen
59 @title %%(title)s
60 @end ignore
61
62 @node Top, , , (dir)
63 @top %%(title)s
64
65 %s
66
67 @bye
68 ''' % include_snippets
69
70 for opt in options:
71     o = opt[0]
72     a = opt[1]
73     if o == '-h' or o == '--help':
74         # We can't use vars () inside a function, as that only contains all 
75         # local variables and none of the global variables! Thus we have to 
76         # generate the help text here and pass it to the function...
77         help (help_text % vars ())
78     elif o == '-n' or o == '--name':
79         name = a
80     elif o == '-t' or o == '--title':
81         title = a
82     elif o == '-f' or o == '--fragment-options':
83         fragment_options = a
84     elif o == '--template':
85         template = open (a, 'r').read ()
86     else:
87         raise Exception ('unknown option: ' + o)
88
89 texi_file_re = re.compile ('.*\.i?te(ly|xi)$')
90
91 def name2line (n):
92     if texi_file_re.match (n):
93         # We have a texi include file, simply include it:
94         s = r"@include %s" % os.path.basename (n)
95     else:
96         # Assume it's a lilypond file -> create image etc.
97         s = r"""
98 @ifhtml
99 @html
100 <a name="%s"></a>
101 @end html
102 @end ifhtml
103
104 @lilypondfile[%s]{%s}
105 """ % (os.path.basename (n), fragment_options, n)
106     return s
107
108 if files:
109     dir = os.path.dirname (name) or "."
110 # don't strip .tely extension, input/lsr uses .itely
111     name = os.path.basename (name)
112     template = template % vars ()
113
114     s = "\n".join (map (name2line, files))
115     s = template.replace (include_snippets, s, 1)
116     f = "%s/%s" % (dir, name)
117     sys.stderr.write ("%s: writing %s..." % (program_name, f))
118     h = open (f, "w")
119     h.write (s)
120     h.close ()
121     sys.stderr.write ('\n')
122 else:
123     # not Unix philosophy, but hey, at least we notice when
124     # we don't distribute any .ly files.
125     sys.stderr.write ("No files specified. Doing nothing")