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