]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/texi-langutils.py
Updates to match latest changes in originals
[lilypond.git] / buildscripts / texi-langutils.py
1 #!@PYTHON@
2 # texi-langutils.py
3
4 # WARNING: this script can't find files included in a different directory
5
6 import sys
7 import re
8 import getopt
9 import os
10
11 def read_pipe (command):
12     print command
13     pipe = os.popen (command)
14     output = pipe.read ()
15     if pipe.close ():
16         print "pipe failed: %(command)s" % locals ()
17     return output
18
19
20 optlist, texi_files = getopt.getopt(sys.argv[1:],'no:d:b:i:l:',['skeleton', 'gettext'])
21 process_includes = not ('-n', '') in optlist # -n   don't process @include's in texinfo files
22
23 make_gettext = ('--gettext', '') in optlist   # --gettext    generate a node list from a Texinfo source
24 make_skeleton = ('--skeleton', '') in optlist # --skeleton   extract the node tree from a Texinfo source
25
26 output_file = 'doc.pot'
27 node_blurb = ''
28 doclang = ''
29 head_committish = read_pipe ('git-rev-parse HEAD')
30 intro_blurb = '''@c -*- coding: utf-8; mode: texinfo%(doclang)s -*-
31 @c This file is part of %(topfile)s
32 @ignore
33     Translation of GIT committish: %(head_committish)s
34
35     When revising a translation, copy the HEAD committish of the
36     version that you are working on.  See TRANSLATION for details.
37 @end ignore
38 '''
39
40 end_blurb = """
41 -- SKELETON FILE --
42 When you actually translate this file, please remove these lines as
43 well as all `UNTRANSLATED NODE: IGNORE ME' lines.
44 """
45
46 for x in optlist:
47         if x[0] == '-o': # -o NAME   set PO output file name to NAME
48                 output_file = x[1]
49         elif x[0] == '-d': # -d DIR    set working directory to DIR
50                 os.chdir (x[1])
51         elif x[0] == '-b': # -b BLURB  set blurb written at each node to BLURB
52                 node_blurb = x[1]
53         elif x[0] == '-i': # -i BLURB  set blurb written at beginning of each file to BLURB
54                 intro_blurb = x[1]
55         elif x[0] == '-l': # -l ISOLANG  set documentlanguage to ISOLANG
56                 doclang = '; documentlanguage: ' + x[1]
57
58
59 def process_texi (texifilename, i_blurb, n_blurb, write_skeleton, topfile, output_file=None):
60         try:
61                 f = open (texifilename, 'r')
62                 texifile = f.read ()
63                 f.close ()
64                 includes = []
65                 if write_skeleton:
66                         g = open (os.path.basename (texifilename), 'w')
67                         subst = globals ()
68                         subst.update (locals ())
69                         g.write (i_blurb % subst)
70                         tutu = re.findall (r"""^(\*) +([^:
71                         ]+)::[^
72                         ]*?$|^@(include|menu|end menu|node|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading) *([^
73                         ]*)[^
74                         ]*?$|@(rglos){(.+?)}""", texifile, re.M)
75                         node_trigger = False
76                         for item in tutu:
77                                 if item[0] == '*':
78                                         g.write ('* ' + item[1] + '::\n')
79                                 elif output_file and item[4] == 'rglos':
80                                         output_file.write ('_(r"' + item[5] + '") # @rglos in ' + texifilename + '\n')
81                                 else:
82                                         g.write ('@' + item[2] + ' ' + item[3] + '\n')
83                                         if node_trigger:
84                                                 g.write (n_blurb)
85                                                 node_trigger = False
86                                         if not item[2] in ('include', 'menu', 'end menu'):
87                                                 if output_file:
88                                                         output_file.write ('_(r"' + item[3].strip () + '") # @' + item[2] + \
89                                                                            ' in ' + texifilename + '\n')
90                                                 if item[2] == 'node':
91                                                         node_trigger = True
92                                         elif item[2] == 'include':
93                                                 includes.append(item[3])
94                         g.write (end_blurb)
95                         g.close ()
96                 elif output_file:
97                         toto = re.findall (r"""^@(include|node|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading) *([^
98                         ]*)[^
99                         ]*?$|@(rglos){(.+?)}""", texifile, re.M)
100                         for item in toto:
101                                 if item[0] == 'include':
102                                         includes.append(item[1])
103                                 elif item[2] == 'rglos':
104                                         output_file.write ('# @rglos in ' + texifilename + '\n_(r"' + item[3] + '")\n')
105                                 else:
106                                         output_file.write ('# @' + item[0] + ' in ' + texifilename + '\n_(r"' + item[1].strip () + '")\n')
107                 if process_includes:
108                         dir = os.path.dirname (texifilename)
109                         for item in includes:
110                                 process_texi (os.path.join (dir, item.strip ()), i_blurb, n_blurb, write_skeleton, topfile, output_file)
111         except IOError, (errno, strerror):
112                 print "I/O error(%s): %s: %s" % (errno, texifilename, strerror)
113
114
115 if intro_blurb != '':
116         intro_blurb += '\n\n'
117 if node_blurb != '':
118         node_blurb = '\n' + node_blurb + '\n\n'
119 if make_gettext:
120         node_list_filename = 'node_list'
121         node_list = open (node_list_filename, 'w')
122         for texi_file in texi_files:
123                 process_texi (texi_file, intro_blurb, node_blurb, make_skeleton, os.path.basename (texi_file), node_list)
124         for word in ('Up:', 'Next:', 'Previous:', 'Appendix ', 'Footnotes', 'Table of Contents'):
125                 node_list.write ('_(r"' + word + '")\n')
126         node_list.close ()
127         os.system ('xgettext -c -L Python --no-location -o ' + output_file + ' ' + node_list_filename)
128 else:
129         for texi_file in texi_files:
130                 process_texi (texi_file, intro_blurb, node_blurb, make_skeleton, os.path.basename (texi_file))