]> git.donarmstrong.com Git - lilypond.git/blob - scripts/aux/tely-gettext.py
Clean up buildscripts
[lilypond.git] / scripts / aux / tely-gettext.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Temporary script that helps translated docs sources conversion
5 # for texi2html processing
6
7 # USAGE:  tely-gettext.py PYTHON-DIR LOCALEDIR LANG FILES
8
9 print "tely-gettext.py"
10
11 import sys
12 import re
13 import os
14 import gettext
15
16 if len (sys.argv) > 3:
17     buildscript_dir, localedir, lang = sys.argv[1:4]
18 else:
19     print """USAGE:  tely-gettext.py PYTHON-DIR LOCALEDIR LANG FILES
20   For example scripts/aux/tely-gettext.py python/out Documentation/po/out-www de Documentation/de/user/*.tely"""
21     sys.exit (1)
22
23 sys.path.append (buildscript_dir)
24 import langdefs
25
26 double_punct_char_separator = langdefs.LANGDICT[lang].double_punct_char_sep
27 t = gettext.translation('lilypond-doc', localedir, [lang])
28 _doc = t.gettext
29
30 include_re = re.compile (r'@include (.*?)$', re.M)
31 whitespaces = re.compile (r'\s+')
32 ref_re = re.compile (r'(?ms)@(ruser|rprogram|ref|rlearning)\{(.*?)\}')
33 node_section_re = re.compile (r'@node (.*?)\n@((?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading) (.*?)\n')
34 menu_entry_re = re.compile (r'\* (.*?)::')
35
36 def ref_gettext (m):
37     r = whitespaces.sub (' ', m.group (2))
38     return '@' + m.group (1) + '{' + _doc (r) + '}'
39
40 def node_gettext (m):
41     return '@node ' + _doc (m.group (1)) + '\n@' + \
42         m.group (2) + ' ' + _doc (m.group (3)) + \
43         '\n@translationof ' + m.group (1) + '\n'
44
45 def menu_entry_gettext (m):
46     return '* ' + _doc (m.group (1)) + '::'
47
48 def process_file (filename):
49     print "Processing %s" % filename
50     f = open (filename, 'r')
51     page = f.read ()
52     f.close()
53     page = node_section_re.sub (node_gettext, page)
54     page = ref_re.sub (ref_gettext, page)
55     page = menu_entry_re.sub (menu_entry_gettext, page)
56     page = page.replace ("""-- SKELETON FILE --
57 When you actually translate this file, please remove these lines as
58 well as all `UNTRANSLATED NODE: IGNORE ME' lines.""", """@c -- SKELETON FILE --""")
59     page = page.replace ('UNTRANSLATED NODE: IGNORE ME', "@c UNTRANSLATED NODE: IGNORE ME")
60     includes = [whitespaces.sub ('', f) for f in include_re.findall (page)]
61     f = open (filename, 'w')
62     f.write (page)
63     f.close ()
64     dir = os.path.dirname (filename)
65     for file in includes:
66         p = os.path.join (dir, file)
67         if os.path.exists (p):
68             process_file (p)
69
70 for filename in sys.argv[4:]:
71     process_file (filename)