summaryrefslogtreecommitdiff
path: root/scripts/build/texi-gettext.py
blob: 7ce17cb49a5db9563a352231115ad499c46bc66c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!@PYTHON@
# -*- coding: utf-8 -*-
# texi-gettext.py

# USAGE:  texi-gettext.py [-o OUTDIR] LANG FILES
#
# -o OUTDIR specifies that output files should rather be written in OUTDIR
#

print "texi-gettext.py"

import sys
import re
import os
import getopt

import langdefs

optlist, args = getopt.getopt (sys.argv[1:],'o:')
lang = args[0]
files = args[1:]

outdir = '.'
for x in optlist:
    if x[0] == '-o':
        outdir = x[1]

double_punct_char_separator = langdefs.LANGDICT[lang].double_punct_char_sep
_doc = langdefs.translation[lang]

include_re = re.compile (r'@include ((?!../lily-).*?)\.texi$', re.M)
whitespaces = re.compile (r'\s+')
ref_re = re.compile (r'(?ms)@(rglos|ruser|rprogram|ref|rlearning)(\{)(.*?)(\})')
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)')
menu_entry_re = re.compile (r'\* (.*?)::')

def title_gettext (m):
    if m.group (2) == '{':
        r = whitespaces.sub (' ', m.group (3))
    else:
        r = m.group (3)
    return '@' + m.group (1) + m.group (2) + _doc (r) + m.group (4)

def menu_entry_gettext (m):
    return '* ' + _doc (m.group (1)) + '::'

def include_replace (m, filename):
    if os.path.exists (os.path.join (os.path.dirname (filename), m.group(1)) + '.texi'):
        return '@include ' + m.group(1) + '.pdftexi'
    return m.group(0)

def process_file (filename):
    print "Processing %s" % filename
    f = open (filename, 'r')
    page = f.read ()
    f.close()
    page = node_section_re.sub (title_gettext, page)
    page = ref_re.sub (title_gettext, page)
    page = menu_entry_re.sub (menu_entry_gettext, page)
    page = page.replace ("""-- SKELETON FILE --
When you actually translate this file, please remove these lines as
well as all `UNTRANSLATED NODE: IGNORE ME' lines.""", '')
    page = page.replace ('UNTRANSLATED NODE: IGNORE ME', _doc ("This section has not been translated yet; please refer to the manual in English."))
    includes = include_re.findall (page)
    page = include_re.sub (lambda m: include_replace (m, filename), page)
    p = os.path.join (outdir, filename) [:-4] + 'pdftexi'
    f = open (p, 'w')
    f.write (page)
    f.close ()
    dir = os.path.dirname (filename)
    for file in includes:
        p = os.path.join (dir, file) + '.texi'
        if os.path.exists (p):
            process_file (p)

for filename in files:
    process_file (filename)