summaryrefslogtreecommitdiff
path: root/bin/tpl2c.py
blob: 4fa3eb90a1cff97b658a411f75ca2c942d4aff6f (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
78
79
80
#!/usr/bin/env python2.7
import sys, os, re, json
from lxml import etree
import lxml.html.soupparser as soupparser

BASEPATH = os.path.realpath(os.path.join(os.path.dirname(sys.argv[0]), '..'))
INPUTPATH = os.path.realpath(sys.argv[1])
if not INPUTPATH.startswith(BASEPATH):
    raise IOError('File outside project path: ' + FILEPATH)

FILEPATH = INPUTPATH[len(BASEPATH):].lstrip('/')
OUTPUTPATH = os.path.join(sys.argv[2], FILEPATH[0:FILEPATH.rfind('.')] + '.c')

namespaces = {'com': 'https://github.com/pradosoft/prado/com',
              'prop': 'https://github.com/pradosoft/prado/prop'}

prado_tag_regex = re.compile('<%.*%>')

with open(INPUTPATH) as f:
    lines = f.readlines()
    parser = etree.XMLParser()
    parser.feed('<root')
    for ns, path in namespaces.iteritems():
        parser.feed(' xmlns:%s="%s"' % (ns, path))
    parser.feed('>\n')
    for line in lines:
        line = soupparser.unescape(prado_tag_regex.sub('', line).replace('->', '')).encode('utf-8')
        if not line.startswith('<!'):
            parser.feed(line)
    parser.feed('</root>');
    xml_content = parser.close()

delim_regex = re.compile('(<%\[\s+|\s+]%>)')

tag_open = False
current_token = ''
tokens = {}
for lineno, line in enumerate(lines):
    linelabel = str(lineno+1)
    matches = delim_regex.split(line)
    for match in matches:
        if match.strip() == '<%[':
            if tag_open:
                raise ValueError('Nested tag in line: ' + linelabel)
            tag_open = True
        elif match.strip() == ']%>':
            if not tag_open:
                raise ValueError('Unexpected closing tag in line: ' + linelabel)
            tag_open = False
            if linelabel not in tokens:
                tokens[linelabel] = []
            tokens[linelabel].append(current_token.strip())
            current_token = ''
        else:
            if tag_open:
                current_token += match
    if tag_open:
        current_token += '\n'

for ttranslate in xml_content.xpath('//com:TTranslate', namespaces=namespaces):
    token = ttranslate.text.strip()
    linelabel = str(ttranslate.sourceline-1)
    if linelabel not in tokens:
        tokens[linelabel] = []
    tokens[linelabel].append(token)

if len(tokens):
    OUTPUTDIR = os.path.dirname(OUTPUTPATH)
    if not os.path.exists(OUTPUTDIR):
        try:
            os.makedirs(OUTPUTDIR)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
    with open(OUTPUTPATH, 'w') as f:
        for line, token in tokens.iteritems():
            for string in token:
                f.write('/* %s:%s */\n' % (FILEPATH, line))
                f.write('gettext(%s);\n' % json.dumps(string))
                f.write('\n')