I have a python method ( thanks to this snippet ) that accepts some html and wraps tags <a>
around ONLY unformatted links using BeautifulSoup and Django urlize:
from django.utils.html import urlize
from bs4 import BeautifulSoup
def html_urlize(self, text):
soup = BeautifulSoup(text, "html.parser")
print(soup)
textNodes = soup.findAll(text=True)
for textNode in textNodes:
if textNode.parent and getattr(textNode.parent, 'name') == 'a':
continue
urlizedText = urlize(textNode)
textNode.replaceWith(urlizedText)
print(soup)
return str(soup)
Example input text (as the output of the first print statement):
this is a formatted link <a href="http://google.ca">http:
The resulting return text (as the output of the second print statement) is as follows:
this is a formatted link <a href="http://google.ca">http:
As you can see, this is formatting the link, but it does it with escaped html, so when I print it in the template {{ my.html|safe }}
, it does not appear as html.
, , urlize, unescaped, html? , - , ? , django.utils.html.
: , : textNode.replaceWith(urlizedText)
.