]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/texi-gettext.py
Merge commit 'origin'
[lilypond.git] / buildscripts / texi-gettext.py
1 #!@PYTHON@
2 # -*- coding: utf-8 -*-
3 # texi-gettext.py
4
5 # USAGE:  texi-gettext.py [-o OUTDIR] 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
17 import langdefs
18
19 optlist, args = getopt.getopt (sys.argv[1:],'o:')
20 lang = args[0]
21 files = args[1:]
22
23 outdir = '.'
24 for x in optlist:
25         if x[0] == '-o':
26                 outdir = x[1]
27
28 double_punct_char_separator = langdefs.LANGDICT[lang].double_punct_char_sep
29 _doc = langdefs.translation[lang]
30
31 include_re = re.compile (r'@include ((?!../lily-).*?)\.texi$', re.M)
32 whitespaces = re.compile (r'\s+')
33 ref_re = re.compile (r'(?ms)@(rglos|ruser|rprogram|ref)(\{)(.*?)(\})')
34 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)')
35 menu_entry_re = re.compile (r'\* (.*?)::')
36
37 def title_gettext (m):
38         if m.group (2) == '{':
39                 r = whitespaces.sub (' ', m.group (3))
40         else:
41                 r = m.group (3)
42         return '@' + m.group (1) + m.group (2) + _doc (r) + m.group (4)
43
44 def menu_entry_gettext (m):
45         return '* ' + _doc (m.group (1)) + '::'
46
47 def include_replace (m, filename):
48         if os.path.exists (os.path.join (os.path.dirname (filename), m.group(1)) + '.texi'):
49                 return '@include ' + m.group(1) + '.pdftexi'
50         return m.group(0)
51
52 def process_file (filename):
53         print "Processing %s" % filename
54         f = open (filename, 'r')
55         page = f.read ()
56         f.close()
57         page = node_section_re.sub (title_gettext, page)
58         page = ref_re.sub (title_gettext, page)
59         page = menu_entry_re.sub (menu_entry_gettext, page)
60         page = page.replace ("""-- SKELETON FILE --
61 When you actually translate this file, please remove these lines as
62 well as all `UNTRANSLATED NODE: IGNORE ME' lines.""", '')
63         page = page.replace ('UNTRANSLATED NODE: IGNORE ME', _doc ("This section has not been translated yet; please refer to the manual in English."))
64         includes = include_re.findall (page)
65         page = include_re.sub (lambda m: include_replace (m, filename), page)
66         p = os.path.join (outdir, filename) [:-4] + 'pdftexi'
67         f = open (p, 'w')
68         f.write (page)
69         f.close ()
70         dir = os.path.dirname (filename)
71         for file in includes:
72                 p = os.path.join (dir, file) + '.texi'
73                 if os.path.exists (p):
74                         process_file (p)
75
76 for filename in files:
77         process_file (filename)