src/pyams_default_theme/component/paragraph/html.py
changeset 529 43de10a58806
parent 457 2c473861fcc2
--- a/src/pyams_default_theme/component/paragraph/html.py	Mon Sep 28 15:50:58 2020 +0200
+++ b/src/pyams_default_theme/component/paragraph/html.py	Fri Nov 13 13:23:00 2020 +0100
@@ -11,18 +11,27 @@
 #
 
 from fanstatic import ConfigurationError
+from persistent import Persistent
+from zope.location import Location
+from zope.schema.fieldproperty import FieldProperty
 
 from pyams_content.component.illustration import IIllustration
+from pyams_content.component.links import InternalReferenceMixin
 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.component.paragraph.interfaces.html import \
+    IHTMLParagraphWithLogosRendererSettings
 from pyams_default_theme.features.renderer import BaseContentRenderer
+from pyams_default_theme.shared.logo import DISABLED_LINK, INTERNAL_FIRST
 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.factory import factory_config
 from pyams_utils.fanstatic import ExternalResource
 from pyams_utils.interfaces.text import IHTMLRenderer
 from pyams_utils.pygments import IPygmentsCodeConfiguration, render_source
+from pyams_utils.url import canonical_url, relative_url
 
 
 __docformat__ = 'restructuredtext'
@@ -139,6 +148,7 @@
     """HTML paragraph default renderer"""
 
     label = _("Default rich text renderer")
+    weight = 10
 
     i18n_context_attrs = ('title', 'body',)
 
@@ -152,3 +162,69 @@
             renderer = self.illustration_renderer = self.illustration.get_renderer(self.request)
             if renderer is not None:
                 renderer.update()
+
+
+#
+# HTML paragraph renderer with logos
+#
+
+HTML_PARAGRAPH_WITH_LOGOS_RENDERER_SETTINGS_KEY = 'pyams_content.html.renderer:text-with-logos'
+
+
+@factory_config(IHTMLParagraphWithLogosRendererSettings)
+class HTMLParagraphWithLogosRendererSettings(Persistent, Location, InternalReferenceMixin):
+    """HTML paragraph with logos renderer settings"""
+
+    reference = FieldProperty(IHTMLParagraphWithLogosRendererSettings['reference'])
+    position = FieldProperty(IHTMLParagraphWithLogosRendererSettings['position'])
+    target_priority = FieldProperty(IHTMLParagraphWithLogosRendererSettings['target_priority'])
+    force_canonical_url = FieldProperty(IHTMLParagraphWithLogosRendererSettings['force_canonical_url'])
+
+
+@adapter_config(context=IHTMLParagraph, provides=IHTMLParagraphWithLogosRendererSettings)
+def html_paragraph_with_logos_renderer_settings_factory(context):
+    """HTML paragraph with logos renderer settings factory"""
+    return get_annotation_adapter(context, HTML_PARAGRAPH_WITH_LOGOS_RENDERER_SETTINGS_KEY,
+                                  IHTMLParagraphWithLogosRendererSettings)
+
+
+@adapter_config(name='text-with-logos',
+                context=(IHTMLParagraph, IPyAMSLayer),
+                provides=IContentRenderer)
+@template_config(template='templates/html-logos.pt', layer=IPyAMSLayer)
+class HTMLParagraphWithLogosRenderer(HTMLParagraphDefaultRenderer):
+    """HTML paragraph renderer with logos"""
+
+    label = _("Rich text renderer with logos")
+    weight = 20
+
+    settings_interface = IHTMLParagraphWithLogosRendererSettings
+
+    def get_internal_url(self, logo):
+        if logo is not None:
+            if self.settings.force_canonical_url:
+                url_getter = canonical_url
+            else:
+                url_getter = relative_url
+            return url_getter(logo, request=self.request)
+        return None
+
+    @staticmethod
+    def get_external_url(logo):
+        return logo.url
+
+    def get_logo_url(self):
+        priority = self.settings.target_priority
+        if priority == DISABLED_LINK:
+            return None
+        logo = self.settings.target
+        if logo is None:
+            return None
+        order = [self.get_external_url, self.get_internal_url]
+        if priority == INTERNAL_FIRST:
+            order = reversed(order)
+        for getter in order:
+            result = getter(logo)
+            if result is not None:
+                return result
+        return None