def urlize_html(html):
"""
Returns urls found in an (X)HTML text node element as urls via Django urlize filter.
"""
try:
from bs4 import BeautifulSoup
except ImportError:
if settings.DEBUG:
raise template.TemplateSyntaxError(
"Error in urlize_html The Python BeautifulSoup libraries aren't installed.")
return html
else:
soup = BeautifulSoup(html, 'html.parser')
text_nodes = soup.find_all(text=True)
for text_node in text_nodes:
urlized_text = urlize(text_node)
text_node.replace_with(BeautifulSoup(urlized_text, 'html.parser'))
return mark_safe(str(soup))
评论列表
文章目录