Advertisement
Guest User

Untitled

a guest
Feb 20th, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. from plone.transformchain.interfaces import ITransform
  2.  
  3. from zope.interface import implements, Interface
  4. from zope.component import adapts
  5.  
  6. from lxml.html import HtmlElement
  7.  
  8.  
  9. class FixLinefeedTransform(object):
  10.     """
  11.       This adapter is implemented to avoid 
 and 
 in transformed
  12.       output.
  13.  
  14.       The problem should be fixed in Plone
  15.    """
  16.  
  17.     implements(ITransform)
  18.     adapts(Interface, Interface)
  19.  
  20.     order = 10000
  21.  
  22.     def __init__(self, published, request):
  23.         self.published = published
  24.         self.request = request
  25.  
  26.     def transformString(self, result, encoding):
  27.         return self.transformIterable([result], encoding)
  28.  
  29.     def transformUnicode(self, result, encoding):
  30.         return self.transformIterable([result], encoding)
  31.  
  32.     def recurse(self, tree):
  33.         # We will only handle elements from lxml.html, I am not sure if
  34.         # the other functions are ever invoked.
  35.  
  36.         if type(tree) != HtmlElement:
  37.             return
  38.  
  39.         children = None
  40.         try:
  41.             children = tree.getchildren()
  42.         except:
  43.             # reached leaf node
  44.             pass
  45.  
  46.         for i in tree.keys():
  47.             node_value = tree.get(i)
  48.             if '\n' in node_value or '\r' in node_value:
  49.                 new_value = node_value.replace('\r', '')
  50.                 new_value = new_value.replace('\n', '')
  51.                 tree.set(i, new_value)
  52.  
  53.         if children:
  54.             for node in children:
  55.                 self.recurse(node)
  56.  
  57.     def transformIterable(self, result, encoding):
  58.         if hasattr(result, 'tree'):
  59.             root = result.tree.getroot()
  60.             self.recurse(root)
  61.  
  62.             result = str(result).replace('
', '\n')
  63.         return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement