Add default renderers for HTML paragraphs
authorThierry Florac <thierry.florac@onf.fr>
Fri, 25 May 2018 08:05:52 +0200
changeset 30 bca2dd39ef7f
parent 29 7f8ae123d371
child 31 566d98116bf2
Add default renderers for HTML paragraphs
src/pyams_default_theme/component/paragraph/__init__.py
src/pyams_default_theme/component/paragraph/html.py
src/pyams_default_theme/component/paragraph/templates/html-default.pt
src/pyams_default_theme/component/paragraph/templates/raw-default.pt
--- a/src/pyams_default_theme/component/paragraph/__init__.py	Fri May 18 08:50:30 2018 +0200
+++ b/src/pyams_default_theme/component/paragraph/__init__.py	Fri May 25 08:05:52 2018 +0200
@@ -12,8 +12,33 @@
 
 __docformat__ = 'restructuredtext'
 
+
 # import standard library
 
 # import interfaces
+from pyams_content.component.paragraph.interfaces import IBaseParagraph
+from pyams_default_theme.layer import IPyAMSDefaultLayer
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
 
 # import packages
+from pyams_default_theme.page import BaseIndexPage
+from pyams_pagelet.pagelet import pagelet_config
+
+
+@pagelet_config(name='preview.html', context=IBaseParagraph, layer=IPyAMSDefaultLayer,
+                permission=VIEW_SYSTEM_PERMISSION)
+class ParagraphPreviewPage(BaseIndexPage):
+    """Paragraph preview page"""
+
+    def __init__(self, context, request):
+        super(ParagraphPreviewPage, self).__init__(context, request)
+        self.language = request.params.get('lang')
+        self.renderer = context.get_renderer(request)
+
+    def update(self):
+        super(ParagraphPreviewPage, self).update()
+        self.renderer.language = self.language
+        self.renderer.update()
+
+    def render(self):
+        return self.renderer.render()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/component/paragraph/html.py	Fri May 25 08:05:52 2018 +0200
@@ -0,0 +1,73 @@
+#
+# Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.illustration import IIllustration
+from pyams_content.component.paragraph.interfaces.html import IRawParagraph, IHTMLParagraph
+from pyams_content.features.renderer.interfaces import IContentRenderer
+from pyams_skin.layer import IPyAMSLayer
+
+# import packages
+from pyams_content.features.renderer.skin import BaseContentRenderer
+from pyams_template.template import template_config
+from pyams_utils.adapter import adapter_config
+
+from pyams_default_theme import _
+
+
+#
+# Raw paragraph default renderer
+#
+
+@adapter_config(name='default', context=(IRawParagraph, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/raw-default.pt', layer=IPyAMSLayer)
+class RawParagraphDefaultRenderer(BaseContentRenderer):
+    """Raw paragraph default renderer"""
+
+    label = _("Default raw HTML renderer")
+
+    i18n_context_attrs = ('title', 'body', )
+
+
+#
+# HTML paragraph default renderer
+#
+
+@adapter_config(name='default', context=(IHTMLParagraph, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/html-default.pt', layer=IPyAMSLayer)
+class HTMLParagraphDefaultRenderer(BaseContentRenderer):
+    """HTML paragraph default renderer"""
+
+    label = _("Default rich text renderer")
+
+    i18n_context_attrs = ('title', 'body', )
+
+    illustration = None
+    illustration_renderer = None
+
+    def update(self):
+        super(HTMLParagraphDefaultRenderer, self).update()
+        self.illustration = IIllustration(self.context)
+        if self.illustration.data:
+            renderer = self.illustration_renderer = self.illustration.get_renderer()
+            if renderer is not None:
+                renderer.update()
+
+    def render_illustration(self):
+        if not self.illustration_renderer:
+            return ''
+        return self.illustration_renderer.render()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/component/paragraph/templates/html-default.pt	Fri May 25 08:05:52 2018 +0200
@@ -0,0 +1,3 @@
+<h3 tal:content="view.title">title</h3>
+<div tal:content="structure view.body">body</div>
+<tal:var content="structure view.render_illustration()">Illustration</tal:var>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/component/paragraph/templates/raw-default.pt	Fri May 25 08:05:52 2018 +0200
@@ -0,0 +1,2 @@
+<h3 tal:content="view.title">title</h3>
+<div tal:content="structure view.body">body</div>