# HG changeset patch # User Thierry Florac # Date 1543577599 -3600 # Node ID 8a435c645a72dea437e19939aa33da96f0d28b0a # Parent 50b97b22bdd71a6014eade6212abbd7a09ca5bf0 Added formatted source code portlet renderer diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/__init__.py --- 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' diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/interfaces.py --- 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) diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/skin/__init__.py --- 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""" diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/skin/html.pt --- 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 @@ - - ${structure:tales:html(body, 'oid_to_href')} - \ No newline at end of file diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/skin/raw.pt --- 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 @@ - -

${title}

- ${structure:i18n:settings.body} -
diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/skin/templates/code.pt --- /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 @@ + +

${title}

+ ${structure:view.body} +
diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/skin/templates/html.pt --- /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 @@ + + ${structure:tales:html(body, 'oid_to_href')} + \ No newline at end of file diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/skin/templates/raw.pt --- /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 @@ + +

${title}

+ ${structure:i18n:settings.body} +
diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/zmi/__init__.py --- 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""" diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/zmi/html-preview.pt --- 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 @@ -HTML code diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/zmi/raw-preview.pt --- 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 @@ - -
-
-HTML code diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/zmi/templates/html-preview.pt --- /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 @@ +HTML code diff -r 50b97b22bdd7 -r 8a435c645a72 src/pyams_portal/portlets/html/zmi/templates/raw-preview.pt --- /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 @@ + +
+
+
${structure:body}${body}