home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- import re
- from util import flatten, hex2bin
-
- 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 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 '')]
-
- 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 ]
-
- def tohtml(s):
- s = s.encode('xml')
- 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 []
-
- color = re.compile('\x1b\\[#([^m]+)m')
- match = color.search(s)
- while match:
- (i, j) = match.span()
- s = s[:i] + '<font color="#%s">' % match.group(1) + s[j:]
- match = color.search(s)
- for tag in ('font', 'b', 'i', 'u', 's'):
- s = s + ''.join([
- '</%s>' % tag] * (s.count('<%s' % tag) - s.count('</%s' % tag)))
-
- return s
-
-
- 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
-
-
-