]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/texi-langutils.py
Add '-dcrop' option to ps and svg backends
[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', 'head-only'])
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 head_only = ('--head-only', '') in optlist # --head-only  only write first node in included Texinfo skeletons
28
29 output_name = 'doc.pot'
30
31 # @untranslated should be defined as a macro in Texinfo source
32 node_blurb = '''@untranslated
33 '''
34 doclang = ''
35 head_committish = read_pipe ('git rev-parse HEAD')
36 intro_blurb = '''\\input texinfo @c -*- coding: utf-8; mode: texinfo%(doclang)s -*-
37 @c This file is part of %(topfile)s
38 @ignore
39     Translation of GIT committish: %(head_committish)s
40     When revising a translation, copy the HEAD committish of the
41     version that you are working on.  See TRANSLATION for details.
42 @end ignore
43 '''
44
45 end_blurb = """
46 @c -- SKELETON FILE --
47 """
48
49 for x in optlist:
50     if x[0] == '-o': # -o NAME   set PO output file name to NAME
51         output_name = x[1]
52     elif x[0] == '-d': # -d DIR    set working directory to DIR
53         print 'FIXME: this is evil.  use cd DIR && texi-langutils ...'
54         # even better, add a sane -o option
55         os.chdir (x[1])
56     elif x[0] == '-b': # -b BLURB  set blurb written at each node to BLURB
57         node_blurb = x[1]
58     elif x[0] == '-i': # -i BLURB  set blurb written at beginning of each file to BLURB
59         intro_blurb = x[1]
60     elif x[0] == '-l': # -l ISOLANG  set documentlanguage to ISOLANG
61         doclang = '; documentlanguage: ' + x[1]
62
63 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)
64
65 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)
66
67 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+')
68 lsr_verbatim_ly_re = re.compile (r'% begin verbatim$')
69 texinfo_verbatim_ly_re = re.compile (r'^@lilypond\[.*?verbatim')
70
71 def process_texi (texifilename, i_blurb, n_blurb, write_skeleton, topfile,
72                   output_file=None, scan_ly=False, inclusion_level=0):
73     try:
74         f = open (texifilename, 'r')
75         texifile = f.read ()
76         f.close ()
77         printedfilename = texifilename.replace ('../','')
78         includes = []
79
80         # process ly var names and comments
81         if output_file and (scan_ly or texifilename.endswith ('.ly')):
82             lines = texifile.splitlines ()
83             i = 0
84             in_verb_ly_block = False
85             if texifilename.endswith ('.ly'):
86                 verbatim_ly_re = lsr_verbatim_ly_re
87             else:
88                 verbatim_ly_re = texinfo_verbatim_ly_re
89             for i in range (len (lines)):
90                 if verbatim_ly_re.search (lines[i]):
91                     in_verb_ly_block = True
92                 elif lines[i].startswith ('@end lilypond'):
93                     in_verb_ly_block = False
94                 elif in_verb_ly_block:
95                     for (var, comment, context_id) in ly_string_re.findall (lines[i]):
96                         if var:
97                             output_file.write ('# ' + printedfilename + ':' + \
98                                                str (i + 1) + ' (variable)\n_(r"' + var + '")\n')
99                         elif comment:
100                             output_file.write ('# ' + printedfilename + ':' + \
101                                                str (i + 1) + ' (comment)\n_(r"' + \
102                                                comment.replace ('"', '\\"') + '")\n')
103                         elif context_id:
104                             output_file.write ('# ' + printedfilename + ':' + \
105                                                str (i + 1) + ' (context id)\n_(r"' + \
106                                                context_id + '")\n')
107
108         # process Texinfo node names and section titles
109         if write_skeleton:
110             g = open (os.path.basename (texifilename), 'w')
111             subst = globals ()
112             subst.update (locals ())
113             g.write (i_blurb % subst)
114             tutu = texinfo_with_menus_re.findall (texifile)
115             node_just_defined = ''
116             for item in tutu:
117                 if item[0] == '*':
118                     g.write ('* ' + item[1] + '::\n')
119                 elif output_file and item[4] == 'rglos':
120                     output_file.write ('_(r"' + item[5] + '") # @rglos in ' + printedfilename + '\n')
121                 elif item[2] == 'menu':
122                     g.write ('@menu\n')
123                 elif item[2] == 'end menu':
124                     g.write ('@end menu\n\n')
125                 elif item[2] == 'documentlanguage':
126                     g.write ('@documentlanguage ' + doclang + '\n')
127                 else:
128                     space = ' '
129                     if item[3].startswith ('{') or not item[3].strip ():
130                         space = ''
131                     g.write ('@' + item[2] + space + item[3] + '\n')
132                     if node_just_defined:
133                         g.write ('@translationof ' + node_just_defined + '\n')
134                         g.write (n_blurb)
135                         node_just_defined = ''
136                         if head_only and inclusion_level == 1:
137                             break
138                     elif item[2] == 'include':
139                         includes.append (item[3])
140                     else:
141                         if output_file:
142                             output_file.write ('# @' + item[2] + ' in ' + \
143                                 printedfilename + '\n_(r"' + item[3].strip () + '")\n')
144                         if item[2] == 'node':
145                             node_just_defined = item[3].strip ()
146             if not head_only:
147                 g.write (end_blurb)
148             g.close ()
149
150         elif output_file and scan_ly:
151             toto = texinfo_re.findall (texifile)
152             for item in toto:
153                 if item[0] == 'include':
154                     includes.append(item[1])
155                 elif item[2] == 'rglos':
156                     output_file.write ('# @rglos in ' + printedfilename + '\n_(r"' + item[3] + '")\n')
157                 else:
158                     output_file.write ('# @' + item[0] + ' in ' + printedfilename + '\n_(r"' + item[1].strip ().replace ('\\', r'\\') + '")\n')
159
160         if process_includes and (not head_only or inclusion_level < 1):
161             dir = os.path.dirname (texifilename)
162             for item in includes:
163                 process_texi (os.path.join (dir, item.strip ()), i_blurb, n_blurb,
164                               write_skeleton, topfile, output_file, scan_ly, inclusion_level + 1)
165     except IOError, (errno, strerror):
166         sys.stderr.write ("I/O error(%s): %s: %s\n" % (errno, texifilename, strerror))
167
168
169 if intro_blurb != '':
170     intro_blurb += '\n\n'
171 if node_blurb != '':
172     node_blurb = '\n' + node_blurb + '\n\n'
173 if make_gettext:
174     node_list_filename = 'node_list'
175     node_list = open (node_list_filename, 'w')
176     node_list.write ('# -*- coding: utf-8 -*-\n')
177     for texi_file in texi_files:
178         # Urgly: scan ly comments and variable names only in English doco
179         is_english_doc = (
180             True
181             and not 'Documentation/cs/' in texi_file
182             and not 'Documentation/de/' in texi_file
183             and not 'Documentation/es/' in texi_file
184             and not 'Documentation/fr/' in texi_file
185             and not 'Documentation/hu/' in texi_file
186             and not 'Documentation/ja/' in texi_file
187             and not 'Documentation/it/' in texi_file
188             and not 'Documentation/nl/' in texi_file
189             and not 'Documentation/po/' in texi_file
190             and not 'Documentation/zh/' in texi_file
191             )
192         process_texi (texi_file, intro_blurb, node_blurb, make_skeleton,
193                       os.path.basename (texi_file), node_list,
194                       scan_ly=is_english_doc)
195     for word in ('Up:', 'Next:', 'Previous:', 'Appendix ', 'Footnotes', 'Table of Contents'):
196         node_list.write ('_(r"' + word + '")\n')
197     node_list.close ()
198     os.system ('xgettext --keyword=_doc -c -L Python --no-location -o ' + output_name + ' ' + node_list_filename)
199 else:
200     for texi_file in texi_files:
201         process_texi (texi_file, intro_blurb, node_blurb, make_skeleton,
202                       os.path.basename (texi_file))