Added formatted source code portlet renderer
authorThierry Florac <thierry.florac@onf.fr>
Fri, 30 Nov 2018 12:33:19 +0100
changeset 223 8a435c645a72
parent 222 50b97b22bdd7
child 224 27f7e8478a17
Added formatted source code portlet renderer
src/pyams_portal/portlets/html/__init__.py
src/pyams_portal/portlets/html/interfaces.py
src/pyams_portal/portlets/html/skin/__init__.py
src/pyams_portal/portlets/html/skin/html.pt
src/pyams_portal/portlets/html/skin/raw.pt
src/pyams_portal/portlets/html/skin/templates/code.pt
src/pyams_portal/portlets/html/skin/templates/html.pt
src/pyams_portal/portlets/html/skin/templates/raw.pt
src/pyams_portal/portlets/html/zmi/__init__.py
src/pyams_portal/portlets/html/zmi/html-preview.pt
src/pyams_portal/portlets/html/zmi/raw-preview.pt
src/pyams_portal/portlets/html/zmi/templates/html-preview.pt
src/pyams_portal/portlets/html/zmi/templates/raw-preview.pt
--- a/src/pyams_portal/portlets/html/__init__.py	Wed Nov 28 17:52:02 2018 +0100
+++ b/src/pyams_portal/portlets/html/__init__.py	Fri Nov 30 12:33:19 2018 +0100
@@ -49,7 +49,7 @@
     """Raw HTML code portlet"""
 
     name = RAW_PORTLET_NAME
-    label = _("Raw HTML")
+    label = _("Source code")
 
     toolbar_css_class = 'fa fa-fw fa-2x fa-code'
 
--- a/src/pyams_portal/portlets/html/interfaces.py	Wed Nov 28 17:52:02 2018 +0100
+++ b/src/pyams_portal/portlets/html/interfaces.py	Fri Nov 30 12:33:19 2018 +0100
@@ -30,9 +30,9 @@
     title = I18nTextLineField(title=_("Title"),
                               required=False)
 
