From b7b3ec03937be685219f075283d73295f52b5507 Mon Sep 17 00:00:00 2001 From: Erik Stein Date: Mon, 8 Jan 2018 17:19:08 +0100 Subject: [PATCH] Added html_to_markdown function and template filter. --- setup.py | 1 + shared/markup/markdown_utils.py | 6 ++++++ shared/markup/templatetags/markup_tags.py | 13 +++++++++++++ 3 files changed, 20 insertions(+) diff --git a/setup.py b/setup.py index c111c9e..6e94ad9 100644 --- a/setup.py +++ b/setup.py @@ -37,6 +37,7 @@ setup( # 'django<2', commented out to make `pip install -U` easier 'beautifulsoup4', 'lxml', + 'html2text', ], classifiers=[ 'Development Status :: 4 - Beta', diff --git a/shared/markup/markdown_utils.py b/shared/markup/markdown_utils.py index e0a9156..a0c8aa1 100644 --- a/shared/markup/markdown_utils.py +++ b/shared/markup/markdown_utils.py @@ -5,6 +5,8 @@ from __future__ import unicode_literals import markdown as markdown_module from django.utils.html import strip_tags from django.utils.safestring import mark_safe + +import html2text from shared.utils.text import html_entities_to_unicode @@ -80,3 +82,7 @@ def markdown_to_text(text, **kwargs): """ html = markdown_to_html(text, **kwargs) return strip_tags(html_entities_to_unicode(html)) + + +def html_to_markdown(html): + return html2text.html2text(html) diff --git a/shared/markup/templatetags/markup_tags.py b/shared/markup/templatetags/markup_tags.py index 71bcf2a..465db02 100644 --- a/shared/markup/templatetags/markup_tags.py +++ b/shared/markup/templatetags/markup_tags.py @@ -55,3 +55,16 @@ urlfinder2 = re.compile('\s(http:\/\/\S+)') def urlify_markdown(value): value = urlfinder.sub(r'<\1>', value) return urlfinder2.sub(r' <\1>', value) + + +@register.filter(needs_autoescape=False) +@stringfilter +def html_to_markdown(html, autoescape=None): + """ + Converts a HTML string to markdown, using the html2text tool. + """ + if autoescape: + esc = conditional_escape + else: + esc = lambda x: x + return markdown_utils.html_to_markdown(esc(text))