Skip to content

Commit

Permalink
Avoid custom code for linebreaks
Browse files Browse the repository at this point in the history
We want newlines to be treated as hard breaks; like StackOverflow and
GitHub flavored Markdown do. Previously this was done with a custom
hack, let's use an officially supported extension to our markdown
renderer instead.

Fixes knatten/cppquiz23#257 and helps with #345
  • Loading branch information
knatten committed Jul 30, 2024
1 parent 2468f5e commit f289f25
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
12 changes: 1 addition & 11 deletions quiz/templatetags/quiz_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,11 @@ def standard_ref(text):
regex = re.compile('§(' + section_name + possible_paragraph + ')')
return re.sub(regex, format_reference, text)


def custom_linebreaks(text):
return (text
.replace("\n", "<br />")
.replace("</p><br />", "</p>")
.replace("<br /><p>", "<p>")
.replace("</pre><br />", "</pre>"))


@register.filter(needs_autoescape=True)
def to_html(text, autoescape=None):
return mark_safe(
standard_ref(
custom_linebreaks(
markdown.markdown(text))))
markdown.markdown(text, extensions=['nl2br'])))


@register.filter()
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Django>=4.1,<5
Markdown
pymdown-extensions
mock
parameterized
tweepy
Expand Down
10 changes: 8 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ idna==3.7
lxml==5.2.2
# via splinter
markdown==3.6
# via -r requirements.in
# via
# -r requirements.in
# pymdown-extensions
mock==5.1.0
# via -r requirements.in
oauthlib==3.2.2
Expand All @@ -42,10 +44,14 @@ pip-tools==7.4.1
# via -r requirements.in
pycodestyle==2.12.0
# via autopep8
pymdown-extensions==10.9
# via -r requirements.in
pyproject-hooks==1.1.0
# via
# build
# pip-tools
pyyaml==6.0.1
# via pymdown-extensions
requests==2.32.3
# via
# requests-oauthlib
Expand All @@ -54,7 +60,7 @@ requests-oauthlib==1.3.1
# via tweepy
splinter[django]==0.21.0
# via -r requirements.in
sqlparse==0.5.0
sqlparse==0.5.1
# via django
tweepy==4.14.0
# via -r requirements.in
Expand Down

0 comments on commit f289f25

Please sign in to comment.