diff --git a/tjdests/apps/destinations/templatetags/markdown.py b/tjdests/apps/destinations/templatetags/markdown.py index e3c52be..c6ae66f 100644 --- a/tjdests/apps/destinations/templatetags/markdown.py +++ b/tjdests/apps/destinations/templatetags/markdown.py @@ -2,10 +2,14 @@ from markdown import markdown from django import template +from .strikethrough import StrikethroughExtension + register = template.Library() @register.filter(name="markdown") def convert_markdown(text: str): """Convert text to markdown HTML.""" - return markdown(text, extensions=["extra", "codehilite", "smarty"]) + return markdown( + text, extensions=["extra", "codehilite", "smarty", StrikethroughExtension()] + ) diff --git a/tjdests/apps/destinations/templatetags/sanitize.py b/tjdests/apps/destinations/templatetags/sanitize.py index 6a13e14..e04f277 100644 --- a/tjdests/apps/destinations/templatetags/sanitize.py +++ b/tjdests/apps/destinations/templatetags/sanitize.py @@ -5,7 +5,11 @@ from django import template register = template.Library() -tags = ALLOWED_TAGS + ["h" + str(i) for i in range(1, 7)] + ["div", "p", "pre", "span"] +tags = ( + ALLOWED_TAGS + + ["h" + str(i) for i in range(1, 7)] + + ["div", "p", "pre", "span", "s"] +) attrs = {"*": ["class"]} diff --git a/tjdests/apps/destinations/templatetags/strikethrough.py b/tjdests/apps/destinations/templatetags/strikethrough.py new file mode 100644 index 0000000..ab1df5f --- /dev/null +++ b/tjdests/apps/destinations/templatetags/strikethrough.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +# pylint: skip-file +"""Markdown Strikethrough Extension +Extends the Python-Markdown library to support strikethrough text. +Given the text: + The molecular composition of water is ~~HCl~~. +Will output: +

The molecular composition of water is HCl.

+Based on Markdown Subscript Extension + :website: https://github.com/jambonrose/markdown_subscript_extension + :copyright: Copyright 2014-2018 Andrew Pinkham + :license: Simplified BSD, see LICENSE for details. +This version by Shreyas Mayya. +Pylint was skipped here because python-markdown appears to require +a specific syntax which is at odds with pylint's ruleset. +""" + +from __future__ import unicode_literals + +from markdown import Extension +from markdown.inlinepatterns import SimpleTagPattern + +# match ~~, at least one character that is not ~, and ~~ again +SUBSCRIPT_RE = r"(\~\~)([^(\~)]+)(\~\~)" + + +def makeExtension(*args, **kwargs): # noqa: N802 + """Inform Markdown of the existence of the extension.""" + return StrikethroughExtension(*args, **kwargs) + + +class StrikethroughExtension(Extension): + """Extension: text between ~~ characters will be struck through.""" + + def extendMarkdown(self, md, md_globals): # noqa: N802 + """Insert 's' pattern before 'not_strong' pattern.""" + md.inlinePatterns.add( + "strikethrough", SimpleTagPattern(SUBSCRIPT_RE, "s"), "