diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml index 1f9008b..903e5f3 100644 --- a/.github/workflows/ruff.yml +++ b/.github/workflows/ruff.yml @@ -15,7 +15,7 @@ jobs: steps: - uses: actions/checkout@v4 - - run: python -Im pip install --user ruff + - run: python -Im pip install --user ruff==0.5.0 - name: Run ruff - run: ruff --output-format=github wagtail_footnotes + run: ruff check --output-format=github wagtail_footnotes diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3480c91..b624374 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,7 +18,7 @@ repos: - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/astral-sh/ruff-pre-commit - rev: 'v0.3.7' + rev: 'v0.5.0' hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/README.md b/README.md index 37c57ba..ed73c83 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,10 @@ WAGTAILADMIN_RICH_TEXT_EDITORS = { - Default: `["bold", "italic", "link"]` - Use this to update a list of Rich Text features allowed in the footnote text. +- `WAGTAIL_FOOTNOTES_REFERENCE_TEMPLATE` + - Default: `"wagtail_footnotes/includes/footnote_reference.html"` + - Use this to set a template that renders footnote references. The template receives the footnote `index` in its context. + ## 🌍 Internationalisation Wagtail Footnotes can be translated. Note that in a multi-lingual setup, the URL setup for footnotes diff --git a/tests/templates/test/endnote_reference.html b/tests/templates/test/endnote_reference.html new file mode 100644 index 0000000..b4b31b9 --- /dev/null +++ b/tests/templates/test/endnote_reference.html @@ -0,0 +1 @@ +{{ index }} diff --git a/tests/test/test_blocks.py b/tests/test/test_blocks.py index ade57f0..85e4492 100644 --- a/tests/test/test_blocks.py +++ b/tests/test/test_blocks.py @@ -1,6 +1,6 @@ import json -from django.test import TestCase +from django.test import TestCase, override_settings from wagtail import blocks from wagtail.fields import StreamBlock from wagtail.models import Page @@ -104,7 +104,7 @@ def test_block_replace_footnote_render_basic(self): context = self.test_page_with_footnote.get_context(self.client.get("/")) out = rtb.render_basic(value, context=context) result = '

This is a paragraph with a footnote. [1]

' - self.assertEqual(out, result) + self.assertHTMLEqual(out, result) def test_block_replace_footnote_render(self): rtb = self.test_page_with_footnote.body.stream_block.child_blocks["paragraph"] @@ -112,4 +112,21 @@ def test_block_replace_footnote_render(self): context = self.test_page_with_footnote.get_context(self.client.get("/")) out = rtb.render(value, context=context) result = '

This is a paragraph with a footnote. [1]

' - self.assertEqual(out, result) + self.assertHTMLEqual(out, result) + + def test_render_footnote_tag(self): + block = RichTextBlockWithFootnotes() + html = block.render_footnote_tag(2) + self.assertHTMLEqual( + html, '[2]' + ) + + @override_settings( + WAGTAIL_FOOTNOTES_REFERENCE_TEMPLATE="test/endnote_reference.html" + ) + def test_render_footnote_tag_new_template(self): + block = RichTextBlockWithFootnotes() + html = block.render_footnote_tag(2) + self.assertHTMLEqual( + html, '2' + ) diff --git a/wagtail_footnotes/blocks.py b/wagtail_footnotes/blocks.py index 1211b9e..315fbc6 100644 --- a/wagtail_footnotes/blocks.py +++ b/wagtail_footnotes/blocks.py @@ -1,6 +1,8 @@ import re +from django.conf import settings from django.core.exceptions import ValidationError +from django.template.loader import get_template from django.utils.safestring import mark_safe from wagtail.blocks import RichTextBlock from wagtail.models import Page @@ -25,6 +27,15 @@ def __init__(self, **kwargs): if "footnotes" not in self.features: self.features.append("footnotes") + def render_footnote_tag(self, index): + template_name = getattr( + settings, + "WAGTAIL_FOOTNOTES_REFERENCE_TEMPLATE", + "wagtail_footnotes/includes/footnote_reference.html", + ) + template = get_template(template_name) + return template.render({"index": index}) + def replace_footnote_tags(self, value, html, context=None): if context is None: new_context = self.get_context(value) @@ -47,7 +58,7 @@ def replace_tag(match): except (KeyError, ValidationError): return "" else: - return f'[{index}]' + return self.render_footnote_tag(index) # note: we return safe html return mark_safe(FIND_FOOTNOTE_TAG.sub(replace_tag, html)) # noqa: S308 diff --git a/wagtail_footnotes/templates/wagtail_footnotes/includes/footnote_reference.html b/wagtail_footnotes/templates/wagtail_footnotes/includes/footnote_reference.html new file mode 100644 index 0000000..86810ae --- /dev/null +++ b/wagtail_footnotes/templates/wagtail_footnotes/includes/footnote_reference.html @@ -0,0 +1 @@ +[{{ index }}]