]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/tely-gettext.py
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scripts / auxiliar / 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 LANG FILES
8
9 print "tely-gettext.py"
10
11 import sys
12 import re
13 import os
14
15 import langdefs
16
17 lang = sys.argv[1]
18 files = sys.argv[2:]
19
20 double_punct_char_separator = langdefs.LANGDICT[lang].double_punct_char_sep
21 _doc = langdefs.translation[lang]
22
23 include_re = re.compile (r'@include (.*?)$', re.M)
24 whitespaces = re.compile (r'\s+')
25 ref_re = re.compile (r'(?ms)@((?:ressay|rgloss|rinternals|rlearning|rslr|rprogram|ruser|ref)|named)\{(.*?)\}')
26 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')
27 section_only_re = re.compile (r'@((?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading) (.*?)\n')
28 menu_entry_re = re.compile (r'\* (.*?)::')
29 untranslated_node_re = re.compile (r'(@node\s+.*\n@.*\n.*)(\s+@untranslated(.|\n)+?)(?=@node|@subheading|@menu|$)')
30
31
32 def ref_gettext (m):
33     r = whitespaces.sub (' ', m.group (2))
34     return '@' + m.group (1) + '{' + _doc (r) + '}'
35
36 def node_gettext (m):
37     return '@node ' + _doc (m.group (1)) + '\n@' + \
38         m.group (2) + ' ' + _doc (m.group (3)) + \
39        '\n@translationof ' + m.group (1) + '\n'
40
41 def section_gettext (m):
42     return '@' + m.group (1) + ' ' + _doc (m.group (2)) + '\n'
43
44 def menu_entry_gettext (m):
45     return '* ' + _doc (m.group (1)) + '::'
46
47 def process_file (filename, master_file_dir='.', included=False):
48     print "Processing %s" % filename
49     f = open (filename, 'r')
50     page = f.read ()
51     f.close()
52     page = ref_re.sub (ref_gettext, page)
53     if not '\\n@translationof' in page:
54         page = node_section_re.sub (node_gettext, page)
55     page = section_only_re.sub (section_gettext, page)
56     page = menu_entry_re.sub (menu_entry_gettext, page)
57     page = page.replace ("""@c -- SKELETON FILE --
58 """, '')
59     page = page.replace ('UNTRANSLATED NODE: IGNORE ME', '@untranslated')
60     page = untranslated_node_re.sub ('\\1 @c external\\2', page)
61     includes = include_re.findall (page)
62     f = open (filename, 'w')
63     f.write (page)
64     f.close ()
65     dir = os.path.dirname (filename)
66     if not included:
67         master_file_dir = dir
68     for file in includes:
69         p = os.path.join (dir, file)
70         if os.path.exists (p):
71             process_file (p, master_file_dir, included=True)
72         else:
73             p = os.path.join (master_file_dir, file)
74             if os.path.exists (p):
75                 process_file (p, master_file_dir, included=True)
76
77 for filename in files:
78     process_file (filename)