From 0833cdcf0ede77df42dd743c9775bd54a4f15193 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 10 Jun 2016 11:58:51 +0200 Subject: * extracting Prado template localization tags into gettext-compatible format --- bin/tpl2c.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 bin/tpl2c.py (limited to 'bin') diff --git a/bin/tpl2c.py b/bin/tpl2c.py new file mode 100755 index 0000000..4fa3eb9 --- /dev/null +++ b/bin/tpl2c.py @@ -0,0 +1,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('\n') + for line in lines: + line = soupparser.unescape(prado_tag_regex.sub('', line).replace('->', '')).encode('utf-8') + if not line.startswith(''); + 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') -- cgit v1.2.3