Corrections to grob property descriptions.
[lilypond.git] / buildscripts / texi-langutils.py
blobcece2be39b62357c40cc2b2495658475e1e76487
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'
29 node_blurb = '''@ifhtml
30 UNTRANSLATED NODE: IGNORE ME
31 @end ifhtml
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 ]*(.*)$')
65 verbatim_ly_re = re.compile (r'@lilypond\[.*?verbatim')
67 def process_texi (texifilename, i_blurb, n_blurb, write_skeleton, topfile, output_file=None, scan_ly=False):
68 try:
69 f = open (texifilename, 'r')
70 texifile = f.read ()
71 f.close ()
72 printedfilename = texifilename.replace ('../','')
73 includes = []
75 # process ly var names and comments
76 if output_file and scan_ly:
77 lines = texifile.splitlines ()
78 i = 0
79 in_verb_ly_block = False
80 for i in range (len (lines)):
81 if verbatim_ly_re.match (lines[i]):
82 in_verb_ly_block = True
83 elif lines[i].startswith ('@end lilypond'):
84 in_verb_ly_block = False
85 elif in_verb_ly_block:
86 for (var, comment) in ly_string_re.findall (lines[i]):
87 if var:
88 output_file.write ('# ' + printedfilename + ':' + \
89 str (i + 1) + ' (variable)\n_(r"' + var + '")\n')
90 elif comment:
91 output_file.write ('# ' + printedfilename + ':' + \
92 str (i + 1) + ' (comment)\n_(r"' + comment + '")\n')
94 # process Texinfo node names and section titles
95 if write_skeleton:
96 g = open (os.path.basename (texifilename), 'w')
97 subst = globals ()
98 subst.update (locals ())
99 g.write (i_blurb % subst)
100 tutu = texinfo_with_menus_re.findall (texifile)
101 node_trigger = False
102 for item in tutu:
103 if item[0] == '*':
104 g.write ('* ' + item[1] + '::\n')
105 elif output_file and item[4] == 'rglos':
106 output_file.write ('_(r"' + item[5] + '") # @rglos in ' + printedfilename + '\n')
107 else:
108 g.write ('@' + item[2] + ' ' + item[3] + '\n')
109 if node_trigger:
110 g.write (n_blurb)
111 node_trigger = False
112 if not item[2] in ('include', 'menu', 'end menu'):
113 if output_file:
114 output_file.write ('# @' + item[2] + ' in ' + \
115 printedfilename + '\n_(r"' + item[3].strip () + '")\n')
116 if item[2] == 'node':
117 node_trigger = True
118 elif item[2] == 'include':
119 includes.append (item[3])
120 g.write (end_blurb)
121 g.close ()
123 elif output_file:
124 toto = texinfo_re.findall (texifile)
125 for item in toto:
126 if item[0] == 'include':
127 includes.append(item[1])
128 elif item[2] == 'rglos':
129 output_file.write ('# @rglos in ' + printedfilename + '\n_(r"' + item[3] + '")\n')
130 else:
131 output_file.write ('# @' + item[0] + ' in ' + printedfilename + '\n_(r"' + item[1].strip () + '")\n')
133 if process_includes:
134 dir = os.path.dirname (texifilename)
135 for item in includes:
136 process_texi (os.path.join (dir, item.strip ()), i_blurb, n_blurb, write_skeleton, topfile, output_file, scan_ly)
137 except IOError, (errno, strerror):
138 sys.stderr.write ("I/O error(%s): %s: %s" % (errno, texifilename, strerror))
141 if intro_blurb != '':
142 intro_blurb += '\n\n'
143 if node_blurb != '':
144 node_blurb = '\n' + node_blurb + '\n\n'
145 if make_gettext:
146 node_list_filename = 'node_list'
147 node_list = open (node_list_filename, 'w')
148 node_list.write ('# -*- coding: utf-8 -*-\n')
149 for texi_file in texi_files:
150 # Urgly: scan ly comments and variable names only in English doco
151 is_english_doc = 'Documentation/user' in texi_file
152 process_texi (texi_file, intro_blurb, node_blurb, make_skeleton,
153 os.path.basename (texi_file), node_list,
154 scan_ly=is_english_doc)
155 for word in ('Up:', 'Next:', 'Previous:', 'Appendix ', 'Footnotes', 'Table of Contents'):
156 node_list.write ('_(r"' + word + '")\n')
157 node_list.close ()
158 os.system ('xgettext -c -L Python --no-location -o ' + output_file + ' ' + node_list_filename)
159 else:
160 for texi_file in texi_files:
161 process_texi (texi_file, intro_blurb, node_blurb, make_skeleton,
162 os.path.basename (texi_file))