LSR: Update.
[lilypond.git] / buildscripts / lys-to-tely.py
blobe74c7145034e26f715bb73dfb04da015aef57c39
1 #!@PYTHON@
4 '''
5 TODO:
7 * Add @nodes, split at sections?
9 '''
12 import sys
13 import os
14 import getopt
16 program_name = 'lys-to-tely'
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.
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 """
35 def help (text):
36 sys.stdout.write ( text)
37 sys.exit (0)
39 (options, files) = getopt.getopt (sys.argv[1:], 'f:hn:t:',
40 ['fragment-options=', 'help', 'name=', 'title=', 'template='])
42 name = "ly-doc"
43 title = "Ly Doc"
44 template = '''\input texinfo
45 @setfilename %%(name)s.info
46 @settitle %%(title)s
48 @documentencoding utf-8
49 @iftex
50 @afourpaper
51 @end iftex
53 @finalout @c we do not want black boxes.
55 @c fool ls-latex
56 @ignore
57 @author Han-Wen Nienhuys and Jan Nieuwenhuizen
58 @title %%(title)s
59 @end ignore
61 @node Top, , , (dir)
62 @top %%(title)s
66 @bye
67 ''' % include_snippets
69 for opt in options:
70 o = opt[0]
71 a = opt[1]
72 if o == '-h' or o == '--help':
73 # We can't use vars () inside a function, as that only contains all
74 # local variables and none of the global variables! Thus we have to
75 # generate the help text here and pass it to the function...
76 help (help_text % vars ())
77 elif o == '-n' or o == '--name':
78 name = a
79 elif o == '-t' or o == '--title':
80 title = a
81 elif o == '-f' or o == '--fragment-options':
82 fragment_options = a
83 elif o == '--template':
84 template = open (a, 'r').read ()
85 else:
86 raise Exception ('unknown option: ' + o)
88 def name2line (n):
89 s = r"""
90 @ifhtml
91 @html
92 <a name="%s"></a>
93 @end html
94 @end ifhtml
96 @lilypondfile[%s]{%s}
97 """ % (os.path.basename (n), fragment_options, n)
98 return s
100 if files:
101 dir = os.path.dirname (name) or "."
102 # don't strip .tely extension, input/lsr uses .itely
103 name = os.path.basename (name)
104 template = template % vars ()
106 files.sort ()
107 s = "\n".join (map (name2line, files))
108 s = template.replace (include_snippets, s, 1)
109 f = "%s/%s" % (dir, name)
110 sys.stderr.write ("%s: writing %s..." % (program_name, f))
111 h = open (f, "w")
112 h.write (s)
113 h.close ()
114 sys.stderr.write ('\n')
115 else:
116 # not Unix philosophy, but hey, at least we notice when
117 # we don't distribute any .ly files.
118 sys.stderr.write ("No files specified. Doing nothing")