Skip to content

Commit cef9de9

Browse files
committed
Simpler approach
1 parent a3c8def commit cef9de9

File tree

2 files changed

+2
-19
lines changed

2 files changed

+2
-19
lines changed

blog/models.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import re
21
from urllib.parse import urlparse
32

43
from django.conf import settings
@@ -25,27 +24,12 @@
2524
}
2625
BLOG_DOCUTILS_SETTINGS.update(getattr(settings, "BLOG_DOCUTILS_SETTINGS", {}))
2726

28-
# List copied from:
29-
# https://github.com/Python-Markdown/markdown/blob/3.8/markdown/core.py#L112
30-
_MD_ESCAPE_CHARS = "\\`*_{}[]>()#+-.!"
31-
_MD_ESCAPE_REGEX = re.compile(f"[{re.escape(_MD_ESCAPE_CHARS)}]")
32-
3327

3428
def _md_slugify(value, separator):
3529
# matches the `id_prefix` setting of BLOG_DOCUTILS_SETTINGS
3630
return "s" + separator + _md_title_slugify(value, separator)
3731

3832

39-
def _md_escape(s):
40-
# Add a backslash \ before any reserved characters
41-
return _MD_ESCAPE_REGEX.sub(r"\\\g<0>", s)
42-
43-
44-
def _rst_escape(s):
45-
# New lines mess up rst, it's easier to replace them with spaces.
46-
return s.replace("\n", " ")
47-
48-
4933
class EntryQuerySet(models.QuerySet):
5034
def published(self):
5135
return self.active().filter(pub_date__lte=timezone.now())
@@ -89,9 +73,9 @@ def img(self, url, alt_text):
8973
"""
9074
CF = type(self)
9175
return {
92-
CF.REST: f".. image:: {url}\n :alt: {_rst_escape(alt_text)}",
76+
CF.REST: f".. image:: {url}\n :alt: {alt_text}",
9377
CF.HTML: format_html('<img src="{}" alt="{}">', url, alt_text),
94-
CF.MARKDOWN: f"![{_md_escape(alt_text)}]({url})",
78+
CF.MARKDOWN: f"![{alt_text}]({url})",
9579
}[self]
9680

9781

blog/tests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ def test_alt_text_html_escape(self):
326326
(ContentFormat.REST, "test-", '<img src="." alt="test-">'),
327327
(ContentFormat.REST, "test.", '<img src="." alt="test.">'),
328328
(ContentFormat.REST, "test!", '<img src="." alt="test!">'),
329-
(ContentFormat.REST, "te\nst", '<img src="." alt="te st">'),
330329
]
331330
for cf, alt_text, expected in testdata:
332331
# RST doesn't like an empty src, so we use . instead

0 commit comments

Comments
 (0)