]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/texi-langutils.py
WEB: Integration of new web site [general.texi].
[lilypond.git] / scripts / auxiliar / texi-langutils.py
1 #!/usr/bin/env 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 import langdefs
12
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
20
21
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
24
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
27
28 output_name = 'doc.pot'
29
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 = '''\\input texinfo @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 '''
43
44 end_blurb = """
45 @c -- SKELETON FILE --
46 """
47
48 for x in optlist:
49     if x[0] == '-o': # -o NAME   set PO output file name to NAME
50         output_name = x[1]
51     elif x[0] == '-d': # -d DIR    set working directory to DIR
52         print 'FIXME: this is evil.  use cd DIR && texi-langutils ...'
53         # even better, add a sane -o option
54         os.chdir (x[1])
55     elif x[0] == '-b': # -b BLURB  set blurb written at each node to BLURB
56         node_blurb = x[1]
57     elif x[0] == '-i': # -i BLURB  set blurb written at beginning of each file to BLURB
58         intro_blurb = x[1]
59     elif x[0] == '-l': # -l ISOLANG  set documentlanguage to ISOLANG
60         doclang = '; documentlanguage: ' + x[1]
61
62 texinfo_with_menus_re = re.compile (r"^(\*) +([^:\n]+)::.*?$|^@(afourpaper|author|bye|contents|copying|end copying|divClass|divEnd|divId|documentencoding|documentlanguage|finalout|ifnottex|end ifnottex|imageFloat|imageId|include|menu|end menu|node|quotation|end quotation|ref|rgloss|setfilename|settitle|set|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|titlefont|titlepage|end titlepage|title|subtitle|top|vskip|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading|c) *(([^ \n].*)|$)", re.M)
63
64 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)
65
66 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+')
67 lsr_verbatim_ly_re = re.compile (r'% begin verbatim$')
68 texinfo_verbatim_ly_re = re.compile (r'^@lilypond\[.*?verbatim')
69
70 def process_texi (texifilename, i_blurb, n_blurb, write_skeleton, topfile, output_file=None, scan_ly=False):
71     try:
72         f = open (texifilename, 'r')
73         texifile = f.read ()
74         f.close ()
75         printedfilename = texifilename.replace ('../','')
76         includes = []
77
78         # process ly var names and comments
79         if output_file and (scan_ly or texifilename.endswith ('.ly')):
80             lines = texifile.splitlines ()
81             i = 0
82             in_verb_ly_block = False
83             if texifilename.endswith ('.ly'):
84                 verbatim_ly_re = lsr_verbatim_ly_re
85             else:
86                 verbatim_ly_re = texinfo_verbatim_ly_re
87             for i in range (len (lines)):
88                 if verbatim_ly_re.search (lines[i]):
89                     in_verb_ly_block = True
90                 elif lines[i].startswith ('@end lilypond'):
91                     in_verb_ly_block = False
92                 elif in_verb_ly_block:
93                     for (var, comment, context_id) in ly_string_re.findall (lines[i]):
94                         if var:
95                             output_file.write ('# ' + printedfilename + ':' + \
96                                                str (i + 1) + ' (variable)\n_(r"' + var + '")\n')
97                         elif comment:
98                             output_file.write ('# ' + printedfilename + ':' + \
99                                                str (i + 1) + ' (comment)\n_(r"' + \
100                                                comment.replace ('"', '\\"') + '")\n')
101                         elif context_id:
102                             output_file.write ('# ' + printedfilename + ':' + \
103                                                str (i + 1) + ' (context id)\n_(r"' + \
104                                                context_id + '")\n')
105
106         # process Texinfo node names and section titles
107         if write_skeleton:
108             g = open (os.path.basename (texifilename), 'w')
109             subst = globals ()
110             subst.update (locals ())
111             g.write (i_blurb % subst)
112             tutu = texinfo_with_menus_re.findall (texifile)
113             node_trigger = False
114             for item in tutu:
115                 if item[0] == '*':
116                     g.write ('* ' + item[1] + '::\n')
117                 elif output_file and item[4] == 'rglos':
118                     output_file.write ('_(r"' + item[5] + '") # @rglos in ' + printedfilename + '\n')
119                 elif item[2] == 'menu':
120                     g.write ('@menu\n')
121                 elif item[2] == 'end menu':
122                     g.write ('@end menu\n\n')
123                 else:
124                     space = ' '
125                     if item[3].startswith ('{') or not item[3].strip ():
126                         space = ''
127                     g.write ('@' + item[2] + space + item[3] + '\n')
128                     if node_trigger:
129                         g.write (n_blurb)
130                         node_trigger = False
131                     elif item[2] == 'include':
132                         includes.append (item[3])
133                     else:
134                         if output_file:
135                             output_file.write ('# @' + item[2] + ' in ' + \
136                                 printedfilename + '\n_(r"' + item[3].strip () + '")\n')
137                         if item[2] == 'node':
138                             node_trigger = True
139             g.write (end_blurb)
140             g.close ()
141
142         elif output_file:
143             toto = texinfo_re.findall (texifile)
144             for item in toto:
145                 if item[0] == 'include':
146                     includes.append(item[1])
147                 elif item[2] == 'rglos':
148                     output_file.write ('# @rglos in ' + printedfilename + '\n_(r"' + item[3] + '")\n')
149                 else:
150                     output_file.write ('# @' + item[0] + ' in ' + printedfilename + '\n_(r"' + item[1].strip () + '")\n')
151
152         if process_includes:
153             dir = os.path.dirname (texifilename)
154             for item in includes:
155                 process_texi (os.path.join (dir, item.strip ()), i_blurb, n_blurb, write_skeleton, topfile, output_file, scan_ly)
156     except IOError, (errno, strerror):
157         sys.stderr.write ("I/O error(%s): %s: %s\n" % (errno, texifilename, strerror))
158
159
160 if intro_blurb != '':
161     intro_blurb += '\n\n'
162 if node_blurb != '':
163     node_blurb = '\n' + node_blurb + '\n\n'
164 if make_gettext:
165     node_list_filename = 'node_list'
166     node_list = open (node_list_filename, 'w')
167     node_list.write ('# -*- coding: utf-8 -*-\n')
168     for texi_file in texi_files:
169         # Urgly: scan ly comments and variable names only in English doco
170         is_english_doc = (
171             True
172             and not 'Documentation/de/' in texi_file
173             and not 'Documentation/es/' in texi_file
174             and not 'Documentation/fr/' in texi_file
175             and not 'Documentation/ja/' in texi_file
176             and not 'Documentation/nl/' in texi_file
177             and not 'Documentation/po/' in texi_file
178             )
179         process_texi (texi_file, intro_blurb, node_blurb, make_skeleton,
180                       os.path.basename (texi_file), node_list,
181                       scan_ly=is_english_doc)
182     for word in ('Up:', 'Next:', 'Previous:', 'Appendix ', 'Footnotes', 'Table of Contents'):
183         node_list.write ('_(r"' + word + '")\n')
184     node_list.close ()
185     os.system ('xgettext -c -L Python --no-location -o ' + output_name + ' ' + node_list_filename)
186 else:
187     for texi_file in texi_files:
188         process_texi (texi_file, intro_blurb, node_blurb, make_skeleton,
189                       os.path.basename (texi_file))