]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/texi-gettext.py
49cc62288b2cb7ce25fc3bec13e6c666096db33c
[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 # Why not use recode?
40 # - well, it would add one more dependency...
41 accents2texi = (
42         ("á", "@'a"),
43         ("à", "@`a"),
44         ("â", "@^a"),
45         ("ä", "@\"a"),
46         ("é", "@'e"),
47         ("è", "@`e"),
48         ("ê", "@^e"),
49         ("ë", "@\"e"),
50         ("ó", "@'o"),
51         ("ò", "@`o"),
52         ("ô", "@^o"),
53         ("ö", "@\"o"),
54         ("ú", "@'u"),
55         ("ù", "@`u"),
56         ("û", "@^u"),
57         ("ü", "@\"u"),
58         ("ç", "@,{c}"),
59         ("À", "@`A"),
60         ("Á", "@'A"),
61         ("Â", "@^A"),
62         ("Ä", "@\"A"),
63         ("É", "@'E"),
64         ("È", "@`E"),
65         ("Ê", "@^E"),
66         ("Ë", "@\"E"),
67         ("Ó", "@'O"),
68         ("Ò", "@`O"),
69         ("Ô", "@^O"),
70         ("Ö", "@\"O"),
71         ("Ú ", "@'U"),
72         ("Ù", "@`U"),
73         ("Û", "@^U"),
74         ("Ü", "@\"U"),
75         ("Ç", "@,{C}"),
76         ("Í", "@'{@dotless{i}}"),
77         ("ì", "@`{@dotless{i}}"),
78         ("î", "@^{@dotless{i}}"),
79         ("ï", "@\"{@dotless{i}}"),
80         ("Í", "@'I"),
81         ("Ì", "@`I"),
82         ("Î", "@^I"),
83         ("Ï", "@\"I"),
84         ("œ", "@oe{}"),
85         ("Œ", "@OE{}"),
86         ("æ", "@ae{}"),
87         ("Æ", "@AE{}"),
88         ("¡", "@exclamdown{}"),
89         ("¿", "@questiondown{}"),
90         ("ø", "@o{}"),
91         ("Ø", "@O{}"),
92         ("ß", "@ss{}"),
93         ("ł", "@l{}"),
94         ("Ł", "@L{}"),
95         ("å", "@aa{}"),
96         ("Å", "@AA{}"))
97
98
99 def title_gettext (m):
100         if m.group (2) == '{':
101                 r = whitespaces.sub (' ', m.group (3))
102         else:
103                 r = m.group (3)
104         return '@' + m.group (1) + m.group (2) + _doc (r) + m.group (4)
105
106 def menu_entry_gettext (m):
107         return '* ' + _doc (m.group (1)) + '::'
108
109 def include_replace (m, filename):
110         if os.path.exists (os.path.join (os.path.dirname (filename), m.group(1)) + '.texi'):
111                 return '@include ' + m.group(1) + '.pdftexi'
112         return m.group(0)
113
114 def process_file (filename):
115         print "Processing %s" % filename
116         f = open (filename, 'r')
117         page = f.read ()
118         f.close()
119         page = node_section_re.sub (title_gettext, page)
120         page = ref_re.sub (title_gettext, page)
121         page = menu_entry_re.sub (menu_entry_gettext, page)
122         page = page.replace ("""-- SKELETON FILE --
123 When you actually translate this file, please remove these lines as
124 well as all `UNTRANSLATED NODE: IGNORE ME' lines.""", '')
125         page = page.replace ('UNTRANSLATED NODE: IGNORE ME', _doc ("This section has not been translated yet; please refer to the manual in English."))
126         includes = include_re.findall (page)
127         page = include_re.sub (lambda m: include_replace (m, filename), page)
128         for (u_char, texiaccent_char) in accents2texi:
129                 page = page.replace (u_char, texiaccent_char)
130         p = os.path.join (outdir, filename) [:-4] + 'pdftexi'
131         f = open (p, 'w')
132         f.write (page)
133         f.close ()
134         dir = os.path.dirname (filename)
135         for file in includes:
136                 p = os.path.join (dir, file) + '.texi'
137                 if os.path.exists (p):
138                         process_file (p)
139
140 for filename in args[3:]:
141         process_file (filename)