home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- import traceback
- import re
- from util import flatten
- import util.primitives.bits as bits
-
- color = lambda hex: ('font color="#' + hex + '"', 'font')
- codes = {
- 1: 'b',
- 2: 'i',
- 4: 'u',
- 'l': '' }
- color_codes = {
- 30: '000000',
- 31: '0000FF',
- 32: '008080',
- 33: '808080',
- 34: '008000',
- 35: 'FF0080',
- 36: '800080',
- 37: 'FF8000',
- 38: 'FF0000',
- 39: '808000' }
- rereplace = [
- ('<font (.+?)>', '<font %s>'),
- ('</font>', '</font>'),
- ('<s>', '<s>'),
- ('</s>', '</s>'),
- ('<alt (?:.+?)>', ''),
- ('</alt>', ''),
- ('<fade (?:.+?)>', ''),
- ('</fade>', '')]
-
- def hexstr_to_tuple(hexstr):
- return []([ ord(a) for a in bits.hex2bin(' '.join((lambda .0: for x in .0:
- ''.join(x))(zip(hexstr[::2], hexstr[1::2])))) ])
-
- color_lookup = dict((lambda .0: for k, v in .0:
- (hexstr_to_tuple(v), str(k)))(color_codes.iteritems()))
- format_codes = {
- '1': ('b', 'b'),
- '2': ('i', 'i'),
- '4': ('u', 'u'),
- '#': ((lambda hex: 'font color="#%s"' % hex), 'font'),
- 'face': ((lambda face: 'font face="%s"' % face), 'font'),
- 'size': ((lambda size: 'font style="font-size: %spt"' % size), 'font') }
- format_lookup = {
- 'b': '1',
- 'i': '2',
- 'u': '4' }
- format_codes.update(dict((lambda .0: for k, v in .0:
- (str(k), color(v)))(color_codes.iteritems())))
-
- def flatten(s):
- for a in s:
- for e in a:
- yield e
-
-
-
-
- def codepair(code, tag):
- if isinstance(tag, tuple):
- (start, end) = tag
- else:
- start = end = tag
- return [
- None,
- ((None, '\x1b[%sm' % code if start else ''), '\x1b[x%sm' % code if end else '')]
-
- color_re = re.compile('\x1b\\[#([^m]+)m')
- codes.update((lambda .0: for code, color in .0:
- ('' + str(code), ('font color="#%s"' % color, 'font')))(color_codes.iteritems()))
- replace = list(flatten((lambda .0: for code, tag in .0:
- codepair(code, tag))(codes.iteritems())))
- rereplace = [ (re.compile(txt, re.IGNORECASE), repl) for txt, repl in rereplace ]
- FADE_ALT_RE = '<(?:(?:FADE)|(?:ALT))\\s+(?:(?:#[a-f0-9]{6}),?\\s*)+>'
- BIT_DEFENDER_RE = '<font (?:BDYENCID|BDPK|BDCH).*>'
- STUPID_COMBINED_RE = '((?:%s)|(?:%s))' % (FADE_ALT_RE, BIT_DEFENDER_RE)
- tags_re = re.compile('(</?\\w+(?:(?:\\s+\\w+(?:\\s*=\\s*(?:".*?"|\'.*?\'|[^\'">\\s]+))?)+\\s*|\\s*)/?>)', re.DOTALL | re.IGNORECASE)
- entity_re = re.compile('&(?:(?:(?:#(?:(?:x[a-f0-9]+)|(?:[0-9]+)))|[a-z]+));', re.IGNORECASE)
- stupid_tags_re = re.compile(STUPID_COMBINED_RE, re.DOTALL | re.MULTILINE | re.IGNORECASE)
- stupid_tags = frozenset(('alt', 'fade'))
- allowed_tags = frozenset(('b', 'i', 'u', 's', 'font'))
-
- def is_ascii(s):
- return all((lambda .0: for c in .0:
- if ord(c) <= ord(c):
- passelif not ord(c) <= 126:
- passord(c) in (9, 10, 13))(s))
-
-
- def tohtml(s):
- for txt, repl in replace:
- s = s.replace(txt, repl)
-
- for pattern, repl in rereplace:
- match = pattern.search(s)
- for a in match.groups():
- splat = _[1][a.decode('xml')]
- s = s[:i] + repl % tuple(splat) + s[j:]
- match = pattern.search(s)
- [] if repl == '<font %s>' else []
-
- match = color_re.search(s)
- while match:
- (i, j) = match.span()
- s = s[:i] + '<font color="#%s">' % match.group(1) + s[j:]
- match = color_re.search(s)
- exploded = tags_re.split(s)
- ret = []
- for None in exploded:
- chunk = None
- if tags_re.match(chunk):
- if (any,)((lambda .0: for None in .0:
- tag = None(None, any)((lambda .0: for s in .0:
- chunk.lower().startswith(s % tag))(('<%s ', '<%s>', '</%s ', '</%s>')))
-
- )(allowed_tags)):
- ret.append(chunk)
- continue
- elif (any,)((lambda .0: for None in .0:
- tag = None(None, any)((lambda .0: for s in .0:
- chunk.lower().startswith(s % tag))(('</%s ', '</%s>')))
-
- )(stupid_tags)):
- continue
-
- elif stupid_tags_re.match(chunk):
- exp2 = filter((lambda arg: not stupid_tags_re.match(arg)), stupid_tags_re.split(chunk))
- for chunk2 in exp2:
- ret.append(chunk2.encode('xml'))
-
- continue
-
-
- s = ''.join(ret)
- return s
-
-
- def fix_font_size(e):
- return re.sub('size=["\\\']([^"\\\']+)["\\\']', (lambda match: 'style="font-size: %spt"' % match.group(1)), e)
-
-
- def color2code(color):
- return ''.join([
- '\x1b[',
- color_lookup.get(color, '#' + ''.join((lambda .0: for c in .0:
- '%02x' % c)(color[:3]))),
- 'm'])
-
-
- def format(format, string):
- before = '<font face="%(face)s" size="%(size)s">' % format
- after = ''
-
- try:
- foregroundcolor = format.get('foregroundcolor')
- except KeyError:
- pass
-
- before += color2code(foregroundcolor)
- for a in ('bold', 'italic', 'underline'):
- if format.get(a, False):
- before += '\x1b[' + format_lookup[a[0]] + 'm'
- continue
-
- return before + string + after
-
-
- def main():
- tests = '\n\n<font size="10">\x1b[30mhey, who\'s this?\n<font size="14">normal</font>\n\x1b[1m<font size="14">bold</font>\x1b[x1m\n\x1b[1m<font size="14">bold</font>\x1b[x1m<font size="14">notbold</font>\n\x1b[1mbold\x1b[x1m\x1b[2mitalic\x1b[1mbolditalic\x1b[4mall\x1b[x1m\x1b[x2mjustunderline\n<not html>\n\x1b[38mred\n\x1b[#ff3737moffre\n<font face="Chiller" size="15">test</font>\n<font face="Chiller" size=\'15\'>test</font>\n\x1b[#FF0000mhello\x1b[#000000m\n\x1b[1m\x1b[4m\x1b[2m\x1b[#000000m<font face="Arial"><font size="20">\x1b[lmhttp://www.digsby.com\x1b[xlm\x1b[x2m\x1b[x4m\x1b[x1m\n\n'.strip().split('\n')
- for t in tests:
- print repr(t)
- print repr(tohtml(t))
- print
-
-
-