home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / gui / imwin / styles / stripformatting.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  4.9 KB  |  155 lines

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