home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / yahoo / yahooformat.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  9.0 KB  |  181 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import traceback
  5. import re
  6. from util import flatten
  7. import util.primitives.bits as bits
  8.  
  9. color = lambda hex: ('font color="#' + hex + '"', 'font')
  10. codes = {
  11.     1: 'b',
  12.     2: 'i',
  13.     4: 'u',
  14.     'l': '' }
  15. color_codes = {
  16.     30: '000000',
  17.     31: '0000FF',
  18.     32: '008080',
  19.     33: '808080',
  20.     34: '008000',
  21.     35: 'FF0080',
  22.     36: '800080',
  23.     37: 'FF8000',
  24.     38: 'FF0000',
  25.     39: '808000' }
  26. rereplace = [
  27.     ('<font (.+?)>', '<font %s>'),
  28.     ('</font>', '</font>'),
  29.     ('<s>', '<s>'),
  30.     ('</s>', '</s>'),
  31.     ('<alt (?:.+?)>', ''),
  32.     ('</alt>', ''),
  33.     ('<fade (?:.+?)>', ''),
  34.     ('</fade>', '')]
  35.  
  36. def hexstr_to_tuple(hexstr):
  37.     return []([ ord(a) for a in bits.hex2bin(' '.join((lambda .0: for x in .0:
  38. ''.join(x))(zip(hexstr[::2], hexstr[1::2])))) ])
  39.  
  40. color_lookup = dict((lambda .0: for k, v in .0:
  41. (hexstr_to_tuple(v), str(k)))(color_codes.iteritems()))
  42. format_codes = {
  43.     '1': ('b', 'b'),
  44.     '2': ('i', 'i'),
  45.     '4': ('u', 'u'),
  46.     '#': ((lambda hex: 'font color="#%s"' % hex), 'font'),
  47.     'face': ((lambda face: 'font face="%s"' % face), 'font'),
  48.     'size': ((lambda size: 'font style="font-size: %spt"' % size), 'font') }
  49. format_lookup = {
  50.     'b': '1',
  51.     'i': '2',
  52.     'u': '4' }
  53. format_codes.update(dict((lambda .0: for k, v in .0:
  54. (str(k), color(v)))(color_codes.iteritems())))
  55.  
  56. def flatten(s):
  57.     for a in s:
  58.         for e in a:
  59.             yield e
  60.         
  61.     
  62.  
  63.  
  64. def codepair(code, tag):
  65.     if isinstance(tag, tuple):
  66.         (start, end) = tag
  67.     else:
  68.         start = end = tag
  69.     return [
  70.         None,
  71.         ((None, '\x1b[%sm' % code if start else ''), '\x1b[x%sm' % code if end else '')]
  72.  
  73. color_re = re.compile('\x1b\\[#([^m]+)m')
  74. codes.update((lambda .0: for code, color in .0:
  75. ('' + str(code), ('font color="#%s"' % color, 'font')))(color_codes.iteritems()))
  76. replace = list(flatten((lambda .0: for code, tag in .0:
  77. codepair(code, tag))(codes.iteritems())))
  78. rereplace = [ (re.compile(txt, re.IGNORECASE), repl) for txt, repl in rereplace ]
  79. FADE_ALT_RE = '<(?:(?:FADE)|(?:ALT))\\s+(?:(?:#[a-f0-9]{6}),?\\s*)+>'
  80. BIT_DEFENDER_RE = '<font (?:BDYENCID|BDPK|BDCH).*>'
  81. STUPID_COMBINED_RE = '((?:%s)|(?:%s))' % (FADE_ALT_RE, BIT_DEFENDER_RE)
  82. tags_re = re.compile('(</?\\w+(?:(?:\\s+\\w+(?:\\s*=\\s*(?:".*?"|\'.*?\'|[^\'">\\s]+))?)+\\s*|\\s*)/?>)', re.DOTALL | re.IGNORECASE)
  83. entity_re = re.compile('&(?:(?:(?:#(?:(?:x[a-f0-9]+)|(?:[0-9]+)))|[a-z]+));', re.IGNORECASE)
  84. stupid_tags_re = re.compile(STUPID_COMBINED_RE, re.DOTALL | re.MULTILINE | re.IGNORECASE)
  85. stupid_tags = frozenset(('alt', 'fade'))
  86. allowed_tags = frozenset(('b', 'i', 'u', 's', 'font'))
  87.  
  88. def is_ascii(s):
  89.     return all((lambda .0: for c in .0:
  90. if ord(c) <= ord(c):
  91. passelif not ord(c) <= 126:
  92. passord(c) in (9, 10, 13))(s))
  93.  
  94.  
  95. def tohtml(s):
  96.     for txt, repl in replace:
  97.         s = s.replace(txt, repl)
  98.     
  99.     for pattern, repl in rereplace:
  100.         match = pattern.search(s)
  101.         for a in match.groups():
  102.             splat = _[1][a.decode('xml')]
  103.             s = s[:i] + repl % tuple(splat) + s[j:]
  104.             match = pattern.search(s)
  105.             [] if repl == '<font %s>' else []
  106.     
  107.     match = color_re.search(s)
  108.     while match:
  109.         (i, j) = match.span()
  110.         s = s[:i] + '<font color="#%s">' % match.group(1) + s[j:]
  111.         match = color_re.search(s)
  112.     exploded = tags_re.split(s)
  113.     ret = []
  114.     for None in exploded:
  115.         chunk = None
  116.         if tags_re.match(chunk):
  117.             if (any,)((lambda .0: for None in .0:
  118. tag = None(None, any)((lambda .0: for s in .0:
  119. chunk.lower().startswith(s % tag))(('<%s ', '<%s>', '</%s ', '</%s>')))
  120.                 
  121. )(allowed_tags)):
  122.                 ret.append(chunk)
  123.                 continue
  124.             elif (any,)((lambda .0: for None in .0:
  125. tag = None(None, any)((lambda .0: for s in .0:
  126. chunk.lower().startswith(s % tag))(('</%s ', '</%s>')))
  127.                 
  128. )(stupid_tags)):
  129.                 continue
  130.             
  131.         elif stupid_tags_re.match(chunk):
  132.             exp2 = filter((lambda arg: not stupid_tags_re.match(arg)), stupid_tags_re.split(chunk))
  133.             for chunk2 in exp2:
  134.                 ret.append(chunk2.encode('xml'))
  135.             
  136.             continue
  137.         
  138.     
  139.     s = ''.join(ret)
  140.     return s
  141.  
  142.  
  143. def fix_font_size(e):
  144.     return re.sub('size=["\\\']([^"\\\']+)["\\\']', (lambda match: 'style="font-size: %spt"' % match.group(1)), e)
  145.  
  146.  
  147. def color2code(color):
  148.     return ''.join([
  149.         '\x1b[',
  150.         color_lookup.get(color, '#' + ''.join((lambda .0: for c in .0:
  151. '%02x' % c)(color[:3]))),
  152.         'm'])
  153.  
  154.  
  155. def format(format, string):
  156.     before = '<font face="%(face)s" size="%(size)s">' % format
  157.     after = ''
  158.     
  159.     try:
  160.         foregroundcolor = format.get('foregroundcolor')
  161.     except KeyError:
  162.         pass
  163.  
  164.     before += color2code(foregroundcolor)
  165.     for a in ('bold', 'italic', 'underline'):
  166.         if format.get(a, False):
  167.             before += '\x1b[' + format_lookup[a[0]] + 'm'
  168.             continue
  169.     
  170.     return before + string + after
  171.  
  172.  
  173. def main():
  174.     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')
  175.     for t in tests:
  176.         print repr(t)
  177.         print repr(tohtml(t))
  178.         print 
  179.     
  180.  
  181.