Added "remove_tags" HTML renderer
authorThierry Florac <tflorac@ulthar.net>
Fri, 21 Dec 2018 13:56:34 +0100
changeset 1193 7601e1471970
parent 1192 3f26c34ccaab
child 1194 d4ae54d8fe17
Added "remove_tags" HTML renderer
src/pyams_content/features/html/__init__.py
--- a/src/pyams_content/features/html/__init__.py	Fri Dec 21 10:26:11 2018 +0100
+++ b/src/pyams_content/features/html/__init__.py	Fri Dec 21 13:56:34 2018 +0100
@@ -12,22 +12,18 @@
 
 __docformat__ = 'restructuredtext'
 
+from pyquery import PyQuery
+from pyramid.interfaces import IRequest
 
-# import standard library
-from pyquery import PyQuery
-
-# import interfaces
 from pyams_sequence.interfaces import ISequentialIntIds
+from pyams_sequence.reference import get_reference_target
+from pyams_utils.adapter import ContextRequestAdapter, adapter_config
 from pyams_utils.interfaces.text import IHTMLRenderer
 from pyams_utils.interfaces.url import DISPLAY_CONTEXT
+from pyams_utils.registry import get_utility
+from pyams_utils.text import text_to_html
+from pyams_utils.url import relative_url
 from pyams_workflow.interfaces import IWorkflowPublicationInfo
-from pyramid.interfaces import IRequest
-
-# import packages
-from pyams_sequence.reference import get_reference_target
-from pyams_utils.adapter import adapter_config, ContextRequestAdapter
-from pyams_utils.registry import get_utility
-from pyams_utils.url import relative_url
 
 
 @adapter_config(name='oid_to_href', context=(str, IRequest), provides=IHTMLRenderer)
@@ -36,7 +32,7 @@
 
     def render(self):
         context = self.request.annotations.get(DISPLAY_CONTEXT, self.request.context)
-        html = PyQuery('<div>{0}</div>'.format(self.context))
+        html = PyQuery('<div>{}</div>'.format(self.context))
         sequence = get_utility(ISequentialIntIds)
         for link in html('a[href]'):
             href = link.attrib['href']
@@ -51,3 +47,12 @@
                 # invalid link => remove href!
                 del link.attrib['href']
         return html.html()
+
+
+@adapter_config(name='remove_tags', context=(str, IRequest), provides=IHTMLRenderer)
+class TagsRemoveHTMLRenderer(ContextRequestAdapter):
+    """Remove all tags from HTML code"""
+
+    def render(self):
+        html = PyQuery('<div>{}</div>'.format(self.context))
+        return text_to_html(html.text())