# HG changeset patch # User Thierry Florac # Date 1543577547 -3600 # Node ID d525a81cc2e990e72395800a3c38e3681fdbb692 # Parent cf4776387bfdd358f24aa925d76b8044ad822b2c Added formatted source code paragraph renderer diff -r cf4776387bfd -r d525a81cc2e9 src/pyams_default_theme/component/paragraph/html.py --- 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 '' # diff -r cf4776387bfd -r d525a81cc2e9 src/pyams_default_theme/component/paragraph/templates/raw-source-code.pt --- /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 @@ +

${view.title}

+ +
+
+			${structure:view.source}
+		
+
+