Browse Source

Added html_to_markdown function and template filter.

master
Erik Stein 7 years ago
parent
commit
b7b3ec0393
  1. 1
      setup.py
  2. 6
      shared/markup/markdown_utils.py
  3. 13
      shared/markup/templatetags/markup_tags.py

1
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',

6
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)

13
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))

Loading…
Cancel
Save