]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/texi-langutils.py
Merge branch 'master' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[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 topfile = os.path.basename (texi_files[0])
30 head_committish = read_pipe ('git-rev-parse HEAD')
31 intro_blurb = '''@c -*- coding: utf-8; mode: texinfo%(doclang)s -*-
32 @c This file is part of %(topfile)s
33 @ignore
34     Translation of GIT committish: %(head_committish)s
35
36     When revising a translation, copy the HEAD committish of the
37     version that you are working on.  See TRANSLATION for details.
38 @end ignore
39 '''
40
41 end_blurb = """
42 -- SKELETON FILE --
43 When you actually translate this file, please remove these lines as
44 well as all `UNTRANSLATED NODE: IGNORE ME' lines.
45 """
46
47 for x in optlist:
48         if x[0] == '-o': # -o NAME   set PO output file name to NAME
49                 output_file = x[1]
50         elif x[0] == '-d': # -d DIR    set working directory to DIR
51                 os.chdir (x[1])
52         elif x[0] == '-b': # -b BLURB  set blurb written at each node to BLURB
53                 node_blurb = x[1]
54         elif x[0] == '-i': # -i BLURB  set blurb written at beginning of each file to BLURB
55                 intro_blurb = x[1]
56         elif x[0] == '-l': # -l ISOLANG  set documentlanguage to ISOLANG
57                 doclang = '; documentlanguage: ' + x[1]
58
59
60 intro_blurb = intro_blurb % vars()
61
62
63 def process_texi (texifilename, i_blurb, n_blurb, write_skeleton, output_file=None):
64         try:
65                 f = open (texifilename, 'r')
66                 texifile = f.read ()
67                 f.close ()
68                 includes = []
69                 if write_skeleton:
70                         g = open (os.path.basename (texifilename), 'w')
71                         g.write (i_blurb)
72                         tutu = re.findall (r"""^(\*) +([^:
73                         ]+)::[^
74                         ]*?$|^@(include|menu|end menu|node|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading) *([^
75                         ]*)[^
76                         ]*?$|@(rglos){(.+?)}""", texifile, re.M)
77                         node_trigger = False
78                         for item in tutu:
79                                 if item[0] == '*':
80                                         g.write ('* ' + item[1] + '::\n')
81                                 elif output_file and item[4] == 'rglos':
82                                         output_file.write ('_("' + item[5] + '") # @rglos in ' + texifilename + '\n')
83                                 else:
84                                         g.write ('@' + item[2] + ' ' + item[3] + '\n')
85                                         if node_trigger:
86                                                 g.write (n_blurb)
87                                                 node_trigger = False
88                                         if not item[2] in ('include', 'menu', 'end menu'):
89                                                 if output_file:
90                                                         output_file.write ('_("' + item[3].strip () + '") # @' + item[2] + \
91                                                                            ' in ' + texifilename + '\n')
92                                                 if item[2] == 'node':
93                                                         node_trigger = True
94                                         elif item[2] == 'include':
95                                                 includes.append(item[3])
96                         g.write (end_blurb)
97                         g.close ()
98                 elif output_file:
99                         toto = re.findall (r"""^@(include|node|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading) *([^
100                         ]*)[^
101                         ]*?$|@(rglos){(.+?)}""", texifile, re.M)
102                         for item in toto:
103                                 if item[0] == 'include':
104                                         includes.append(item[1])
105                                 elif item[2] == 'rglos':
106                                         output_file.write ('# @rglos in ' + texifilename + '\n_("' + item[3] + '")\n')
107                                 else:
108                                         output_file.write ('# @' + item[0] + ' in ' + texifilename + '\n_("' + item[1].strip () + '")\n')
109                 if process_includes:
110                         dir = os.path.dirname (texifilename)
111                         for item in includes:
112                                 process_texi (os.path.join (dir, item.strip ()), i_blurb, n_blurb, write_skeleton, output_file)
113         except IOError, (errno, strerror):
114                 print "I/O error(%s): %s: %s" % (errno, texifilename, strerror)
115
116
117 if intro_blurb != '':
118         intro_blurb += '\n\n'
119 if node_blurb != '':
120         node_blurb = '\n' + node_blurb + '\n\n'
121 if make_gettext:
122         node_list_filename = 'node_list'
123         node_list = open (node_list_filename, 'w')
124         for texi_file in texi_files:
125                 process_texi (texi_file, intro_blurb, node_blurb, make_skeleton, node_list)
126         for word in ('Up:', 'Next:', 'Previous:', 'Appendix', 'Footnotes'):
127                 node_list.write ('_("' + word + '")\n')
128         node_list.close ()
129         os.system ('xgettext -c -L Python --no-location -o ' + output_file + ' ' + node_list_filename)
130 else:
131         for texi_file in texi_files:
132                 process_texi (texi_file, intro_blurb, node_blurb, make_skeleton)