#!/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')