# Adapted to produce DICT-compatible files by Petr Rockai in 2012
# Based on code from wiktiondict by Greg Hewgill
import re
import sys
import codecs
import os
import textwrap
import time
import xml.sax
class Text:
def __init__(self, s):
self.s = s
def process(self):
return s
class TemplateCall:
def __init__(self):
pass
def process(self):
pass
class Template:
def __init__(self):
self.parts = []
def append(self, part):
self.parts.append(part)
def process(self):
return ''.join(x.process() for x in self.parts)
class Whitespace:
def __init__(self, s):
self.s = s
class OpenDouble: pass
class OpenTriple: pass
class CloseDouble: pass
class CloseTriple: pass
class Equals:
def __str__(self):
return "="
class Delimiter:
def __init__(self, c):
self.c = c
def __str__(self):
return self.c
def Tokenise(s):
s = unicode(s)
stack = []
last = 0
i = 0
while i < len(s):
if s[i] == '{' and i+1 < len(s) and s[i+1] == '{':
if i > last:
yield s[last:i]
if i+2 < len(s) and s[i+2] == '{':
yield OpenTriple()
stack.append(3)
i += 3
else:
yield OpenDouble()
stack.append(2)
i += 2
last = i
elif s[i] == '}' and i+1 < len(s) and s[i+1] == '}':
if i > last:
yield s[last:i]
if len(stack) == 0:
yield "}}"
i += 2
elif stack[-1] == 2:
yield CloseDouble()
i += 2
stack.pop()
elif i+2 < len(s) and s[i+2] == '}':
yield CloseTriple()
i += 3
stack.pop()
else:
raise SyntaxError()
last = i
elif s[i] == ':' or s[i] == '|':
if i > last:
yield s[last:i]
yield Delimiter(s[i])
i += 1
last = i
elif s[i] == '=':
if i > last:
yield s[last:i]
yield Equals()
i += 1
last = i
#elif s[i] == ' ' or s[i] == '\t' or s[i] == '\n':
# if i > last:
# yield s[last:i]
# last = i
# m = re.match(r"\s+", s[i:])
# assert m
# yield Whitespace(m.group(0))
# i += len(m.group(0))
# last = i
else:
i += 1
if i > last:
yield s[last:i]
def processSub(templates, tokens, args):
t = tokens.next()
if not isinstance(t, unicode):
raise SyntaxError
name = t
t = tokens.next()
default = None
if isinstance(t, Delimiter) and t.c == '|':
default = ""
while True:
t = tokens.next()
if isinstance(t, unicode):
default += t
elif isinstance(t, OpenDouble):
default += processTemplateCall(templates, tokens, args)
elif isinstance(t, OpenTriple):
default += processSub(templates, tokens, args)
elif isinstance(t, CloseTriple):
break
else:
print "Unexpected:", t
raise SyntaxError()
if name in args:
return args[name]
if default is not None:
return default
if name == "lang":
return "en"
return "{{{%s}}}" % name
def processTemplateCall(templates, tokens, args):
template = tokens.next().strip().lower()
args = {}
a = 1
t = tokens.next()
while True:
if isinstance(t, Delimiter):
name = unicode(a)
arg = ""
while True:
t = tokens.next()
if isinstance(t, unicode):
arg += t
elif isinstance(t, OpenDouble):
arg += processTemplateCall(templates, tokens, args)
elif isinstance(t, OpenTriple):
arg += processSub(templates, tokens, args)
elif isinstance(t, Delimiter) and t.c != '|':
arg += str(t)
else:
break
if isinstance(t, Equals):
name = arg.strip()
arg = ""
while True:
t = tokens.next()
if isinstance(t, (unicode, Equals)):
arg += unicode(t)
elif isinstance(t, OpenDouble):
arg += processTemplateCall(templates, tokens, args)
elif isinstance(t, OpenTriple):
arg += processSub(templates, tokens, args)
elif isinstance(t, Delimiter) and t.c != '|':
arg += str(t)
else:
break
arg = arg.strip()
else:
a += 1
args[name] = arg
elif isinstance(t, CloseDouble):
break
else:
print "Unexpected:", t
raise SyntaxError
#print template, args
if template[0] == '#':
if template == "#if":
if args['1'].strip():
return args['2']
elif '3' in args:
return args['3']