Skip to content

Commit c6a5d1d

Browse files
author
Cameron Lamb
authored
Cleanup RichTextBlockWithFootnotes (#2)
* Cleanup RichTextBlockWithFootnotes * Make sure we have a context to work from and add a comment * Remove unnecessary step from readme * Add footnotes as a feature when using RichTextBlockWithFootnotes * Update changelog
1 parent c07f281 commit c6a5d1d

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
- Clean up old step from README - It is no longer recommended to define footnotes in `WAGTAILADMIN_RICH_TEXT_EDITORS`
6+
- Add `footnotes` rich text feature automatically
7+
- Clean up `RichTextBlockWithFootnotes`
8+
59
## 0.6.0
610

711
- Add `WAGTAIL_FOOTNOTES_TEXT_FEATURES` to add custom rich text features to footnote content

README.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ Add footnotes functionality to your Wagtail project.
3333
- Update your `RichTextBlock`s
3434
- Add `"footnotes"` to the `features` arg for each `RichTextBlock` that you want to have this functionality
3535
- You will also need to change any `RichTextBlock`s to `wagtail_footnotes.blocks.RichTextBlockWithFootnotes`
36-
- You can add the footnotes to `RichTextBlock`s across the project by updating `WAGTAILADMIN_RICH_TEXT_EDITORS["default"]["OPTIONS"]["features"]`:
37-
```python
38-
WAGTAILADMIN_RICH_TEXT_EDITORS = {
39-
"default": {
40-
"WIDGET": "wagtail.admin.rich_text.DraftailRichTextArea",
41-
"OPTIONS": {"features": ["bold", "italic", "h3", "h4", "ol", "ul", "link", "footnotes"]},
42-
}
43-
}
44-
```
4536
- Update your page templates to include `{% include "wagtail_footnotes/includes/footnotes.html" %}`
4637
- Make and run migrations:
4738
```

wagtail_footnotes/blocks.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,32 @@ class RichTextBlockWithFootnotes(RichTextBlock):
1616
final template context.
1717
"""
1818

19+
def __init__(self, **kwargs):
20+
super().__init__(**kwargs)
21+
self.features += "footnotes"
22+
1923
def replace_footnote_tags(self, html, context=None):
24+
if context is None:
25+
new_context = self.get_context(value)
26+
else:
27+
new_context = self.get_context(value, parent_context=dict(context))
28+
29+
if "page" not in new_context:
30+
return html
31+
32+
page = new_context["page"]
33+
if not hasattr(page, "footnotes_list"):
34+
page.footnotes_list = []
35+
self.footnotes = {footnote.uuid: footnote for footnote in page.footnotes.all()}
36+
2037
def replace_tag(match):
2138
try:
22-
index = self.process_footnote(match.group(1), context["page"])
39+
index = self.process_footnote(match.group(1), page)
2340
except (KeyError, ValidationError):
2441
return ""
2542
else:
2643
return f'<a href="#footnote-{index}" id="footnote-source-{index}"><sup>[{index}]</sup></a>'
44+
2745
return mark_safe(FIND_FOOTNOTE_TAG.sub(replace_tag, html))
2846

2947
def render(self, value, context=None):
@@ -39,13 +57,8 @@ def render_basic(self, value, context=None):
3957
return self.replace_footnote_tags(html, context=context)
4058

4159
def process_footnote(self, footnote_id, page):
42-
footnotes = self.get_footnotes(page)
43-
footnote = page.footnotes.get(uuid=footnote_id)
44-
if footnote not in footnotes:
45-
footnotes.append(footnote)
46-
return footnotes.index(footnote) + 1
47-
48-
def get_footnotes(self, page):
49-
if not hasattr(page, "footnotes_list"):
50-
page.footnotes_list = []
51-
return page.footnotes_list
60+
footnote = self.footnotes[footnote_id]
61+
if footnote not in page.footnotes_list:
62+
page.footnotes_list.append(footnote)
63+
# Add 1 to the index as footnotes are indexed starting at 1 not 0.
64+
return page.footnotes_list.index(footnote) + 1

0 commit comments

Comments
 (0)