|
| 1 | +from functools import partial |
| 2 | + |
1 | 3 | import bleach |
2 | 4 | from csp.decorators import csp_update |
3 | 5 | from django import forms |
|
17 | 19 | UpdateView, |
18 | 20 | ) |
19 | 21 | from i18nfield.forms import I18nModelForm |
| 22 | +from pretalx.common.templatetags import rich_text |
20 | 23 | from pretalx.common.views.mixins import EventPermissionRequired |
21 | 24 |
|
22 | 25 | from .models import Page |
23 | 26 |
|
| 27 | +ALLOWED_ATTRIBUTES = dict(rich_text.ALLOWED_ATTRIBUTES) |
| 28 | +ALLOWED_ATTRIBUTES["a"] = ["href", "title", "target", "class"] |
| 29 | +ALLOWED_ATTRIBUTES["p"] = ["class"] |
| 30 | +ALLOWED_ATTRIBUTES["li"] = ["class"] |
| 31 | +ALLOWED_ATTRIBUTES["img"] = ["src", "title", "alt", "class"] |
| 32 | +CLEANER = bleach.Cleaner( |
| 33 | + tags=rich_text.ALLOWED_TAGS |
| 34 | + | {"img", "p", "br", "s", "sup", "sub", "u", "h3", "h4", "h5", "h6"}, |
| 35 | + attributes=ALLOWED_ATTRIBUTES, |
| 36 | + protocols=rich_text.ALLOWED_PROTOCOLS | {"data"}, |
| 37 | + filters=[ |
| 38 | + partial( |
| 39 | + bleach.linkifier.LinkifyFilter, |
| 40 | + url_re=rich_text.TLD_REGEX, |
| 41 | + parse_email=True, |
| 42 | + email_re=rich_text.EMAIL_REGEX, |
| 43 | + skip_tags={"pre", "code"}, |
| 44 | + callbacks=bleach.linkifier.DEFAULT_CALLBACKS |
| 45 | + + [rich_text.safelink_callback], |
| 46 | + ) |
| 47 | + ], |
| 48 | +) |
| 49 | + |
24 | 50 |
|
25 | 51 | class PageList(EventPermissionRequired, ListView): |
26 | 52 | model = Page |
@@ -232,27 +258,5 @@ def get_context_data(self, **kwargs): |
232 | 258 | ctx = super().get_context_data() |
233 | 259 | page = self.get_page() |
234 | 260 | ctx["page_title"] = page.title |
235 | | - from pretalx.common.templatetags.rich_text import ( |
236 | | - ALLOWED_ATTRIBUTES, |
237 | | - ALLOWED_PROTOCOLS, |
238 | | - ALLOWED_TAGS, |
239 | | - md, |
240 | | - ) |
241 | | - |
242 | | - attributes = dict(ALLOWED_ATTRIBUTES) |
243 | | - attributes["a"] = ["href", "title", "target", "class"] |
244 | | - attributes["p"] = ["class"] |
245 | | - attributes["li"] = ["class"] |
246 | | - attributes["img"] = ["src", "title", "alt", "class"] |
247 | | - |
248 | | - ctx["content"] = bleach.clean( |
249 | | - md.reset().convert(str(page.text)), |
250 | | - tags=ALLOWED_TAGS |
251 | | - | {"img", "p", "br", "s", "sup", "sub", "u", "h3", "h4", "h5", "h6"}, |
252 | | - attributes=attributes, |
253 | | - protocols=ALLOWED_PROTOCOLS |
254 | | - | { |
255 | | - "data", |
256 | | - }, |
257 | | - ) |
| 261 | + ctx["content"] = CLEANER.clean(rich_text.md.reset().convert(str(page.text))) |
258 | 262 | return ctx |
0 commit comments