From 7c61d3ec3fa5e1cddbd5e1f865a8582ca3528dc6 Mon Sep 17 00:00:00 2001 From: Erik Stein Date: Fri, 24 Aug 2018 13:04:15 +0200 Subject: [PATCH] FootnoteBase as FilesystemTemplateRendererPlugin. --- CHANGES | 3 ++ content_plugins/base.py | 52 +++++++------------ .../templates/plugins/_footnote.html | 7 +++ .../templates/plugins/_richtext.html | 2 + 4 files changed, 30 insertions(+), 34 deletions(-) create mode 100644 content_plugins/templates/plugins/_footnote.html create mode 100644 content_plugins/templates/plugins/_richtext.html diff --git a/CHANGES b/CHANGES index f5bc97a..74ca270 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +0.3.5 2018-08-24 +- FootnoteBase as FilesystemTemplateRendererPlugin. + 0.3.4 2018-08-24 - prepared_richtext, get_prepared_richtext API. diff --git a/content_plugins/base.py b/content_plugins/base.py index 0a9f31b..187ae0d 100644 --- a/content_plugins/base.py +++ b/content_plugins/base.py @@ -1,7 +1,7 @@ import re +from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.db import models -from django.template import Template from django.utils.html import mark_safe, strip_tags from django.utils.text import Truncator from django.utils.translation import ugettext_lazy as _ @@ -124,14 +124,23 @@ class FilesystemTemplateRendererPlugin(TemplateRendererPlugin): ] -class RichTextBase(StyleMixin, FilesystemTemplateRendererPlugin): +class PrepareRichtextMixin: + @property + def prepared_richtext(self): + return mark_safe(self.get_prepared_richtext(self.richtext)) + + def get_prepared_richtext(self, richtext): + return richtext + + +class RichTextBase(PrepareRichtextMixin, StyleMixin, FilesystemTemplateRendererPlugin): if USE_TRANSLATABLE_FIELDS: richtext = TranslatableCleansedRichTextField(_("text"), blank=True) else: richtext = CleansedRichTextField(_("text"), blank=True) admin_inline_baseclass = RichTextInlineBase - template_name = '_richtext.html' + template_name = 'plugins/_richtext.html' class Meta: abstract = True @@ -141,13 +150,6 @@ class RichTextBase(StyleMixin, FilesystemTemplateRendererPlugin): def __str__(self): return Truncator(strip_tags(self.richtext)).words(10, truncate=" ...") - @property - def prepared_richtext(self): - return mark_safe(self.get_prepared_richtext(self.richtext)) - - def get_prepared_richtext(self, richtext): - return richtext - # TODO Rename to SectionBreakBase class SectionBase(StyleMixin, FilesystemTemplateRendererPlugin): @@ -167,6 +169,7 @@ class SectionBase(StyleMixin, FilesystemTemplateRendererPlugin): def __str__(self): return Truncator(strip_tags(self.subheading)).words(10, truncate=" ...") + # FIXME Not need, members are accessible through {{ content.slug }} etc. def get_plugin_context(self, context=None, **kwargs): context = super().get_plugin_context(context=None, **kwargs) context['slug'] = self.slug @@ -223,7 +226,7 @@ class DownloadBase(StyleMixin, StringRendererPlugin): )) -class FootnoteBase(StringRendererPlugin): +class FootnoteBase(PrepareRichtextMixin, FilesystemTemplateRendererPlugin): # TODO Validators: index might only contain alphanumeric characters index = models.CharField(_("footnote index"), max_length=10) if USE_TRANSLATABLE_FIELDS: @@ -231,7 +234,9 @@ class FootnoteBase(StringRendererPlugin): else: richtext = CleansedRichTextField(_("footnote text"), null=True, blank=True) - html_tag = '
  • ' + html_tag = getattr(settings, 'FOOTNOTE_TAG', 'div') + + template_name = 'plugins/_footnote.html' class Meta: abstract = True @@ -244,28 +249,7 @@ class FootnoteBase(StringRendererPlugin): Truncator(strip_tags(self.richtext)).words(10, truncate=" ...") ) - # TODO Convert to Template - def render(self, html_tag=None): - template = """ - {opening_tag} - {number} -
    {text}
    - {closing_tag} - """ - context = { - 'number': self.index, - 'text': mark_safe(self.richtext or ""), - 'opening_tag': "", - 'closing_tag': "", - } - html_tag = html_tag or self.html_tag - if html_tag: - context['opening_tag'] = html_tag - context['closing_tag'] = '{0}/{1}'.format(html_tag[:1], html_tag[1:]) - return mark_safe(template.format(**context)) - - -# FIXME Currently doesn't do anything + class RichTextFootnoteMixin: MATCH_FOOTNOTES = re.compile("(\w+)") diff --git a/content_plugins/templates/plugins/_footnote.html b/content_plugins/templates/plugins/_footnote.html new file mode 100644 index 0000000..7495091 --- /dev/null +++ b/content_plugins/templates/plugins/_footnote.html @@ -0,0 +1,7 @@ + + <{{ content.html_tag }} class="footnote"> + {{ content.index }} +
    + {{ content.prepared_richtext }} +
    + diff --git a/content_plugins/templates/plugins/_richtext.html b/content_plugins/templates/plugins/_richtext.html new file mode 100644 index 0000000..e54b38d --- /dev/null +++ b/content_plugins/templates/plugins/_richtext.html @@ -0,0 +1,2 @@ + +{{ content.prepared_richtext }}