home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / gui / imwin / styles / stripformatting.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  4.9 KB  |  154 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. from gui.imwin.emoticons import apply_emoticons
  6. from util import traceguard, linkify, spacify, soupify
  7. from traceback import print_exc
  8. from collections import defaultdict
  9. from re import compile
  10. from logging import getLogger
  11. log = getLogger('stripformatting')
  12. LOG = log.debug
  13. plaintext_transform_functions = [
  14.     ('emoticons', apply_emoticons),
  15.     ('links', linkify),
  16.     ('spaces', spacify)]
  17.  
  18. def strip(html, formatting = True, colors = True, plaintext_transforms = None):
  19.     if plaintext_transforms is None:
  20.         plaintext_transforms = { }
  21.     
  22.     removed = defaultdict(list)
  23.     
  24.     try:
  25.         soup = soupify(html, convertEntities = 'html')
  26.         if formatting:
  27.             strip_formatting(soup, removed)
  28.         
  29.         remove_attrs(soup, [
  30.             'color',
  31.             'bgcolor'], removed, doremove = colors)
  32.         if colors:
  33.             remove_styles(soup, [
  34.                 'background',
  35.                 'color'], removed)
  36.             remove_attrs(soup, [
  37.                 'back'], removed)
  38.         else:
  39.             convert_back(soup, removed)
  40.         remove_tags(soup, 'html')
  41.         remove_tags(soup, 'body')
  42.         apply_plaintext_transforms(soup, plaintext_transforms)
  43.         final = soup.renderContents(None)
  44.         return (final, removed)
  45.     except Exception:
  46.         print_exc()
  47.         return (html, removed)
  48.  
  49.  
  50.  
  51. def strip_formatting(soup, removed):
  52.     remove_attrs(soup, [
  53.         'face',
  54.         'size'], removed)
  55.     remove_tags(soup, 'small', 'big')
  56.     remove_styles(soup, [
  57.         'font-family',
  58.         'font-size'], removed)
  59.     remove_styles(soup, [
  60.         'font'], removed)
  61.  
  62.  
  63. def apply_plaintext_transforms(soup, plaintext_transforms):
  64.     for textElem in soup(text = True):
  65.         s = textElem
  66.         for name, func in plaintext_transform_functions:
  67.             res = plaintext_transforms.get(name, False)
  68.             if res:
  69.                 traceguard.__enter__()
  70.                 
  71.                 try:
  72.                     if res not in (False, True, None):
  73.                         s = func(s, res)
  74.                     else:
  75.                         s = func(s)
  76.                 finally:
  77.                     pass
  78.  
  79.                 continue
  80.             traceguard.__exit__
  81.         
  82.         traceguard.__enter__()
  83.         
  84.         try:
  85.             textElem.replaceWith(s)
  86.         finally:
  87.             pass
  88.  
  89.     
  90.  
  91.  
  92. def attr_match(tag, attrnames):
  93.     tagattrs = set((lambda .0: for attrName, attrValue in .0:
  94. attrName)(tag.attrs))
  95.     return (any,)((lambda .0: for a in .0:
  96. a in tagattrs)(attrnames))
  97.  
  98.  
  99. def remove_attrs(soup, attrs, removed, doremove = True):
  100.     for tag in (soup.findAll,)((lambda t: attr_match(t, attrs))):
  101.         for attrName, attrValue in list(tag.attrs):
  102.             if attrName in attrs:
  103.                 removed[attrName].append(attrValue)
  104.                 if doremove:
  105.                     del tag[attrName]
  106.                 
  107.             doremove
  108.         
  109.     
  110.  
  111.  
  112. def remove_tags(soup, *tags):
  113.     for tag in soup.findAll(name = tags):
  114.         tag.replaceWith(soupify(tag.renderContents(None)))
  115.     
  116.  
  117.  
  118. def remove_style(s, style):
  119.     search = compile('\\s*' + style + ' *:([^;]*)').search
  120.     removed = []
  121.     match = search(s)
  122.     while match:
  123.         removed.append(match.groups(1)[0].strip())
  124.         (i, j) = match.span()
  125.         s = s[:i] + s[j + 1:]
  126.         match = search(s)
  127.     return (s, removed)
  128.  
  129.  
  130. def remove_styles(soup, styles, removed):
  131.     all_removed = []
  132.     for tag in soup.findAll(style = True):
  133.         for style in styles:
  134.             (stripped_style, removed_style) = remove_style(tag['style'], style)
  135.             removed[stripped_style].append(removed_style)
  136.             tag['style'] = stripped_style
  137.         
  138.     
  139.     return all_removed
  140.  
  141.  
  142. def convert_back(soup, removed):
  143.     for tag in soup.findAll(name = 'font', back = True):
  144.         removed['back'].append(tag['back'])
  145.         styles = [
  146.             'background-color: %s' % tag['back']]
  147.         if 'color' in dict(tag.attrs):
  148.             removed['color'].append(tag['color'])
  149.             styles.append('color: %s' % tag['color'])
  150.         
  151.         tag.replaceWith(soupify('<span style="%s">' % '; '.join(styles) + tag.renderContents(None) + '</span>'))
  152.     
  153.  
  154.