-    body = I18nTextField(title=_("Raw HTML code"),
-                         description=_("This HTML code will be used 'as is', without any transformation. Use with "
-                                       "care!!"),
+    body = I18nTextField(title=_("Source code"),
+                         description=_("This code will be used 'as is', without any transformation, if using the "
+                                       "'raw' renderer. Use with care!!"),
                          required=False)
 
 
--- a/src/pyams_portal/portlets/html/skin/__init__.py	Wed Nov 28 17:52:02 2018 +0100
+++ b/src/pyams_portal/portlets/html/skin/__init__.py	Fri Nov 30 12:33:19 2018 +0100
@@ -12,33 +12,70 @@
 
 __docformat__ = 'restructuredtext'
 
+from zope.interface import Interface
 
-# import standard library
-
-# import interfaces
+from pyams_i18n.interfaces import II18n
 from pyams_portal.interfaces import IPortalContext, IPortletRenderer
-from pyams_portal.portlets.html.interfaces import IRawPortletSettings, IHTMLPortletSettings
+from pyams_portal.portlet import PortletRenderer
+from pyams_portal.portlets.html.interfaces import IHTMLPortletSettings, IRawPortletSettings
 from pyams_skin.layer import IPyAMSLayer
-
-# import packages
-from pyams_portal.portlet import PortletRenderer
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
-from zope.interface import Interface
+from pyams_utils.fanstatic import ExternalResource
+from pyams_utils.pygments import render_source, IPygmentsCodeConfiguration, library
 
 from pyams_portal import _
 
 
+#
+# Raw code renderer
+#
+
 @adapter_config(context=(IPortalContext, IPyAMSLayer, Interface, IRawPortletSettings), provides=IPortletRenderer)
-@template_config(template='raw.pt', layer=IPyAMSLayer)
+@template_config(template='templates/raw.pt', layer=IPyAMSLayer)
 class RawPortletDefaultRenderer(PortletRenderer):
     """Raw HTML portlet renderer"""
 
     label = _("Default code renderer")
+    weight = 1
 
 
+#
+# Formatted source code renderer
+#
+
+@adapter_config(name='source-code',
+                context=(IPortalContext, IPyAMSLayer, Interface, IRawPortletSettings),
+                provides=IPortletRenderer)
+@template_config(template='templates/code.pt', layer=IPyAMSLayer)
+class RawPortletSourceCodeRenderer(PortletRenderer):
+    """Formatted source code portlet renderer"""
+
+    label = _("Formatted source code renderer")
+    weight = 10
+
+    settings_interface = IPygmentsCodeConfiguration
+
+    @property
+    def resources(self):
+        settings = self.settings
+        yield ExternalResource(library, 'get-pygments-style.css?style={}'.format(settings.style),
+                               resource_type='css')
+
+    @property
+    def body(self):
+        code = II18n(self.settings).query_attribute('body', request=self.request)
+        if code:
+            return render_source(code, self.renderer_settings)
+        return ''
+
+
+#
+# Formatted HTML renderer
+#
+
 @adapter_config(context=(IPortalContext, IPyAMSLayer, Interface, IHTMLPortletSettings), provides=IPortletRenderer)
-@template_config(template='html.pt', layer=IPyAMSLayer)
+@template_config(template='templates/html.pt', layer=IPyAMSLayer)
 class HTMLPortletDefaultRenderer(PortletRenderer):
     """Rich text portlet renderer"""
 
--- a/src/pyams_portal/portlets/html/skin/html.pt	Wed Nov 28 17:52:02 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-<tal:var define="body i18n:view.settings.body">
-	${structure:tales:html(body, 'oid_to_href')}
-</tal:var>
\ No newline at end of file
--- a/src/pyams_portal/portlets/html/skin/raw.pt	Wed Nov 28 17:52:02 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-<tal:var define="settings view.settings">
-	<h3 tal:define="title i18n:settings.title"
-		tal:condition="title">${title}</h3>
-	${structure:i18n:settings.body}
-</tal:var>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlets/html/skin/templates/code.pt	Fri Nov 30 12:33:19 2018 +0100
@@ -0,0 +1,5 @@
+<tal:var define="settings view.settings">
+	<h3 tal:define="title i18n:settings.title"
+		tal:condition="title">${title}</h3>
+	${structure:view.body}
+</tal:var>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlets/html/skin/templates/html.pt	Fri Nov 30 12:33:19 2018 +0100
@@ -0,0 +1,3 @@
+<tal:var define="body i18n:view.settings.body">
+	${structure:tales:html(body, 'oid_to_href')}
+</tal:var>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlets/html/skin/templates/raw.pt	Fri Nov 30 12:33:19 2018 +0100
@@ -0,0 +1,5 @@
+<tal:var define="settings view.settings">
+	<h3 tal:define="title i18n:settings.title"
+		tal:condition="title">${title}</h3>
+	${structure:i18n:settings.body}
+</tal:var>
--- a/src/pyams_portal/portlets/html/zmi/__init__.py	Wed Nov 28 17:52:02 2018 +0100
+++ b/src/pyams_portal/portlets/html/zmi/__init__.py	Fri Nov 30 12:33:19 2018 +0100
@@ -52,7 +52,7 @@
     def updateWidgets(self, prefix=None):
         super(RawPortletSettingsPropertiesEditor, self).updateWidgets(prefix)
         if 'body' in self.widgets:
-            self.widgets['body'].widget_css_class = 'input height-100'
+            self.widgets['body'].widget_css_class = 'monospace input height-200'
 
 
 @adapter_config(name='properties.json', context=(IRawPortletSettings, IPyAMSLayer), provides=IPagelet)
@@ -61,7 +61,7 @@
 
 
 @adapter_config(context=(Interface, IPyAMSLayer, Interface, IRawPortletSettings), provides=IPortletPreviewer)
-@template_config(template='raw-preview.pt', layer=IPyAMSLayer)
+@template_config(template='templates/raw-preview.pt', layer=IPyAMSLayer)
 class RawPortletPreviewer(PortletPreviewer):
     """Raw HTML portlet previewer"""
 
@@ -84,6 +84,6 @@
 
 
 @adapter_config(context=(Interface, IPyAMSLayer, Interface, IHTMLPortletSettings), provides=IPortletPreviewer)
-@template_config(template='html-preview.pt', layer=IPyAMSLayer)
+@template_config(template='templates/html-preview.pt', layer=IPyAMSLayer)
 class HTMLPortletPreviewer(PortletPreviewer):
     """Rich text portlet previewer"""
--- a/src/pyams_portal/portlets/html/zmi/html-preview.pt	Wed Nov 28 17:52:02 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-<tal:var define="settings view.settings"
-		 replace="structure i18n:view.settings.body">HTML code</tal:var>
--- a/src/pyams_portal/portlets/html/zmi/raw-preview.pt	Wed Nov 28 17:52:02 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-<strong tal:define="title i18n:view.settings.title"
-		tal:condition="title">
-	<tal:var content="title" /><br />
-</strong>
-<tal:var replace="structure i18n:view.settings.body">HTML code</tal:var>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlets/html/zmi/templates/html-preview.pt	Fri Nov 30 12:33:19 2018 +0100
@@ -0,0 +1,2 @@
+<tal:var define="settings view.settings"
+		 replace="structure i18n:view.settings.body">HTML code</tal:var>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlets/html/zmi/templates/raw-preview.pt	Fri Nov 30 12:33:19 2018 +0100
@@ -0,0 +1,8 @@
+<strong tal:define="title i18n:view.settings.title"
+		tal:condition="title">
+	<tal:var content="title" /><br />
+</strong>
+<pre tal:define="body i18n:view.settings.body"
+	 tal:switch="body and body.startswith('<') and body.endswith('>')"><tal:if
+	case="True">${structure:body}</tal:if><tal:if
+	case="False">${body}</tal:if></pre>