Fix InstrumentSwitch grob definition.
[lilypond.git] / buildscripts / texi-langutils.py
blob720b520a2e8aa8e33c647c6909d86e0723cec170
1 #!@PYTHON@
2 # texi-langutils.py
4 # WARNING: this script can't find files included in a different directory
6 import sys
7 import re
8 import getopt
9 import os
11 import langdefs
13 def read_pipe (command):
14 print command
15 pipe = os.popen (command)
16 output = pipe.read ()
17 if pipe.close ():
18 print "pipe failed: %(command)s" % locals ()
19 return output
22 optlist, texi_files = getopt.getopt(sys.argv[1:],'no:d:b:i:l:',['skeleton', 'gettext'])
23 process_includes = not ('-n', '') in optlist # -n don't process @include's in texinfo files
25 make_gettext = ('--gettext', '') in optlist # --gettext generate a node list from a Texinfo source
26 make_skeleton = ('--skeleton', '') in optlist # --skeleton extract the node tree from a Texinfo source
28 output_file = 'doc.pot'
30 # @untranslated should be defined as a macro in Texinfo source
31 node_blurb = '''@untranslated
32 '''
33 doclang = ''
34 head_committish = read_pipe ('git-rev-parse HEAD')
35 intro_blurb = '''@c -*- coding: utf-8; mode: texinfo%(doclang)s -*-
36 @c This file is part of %(topfile)s
37 @ignore
38 Translation of GIT committish: %(head_committish)s
39 When revising a translation, copy the HEAD committish of the
40 version that you are working on. See TRANSLATION for details.
41 @end ignore
42 '''
44 end_blurb = """
45 @c -- SKELETON FILE --
46 """
48 for x in optlist:
49 if x[0] == '-o': # -o NAME set PO output file name to NAME
50 output_file = x[1]
51 elif x[0] == '-d': # -d DIR set working directory to DIR
52 os.chdir (x[1])
53 elif x[0] == '-b': # -b BLURB set blurb written at each node to BLURB
54 node_blurb = x[1]
55 elif x[0] == '-i': # -i BLURB set blurb written at beginning of each file to BLURB
56 intro_blurb = x[1]
57 elif x[0] == '-l': # -l ISOLANG set documentlanguage to ISOLANG
58 doclang = '; documentlanguage: ' + x[1]
60 texinfo_with_menus_re = re.compile (r"^(\*) +([^:\n]+)::.*?$|^@(include|menu|end menu|node|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading) *(.*?)$|@(rglos){(.+?)}", re.M)
62 texinfo_re = re.compile (r"^@(include|node|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading) *(.+?)$|@(rglos){(.+?)}", re.M)
64 ly_string_re = re.compile (r'^([a-zA-Z]+)[\t ]*=|%+[\t ]*(.*)$|\\(?:new|context)\s+(?:[a-zA-Z]*?(?:Staff(?:Group)?|Voice|FiguredBass|FretBoards|Names|Devnull))\s+=\s+"?([a-zA-Z]+)"?\s+')
65 lsr_verbatim_ly_re = re.compile (r'% begin verbatim$')
66 texinfo_verbatim_ly_re = re.compile (r'^@lilypond\[.*?verbatim')
68 def process_texi (texifilename, i_blurb, n_blurb, write_skeleton, topfile, output_file=None, scan_ly=False):
69 try:
70 f = open (texifilename, 'r')
71 texifile = f.read ()
72 f.close ()
73 printedfilename = texifilename.replace ('../','')
74 includes = []
76 # process ly var names and comments
77 if output_file and (scan_ly or texifilename.endswith ('.ly')):
78 lines = texifile.splitlines ()
79 i = 0
80 in_verb_ly_block = False
81 if texifilename.endswith ('.ly'):
82 verbatim_ly_re = lsr_verbatim_ly_re
83 else:
84 verbatim_ly_re = texinfo_verbatim_ly_re
85 for i in range (len (lines)):
86 if verbatim_ly_re.search (lines[i]):
87 in_verb_ly_block = True
88 elif lines[i].startswith ('@end lilypond'):
89 in_verb_ly_block = False
90 elif in_verb_ly_block:
91 for (var, comment, context_id) in ly_string_re.findall (lines[i]):
92 if var:
93 output_file.write ('# ' + printedfilename + ':' + \
94 str (i + 1) + ' (variable)\n_(r"' + var + '")\n')
95 elif comment:
96 output_file.write ('# ' + printedfilename + ':' + \
97 str (i + 1) + ' (comment)\n_(r"' + \
98 comment.replace ('"', '\\"') + '")\n')
99 elif context_id:
100 output_file.write ('# ' + printedfilename + ':' + \
101 str (i + 1) + ' (context id)\n_(r"' + \
102 context_id + '")\n')
104 # process Texinfo node names and section titles
105 if write_skeleton:
106 g = open (os.path.basename (texifilename), 'w')
107 subst = globals ()
108 subst.update (locals ())
109 g.write (i_blurb % subst)
110 tutu = texinfo_with_menus_re.findall (texifile)
111 node_trigger = False
112 for item in tutu:
113 if item[0] == '*':
114 g.write ('* ' + item[1] + '::\n')
115 elif output_file and item[4] == 'rglos':
116 output_file.write ('_(r"' + item[5] + '") # @rglos in ' + printedfilename + '\n')
117 elif item[2] == 'menu':
118 g.write ('@menu\n')
119 elif item[2] == 'end menu':
120 g.write ('@end menu\n\n')
121 else:
122 g.write ('@' + item[2] + ' ' + item[3] + '\n')
123 if node_trigger:
124 g.write (n_blurb)
125 node_trigger = False
126 elif item[2] == 'include':
127 includes.append (item[3])
128 else:
129 if output_file:
130 output_file.write ('# @' + item[2] + ' in ' + \
131 printedfilename + '\n_(r"' + item[3].strip () + '")\n')
132 if item[2] == 'node':
133 node_trigger = True
134 g.write (end_blurb)
135 g.close ()
137 elif output_file:
138 toto = texinfo_re.findall (texifile)
139 for item in toto:
140 if item[0] == 'include':
141 includes.append(item[1])
142 elif item[2] == 'rglos':
143 output_file.write ('# @rglos in ' + printedfilename + '\n_(r"' + item[3] + '")\n')
144 else:
145 output_file.write ('# @' + item[0] + ' in ' + printedfilename + '\n_(r"' + item[1].strip () + '")\n')
147 if process_includes:
148 dir = os.path.dirname (texifilename)
149 for item in includes:
150 process_texi (os.path.join (dir, item.strip ()), i_blurb, n_blurb, write_skeleton, topfile, output_file, scan_ly)
151 except IOError, (errno, strerror):
152 sys.stderr.write ("I/O error(%s): %s: %s\n" % (errno, texifilename, strerror))
155 if intro_blurb != '':
156 intro_blurb += '\n\n'
157 if node_blurb != '':
158 node_blurb = '\n' + node_blurb + '\n\n'
159 if make_gettext:
160 node_list_filename = 'node_list'
161 node_list = open (node_list_filename, 'w')
162 node_list.write ('# -*- coding: utf-8 -*-\n')
163 for texi_file in texi_files:
164 # Urgly: scan ly comments and variable names only in English doco
165 is_english_doc = 'Documentation/user' in texi_file
166 process_texi (texi_file, intro_blurb, node_blurb, make_skeleton,
167 os.path.basename (texi_file), node_list,
168 scan_ly=is_english_doc)
169 for word in ('Up:', 'Next:', 'Previous:', 'Appendix ', 'Footnotes', 'Table of Contents'):
170 node_list.write ('_(r"' + word + '")\n')
171 node_list.close ()
172 os.system ('xgettext -c -L Python --no-location -o ' + output_file + ' ' + node_list_filename)
173 else:
174 for texi_file in texi_files:
175 process_texi (texi_file, intro_blurb, node_blurb, make_skeleton,
176 os.path.basename (texi_file))