]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/lys-to-tely.py
Lilypond-book: Add a regression test system
[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  -a, --author=AUTHOR            set tely author AUTHOR
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 """
36
37 def help (text):
38     sys.stdout.write ( text)
39     sys.exit (0)
40
41 (options, files) = getopt.getopt (sys.argv[1:], 'f:hn:t:',
42                      ['fragment-options=', 'help', 'name=', 'title=', 'author=', 'template='])
43
44 name = "ly-doc"
45 title = "Ly Doc"
46 author = "Han-Wen Nienhuys and Jan Nieuwenhuizen"
47 template = '''\input texinfo
48 @setfilename %%(name)s.info
49 @settitle %%(title)s
50
51 @documentencoding utf-8
52 @iftex
53 @afourpaper
54 @end iftex
55
56 @finalout @c we do not want black boxes.
57
58 @c fool ls-latex
59 @ignore
60 @author %%(author)s
61 @title %%(title)s
62 @end ignore
63
64 @node Top, , , (dir)
65 @top %%(title)s
66
67 %s
68
69 @bye
70 ''' % include_snippets
71
72 for opt in options:
73     o = opt[0]
74     a = opt[1]
75     if o == '-h' or o == '--help':
76         # We can't use vars () inside a function, as that only contains all
77         # local variables and none of the global variables! Thus we have to
78         # generate the help text here and pass it to the function...
79         help (help_text % vars ())
80     elif o == '-n' or o == '--name':
81         name = a
82     elif o == '-t' or o == '--title':
83         title = a
84     elif o == '-a' or o == '--author':
85         author = a
86     elif o == '-f' or o == '--fragment-options':
87         fragment_options = a
88     elif o == '--template':
89         template = open (a, 'r').read ()
90     else:
91         raise Exception ('unknown option: ' + o)
92
93 texi_file_re = re.compile ('.*\.i?te(ly|xi)$')
94 html_file_re = re.compile ('.*\.i?htm(l)?$')
95
96 def name2line (n):
97     if texi_file_re.match (n):
98         # We have a texi include file, simply include it:
99         s = r"@include %s" % os.path.basename (n)
100     if html_file_re.match (n):
101         s = r"""
102 @ifhtml
103 @html
104 <a href="%s">%s</a>
105 <br/>
106 @end html
107 @end ifhtml
108 """ % (os.path.basename (n), os.path.basename (n))
109         return s
110     else:
111         # Assume it's a lilypond file -> create image etc.
112         s = r"""
113 @ifhtml
114 @html
115 <a name="%s"></a>
116 @end html
117 @end ifhtml
118
119 @lilypondfile[%s]{%s}
120 """ % (os.path.basename (n), fragment_options, n)
121     return s
122
123 if files:
124     dir = os.path.dirname (name) or "."
125 # don't strip .tely extension, Documentation/snippets uses .itely
126     name = os.path.basename (name)
127     template = template % vars ()
128
129     s = "\n".join (map (name2line, files))
130     s = template.replace (include_snippets, s, 1)
131     f = "%s/%s" % (dir, name)
132     sys.stderr.write ("%s: writing %s..." % (program_name, f))
133     h = open (f, "w")
134     h.write (s)
135     h.close ()
136     sys.stderr.write ('\n')
137 else:
138     # not Unix philosophy, but hey, at least we notice when
139     # we don't distribute any .ly files.
140     sys.stderr.write ("No files specified. Doing nothing")