Added ReStructuredText and Markdown raw code renderers
authorThierry Florac <tflorac@ulthar.net>
Fri, 14 Dec 2018 23:41:44 +0100
changeset 295 e13fba68f363
parent 294 1f4687ad112e
child 296 a070719aef84
Added ReStructuredText and Markdown raw code renderers
src/pyams_default_theme/component/paragraph/html.py
src/pyams_default_theme/component/paragraph/templates/raw-markdown.pt
src/pyams_default_theme/component/paragraph/templates/raw-rest.pt
--- a/src/pyams_default_theme/component/paragraph/html.py	Fri Dec 14 18:40:02 2018 +0100
+++ b/src/pyams_default_theme/component/paragraph/html.py	Fri Dec 14 23:41:44 2018 +0100
@@ -17,16 +17,16 @@
 from pyams_content.component.illustration import IIllustration
 from pyams_content.component.paragraph.interfaces.html import IHTMLParagraph, IRawParagraph
 from pyams_content.features.renderer.interfaces import IContentRenderer
+from pyams_default_theme import _
 from pyams_default_theme import library
 from pyams_default_theme.features.renderer import BaseContentRenderer
 from pyams_skin.layer import IPyAMSLayer
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config, get_annotation_adapter
 from pyams_utils.fanstatic import ExternalResource
+from pyams_utils.interfaces.text import IHTMLRenderer
 from pyams_utils.pygments import IPygmentsCodeConfiguration, PygmentsCodeRendererSettings, render_source
 
-from pyams_default_theme import _
-
 
 #
 # Raw paragraph default renderer
@@ -85,6 +85,46 @@
         return ''
 
 
+@adapter_config(name='rest', context=(IRawParagraph, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/raw-rest.pt', layer=IPyAMSLayer)
+class RawParagraphRestRenderer(BaseContentRenderer):
+    """Raw paragraph ReStructuredText code renderer"""
+
+    label = _("ReStructuredText renderer")
+    weight = 20
+
+    i18n_context_attrs = ('title', 'body')
+
+    @property
+    def source(self):
+        code = self.body
+        if code:
+            renderer = self.request.registry.queryMultiAdapter((code, self.request), IHTMLRenderer, name='rest')
+            if renderer is not None:
+                return renderer.render()
+        return ''
+
+
+@adapter_config(name='markdown', context=(IRawParagraph, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/raw-markdown.pt', layer=IPyAMSLayer)
+class RawParagraphMarkdownRenderer(BaseContentRenderer):
+    """Raw paragraph markdown code renderer"""
+
+    label = _("Markdown renderer")
+    weight = 30
+
+    i18n_context_attrs = ('title', 'body')
+
+    @property
+    def source(self):
+        code = self.body
+        if code:
+            renderer = self.request.registry.queryMultiAdapter((code, self.request), IHTMLRenderer, name='markdown')
+            if renderer is not None:
+                return renderer.render()
+        return ''
+
+
 #
 # HTML paragraph default renderer
 #
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/component/paragraph/templates/raw-markdown.pt	Fri Dec 14 23:41:44 2018 +0100
@@ -0,0 +1,4 @@
+<h3>${view.title}</h3>
+<tal:var define="settings view.settings">
+	${structure:view.source}
+</tal:var>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/component/paragraph/templates/raw-rest.pt	Fri Dec 14 23:41:44 2018 +0100
@@ -0,0 +1,4 @@
+<h3>${view.title}</h3>
+<tal:var define="settings view.settings">
+	${structure:view.source}
+</tal:var>