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