Added formatted source code paragraph renderer
authorThierry Florac <thierry.florac@onf.fr>
Fri, 30 Nov 2018 12:32:27 +0100
changeset 268 d525a81cc2e9
parent 267 cf4776387bfd
child 269 036ea10813a5
Added formatted source code paragraph renderer
src/pyams_default_theme/component/paragraph/html.py
src/pyams_default_theme/component/paragraph/templates/raw-source-code.pt
--- a/src/pyams_default_theme/component/paragraph/html.py	Thu Nov 29 16:35:56 2018 +0100
+++ b/src/pyams_default_theme/component/paragraph/html.py	Fri Nov 30 12:32:27 2018 +0100
@@ -12,13 +12,18 @@
 
 __docformat__ = 'restructuredtext'
 
+from fanstatic import ConfigurationError
+
 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 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
+from pyams_utils.adapter import adapter_config, get_annotation_adapter
+from pyams_utils.fanstatic import ExternalResource
+from pyams_utils.pygments import IPygmentsCodeConfiguration, PygmentsCodeRendererSettings, render_source
 
 from pyams_default_theme import _
 
@@ -33,8 +38,51 @@
     """Raw paragraph default renderer"""
 
     label = _("Default raw HTML renderer")
+    weight = 1
 
-    i18n_context_attrs = ('title', 'body',)
+    i18n_context_attrs = ('title', 'body')
+
+
+#
+# Raw paragraph source code renderer
+#
+
+RAW_PARAGRAPH_SOURCE_CODE_RENDERER_SETTINGS_KEY = 'pyams_content.raw.renderer:source-code'
+
+
+@adapter_config(context=IRawParagraph, provides=IPygmentsCodeConfiguration)
+def raw_paragraph_source_code_renderer_settings_factory(context):
+    """Raw paragraph source code renderer settings factory"""
+    return get_annotation_adapter(context, RAW_PARAGRAPH_SOURCE_CODE_RENDERER_SETTINGS_KEY,
+                                  PygmentsCodeRendererSettings)
+
+
+@adapter_config(name='source-code', context=(IRawParagraph, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/raw-source-code.pt', layer=IPyAMSLayer)
+class RawParagraphSourceCodeRenderer(BaseContentRenderer):
+    """Raw paragraph source code renderer"""
+
+    label = _("Formatted source code renderer")
+    weight = 10
+
+    settings_interface = IPygmentsCodeConfiguration
+
+    i18n_context_attrs = ('title', 'body')
+
+    @property
+    def resources(self):
+        path = 'get-pygments-style.css?style={}'.format(self.settings.style)
+        try:
+            yield ExternalResource(library, path, resource_type='css')
+        except ConfigurationError:
+            yield library.known_resources[path]
+
+    @property
+    def source(self):
+        code = self.body
+        if code:
+            return render_source(code, self.settings)
+        return ''
 
 
 #
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/component/paragraph/templates/raw-source-code.pt	Fri Nov 30 12:32:27 2018 +0100
@@ -0,0 +1,9 @@
+<h3>${view.title}</h3>
+<tal:var define="settings view.settings">
+	<div class="source"
+		 tal:omit-tag="not settings.disable_wrap">
+		<pre tal:omit-tag="not settings.disable_wrap">
+			${structure:view.source}
+		</pre>
+	</div>
+</tal:var>