]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/texi-gettext.py
Update texinfo.tex from Texinfo CVS
[lilypond.git] / buildscripts / texi-gettext.py
1 #!@PYTHON@
2 # -*- coding: utf-8 -*-
3 # texi-gettext.py
4
5 # USAGE:  texi-gettext.py [-o OUTDIR] BUILDSCRIPT-DIR LOCALEDIR LANG FILES
6 #
7 # -o OUTDIR specifies that output files should rather be written in OUTDIR
8 #
9
10 print "texi_gettext.py"
11
12 import sys
13 import re
14 import os
15 import getopt
16 import gettext
17
18 optlist, args = getopt.getopt (sys.argv[1:],'o:')
19 buildscript_dir, localedir, lang = args[0:3]
20
21 outdir = '.'
22 for x in optlist:
23         if x[0] == '-o':
24                 outdir = x[1]
25
26 sys.path.append (buildscript_dir)
27 import langdefs
28
29 double_punct_char_separator = langdefs.LANGDICT[lang].double_punct_char_sep
30 t = gettext.translation('lilypond-doc', localedir, [lang])
31 _doc = t.gettext
32
33 include_re = re.compile (r'@include ((?!lily-).*?)\.texi$', re.M)
34 whitespaces = re.compile (r'\s+')
35 ref_re = re.compile (r'(?ms)@(rglos|ref)(\{)(.*?)(\})')
36 node_section_re = re.compile (r'@(node|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading)( )(.*?)(\n)')
37 menu_entry_re = re.compile (r'\* (.*?)::')
38
39 def title_gettext (m):
40         if m.group (2) == '{':
41                 r = whitespaces.sub (' ', m.group (3))
42         else:
43                 r = m.group (3)
44         return '@' + m.group (1) + m.group (2) + _doc (r) + m.group (4)
45
46 def menu_entry_gettext (m):
47         return '* ' + _doc (m.group (1)) + '::'
48
49 def include_replace (m, filename):
50         if os.path.exists (os.path.join (os.path.dirname (filename), m.group(1)) + '.texi'):
51                 return '@include ' + m.group(1) + '.pdftexi'
52         return m.group(0)
53
54 def process_file (filename):
55         print "Processing %s" % filename
56         f = open (filename, 'r')
57         page = f.read ()
58         f.close()
59         page = node_section_re.sub (title_gettext, page)
60         page = ref_re.sub (title_gettext, page)
61         page = menu_entry_re.sub (menu_entry_gettext, page)
62         page = page.replace ("""-- SKELETON FILE --
63 When you actually translate this file, please remove these lines as
64 well as all `UNTRANSLATED NODE: IGNORE ME' lines.""", '')
65         page = page.replace ('UNTRANSLATED NODE: IGNORE ME', _doc ("This section has not been translated yet; please refer to the manual in English."))
66         includes = include_re.findall (page)
67         page = include_re.sub (lambda m: include_replace (m, filename), page)
68         p = os.path.join (outdir, filename) [:-4] + 'pdftexi'
69         f = open (p, 'w')
70         f.write (page)
71         f.close ()
72         dir = os.path.dirname (filename)
73         for file in includes:
74                 p = os.path.join (dir, file) + '.texi'
75                 if os.path.exists (p):
76                         process_file (p)
77
78 for filename in args[3:]:
79         process_file (filename)