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