summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-06-10 11:58:51 +0200
committeremkael <emkael@tlen.pl>2016-06-10 11:58:51 +0200
commit0833cdcf0ede77df42dd743c9775bd54a4f15193 (patch)
tree5cf0ba707ed99edc16157290b3cf7616d93c37f3
parenteba608682f381be989f343c5ab042d5e2c90967a (diff)
* extracting Prado template localization tags into gettext-compatible format
-rwxr-xr-xbin/tpl2c.py80
1 files changed, 80 insertions, 0 deletions
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('<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')