home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- from __future__ import with_statement
- from gui.imwin.emoticons import apply_emoticons
- from util import traceguard, linkify, spacify, soupify
- from traceback import print_exc
- from collections import defaultdict
- from re import compile
- from logging import getLogger
- log = getLogger('stripformatting')
- LOG = log.debug
- plaintext_transform_functions = [
- ('emoticons', apply_emoticons),
- ('links', linkify),
- ('spaces', spacify)]
-
- def strip(html, formatting = True, colors = True, plaintext_transforms = None):
- removed = defaultdict(list)
-
- try:
- soup = soupify(html)
- if formatting:
- strip_formatting(soup, removed)
-
- remove_attrs(soup, [
- 'color',
- 'bgcolor'], removed, doremove = colors)
- if colors:
- remove_styles(soup, [
- 'color'], removed)
- remove_attrs(soup, [
- 'back'], removed)
- else:
- convert_back(soup, removed)
- remove_tags(soup, 'html')
- remove_tags(soup, 'body')
- apply_plaintext_transforms(soup, plaintext_transforms)
- final = soup.renderContents(None)
- return (final, removed)
- except Exception:
- print_exc()
- return (html, removed)
-
-
-
- def strip_formatting(soup, removed):
- remove_attrs(soup, [
- 'face',
- 'size'], removed)
- remove_tags(soup, [
- 'b',
- 'i',
- 'strong',
- 'em',
- 'small',
- 'big'])
- remove_styles(soup, [
- 'font-family'], removed)
- remove_styles(soup, [
- 'font'], removed)
-
-
- def apply_plaintext_transforms(soup, plaintext_transforms):
- for textElem in soup(text = True):
- s = textElem
- for name, func in plaintext_transform_functions:
- res = plaintext_transforms.get(name, False)
- if res:
- traceguard.__enter__()
-
- try:
- if res not in (False, True, None):
- s = func(s, res)
- else:
- s = func(s)
- finally:
- pass
-
- continue
- traceguard
-
- traceguard.__enter__()
-
- try:
- textElem.replaceWith(s)
- finally:
- pass
-
-
-
-
- def attr_match(tag, attrnames):
- tagattrs = set((lambda .0: for attrName, attrValue in .0:
- attrName)(tag.attrs))
- return (any,)((lambda .0: for a in .0:
- a in tagattrs)(attrnames))
-
-
- def remove_attrs(soup, attrs, removed, doremove = True):
- for tag in (soup.findAll,)((lambda t: attr_match(t, attrs))):
- for attrName, attrValue in list(tag.attrs):
- if attrName in attrs:
- removed[attrName].append(attrValue)
- if doremove:
- del tag[attrName]
-
- doremove
-
-
-
-
- def remove_tags(soup, *tags):
- for tag in soup.findAll(name = tags):
- tag.replaceWith(soupify(tag.renderContents(None)))
-
-
-
- def remove_style(s, style):
- search = compile('\\s*' + style + ' *:([^;]*)').search
- removed = []
- match = search(s)
- while match:
- removed.append(match.groups(1)[0].strip())
- (i, j) = match.span()
- s = s[:i] + s[j + 1:]
- match = search(s)
- return (s, removed)
-
-
- def remove_styles(soup, styles, removed):
- all_removed = []
- for tag in soup.findAll(style = True):
- for style in styles:
- (stripped_style, removed_style) = remove_style(tag['style'], style)
- removed[stripped_style].append(removed_style)
- tag['style'] = stripped_style
-
-
- return all_removed
-
-
- def convert_back(soup, removed):
- for tag in soup.findAll(name = 'font', back = True):
- removed['back'].append(tag['back'])
- styles = [
- 'background-color: %s' % tag['back']]
- if 'color' in dict(tag.attrs):
- removed['color'].append(tag['color'])
- styles.append('color: %s' % tag['color'])
-
- tag.replaceWith(soupify('<span style="%s">' % '; '.join(styles) + tag.renderContents(None) + '</span>'))
-
-
-