Added Markdown text renderer
authorThierry Florac <tflorac@ulthar.net>
Fri, 14 Dec 2018 23:36:05 +0100
changeset 299 1b1ef6c6e165
parent 298 477f552632af
child 300 f9b5bbd770c8
Added Markdown text renderer
src/pyams_utils/text.py
--- a/src/pyams_utils/text.py	Fri Dec 14 18:32:37 2018 +0100
+++ b/src/pyams_utils/text.py	Fri Dec 14 23:36:05 2018 +0100
@@ -15,6 +15,7 @@
 import html
 
 import docutils.core
+from markdown import markdown
 from pyramid.interfaces import IRequest
 from zope.interface import Interface
 from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
@@ -127,6 +128,18 @@
         return ''.join((parts['body_pre_docinfo'], parts['docinfo'], parts['body']))
 
 
+@adapter_config(name='markdown', context=(str, IRequest), provides=IHTMLRenderer)
+class MarkdownTextRenderer(BaseHTMLRenderer):
+    """Markdown HTML renderer
+
+    This renderer is using *Markdown* HTML output.
+    """
+
+    def render(self, **kwargs):
+        """Renderer Markdown code to HTML"""
+        return markdown(self.context)
+
+
 def text_to_html(text, renderer='text'):
     """Convert text to HTML using the given renderer