]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/lys-to-tely.py
Merge branch 'master' of git://git.sv.gnu.org/lilypond
[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 shorten_file_name (n):
84     # ugh, regtests file names appear as full paths in GUB builds
85     # we try to do something here
86     b = os.path.basename (n)
87     if os.path.exists (b):
88         return b
89     return n
90
91 def name2line (n):
92     # UGR
93     s = r"""
94 @ifhtml
95 @html
96 <A NAME="%s"></A>
97 @end html
98 @end ifhtml
99
100 @lilypondfile[%s]{%s}""" % (n, fragment_options, n)
101     return s
102
103 if files:
104     dir = os.path.dirname (name) or "."
105 # don't strip .tely extension, input/lsr uses .itely
106     name = os.path.basename (name)
107     template = template % vars ()
108
109     files = map (shorten_file_name, files)
110     files.sort ()
111     s = "\n".join (map (name2line, files))
112     s = template.replace (include_snippets, s, 1)
113     f = "%s/%s" % (dir, name)
114     sys.stderr.write ("%s: writing %s..." % (program_name, f))
115     h = open (f, "w")
116     h.write (s)
117     h.close ()
118     sys.stderr.write ('\n')
119 else:
120     # not Unix philosophy, but hey, at least we notice when
121     # we don't distribute any .ly files.
122     sys.stderr.write ("No files specified. Doing nothing")