4 # WARNING: this script can't find files included in a different directory
13 def read_pipe (command):
15 pipe = os.popen (command)
18 print "pipe failed: %(command)s" % locals ()
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
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
29 output_name = 'doc.pot'
31 # @untranslated should be defined as a macro in Texinfo source
32 node_blurb = '''@untranslated
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
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.
46 @c -- SKELETON FILE --
50 if x[0] == '-o': # -o NAME set PO output file name to NAME
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
56 elif x[0] == '-b': # -b BLURB set blurb written at each node to BLURB
58 elif x[0] == '-i': # -i BLURB set blurb written at beginning of each file to BLURB
60 elif x[0] == '-l': # -l ISOLANG set documentlanguage to ISOLANG
61 doclang = '; documentlanguage: ' + x[1]
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)
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)
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')
71 def process_texi (texifilename, i_blurb, n_blurb, write_skeleton, topfile,
72 output_file=None, scan_ly=False, inclusion_level=0):
74 f = open (texifilename, 'r')
77 printedfilename = texifilename.replace ('../','')
80 # process ly var names and comments
81 if output_file and (scan_ly or texifilename.endswith ('.ly')):
82 lines = texifile.splitlines ()
84 in_verb_ly_block = False
85 if texifilename.endswith ('.ly'):
86 verbatim_ly_re = lsr_verbatim_ly_re
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]):
97 output_file.write ('# ' + printedfilename + ':' + \
98 str (i + 1) + ' (variable)\n_(r"' + var + '")\n')
100 output_file.write ('# ' + printedfilename + ':' + \
101 str (i + 1) + ' (comment)\n_(r"' + \
102 comment.replace ('"', '\\"') + '")\n')
104 output_file.write ('# ' + printedfilename + ':' + \
105 str (i + 1) + ' (context id)\n_(r"' + \
108 # process Texinfo node names and section titles
110 g = open (os.path.basename (texifilename), 'w')
112 subst.update (locals ())
113 g.write (i_blurb % subst)
114 tutu = texinfo_with_menus_re.findall (texifile)
115 node_just_defined = ''
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':
123 elif item[2] == 'end menu':
124 g.write ('@end menu\n\n')
125 elif item[2] == 'documentlanguage':
126 g.write ('@documentlanguage ' + doclang + '\n')
129 if item[3].startswith ('{') or not item[3].strip ():
131 g.write ('@' + item[2] + space + item[3] + '\n')
132 if node_just_defined:
133 g.write ('@translationof ' + node_just_defined + '\n')
135 node_just_defined = ''
136 if head_only and inclusion_level == 1:
138 elif item[2] == 'include':
139 includes.append (item[3])
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 ()
150 elif output_file and scan_ly:
151 toto = texinfo_re.findall (texifile)
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')
158 output_file.write ('# @' + item[0] + ' in ' + printedfilename + '\n_(r"' + item[1].strip ().replace ('\\', r'\\') + '")\n')
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))
169 if intro_blurb != '':
170 intro_blurb += '\n\n'
172 node_blurb = '\n' + node_blurb + '\n\n'
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
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
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')
198 os.system ('xgettext --keyword=_doc -c -L Python --no-location -o ' + output_name + ' ' + node_list_filename)
200 for texi_file in texi_files:
201 process_texi (texi_file, intro_blurb, node_blurb, make_skeleton,
202 os.path.basename (texi_file))