I recently implemented adding target="_blank"
to external links like this:
@hooks.register('after_edit_page')
def do_after_page_edit(request, page):
if hasattr(page, "body"):
soup = BeautifulSoup(page.body)
for a in soup.findAll('a'):
if hasattr(a, "href"):
a["target"] = "_blank"
page.body = str(soup)
page.body = page.body.replace("<html><head></head><body>", "")
page.body = page.body.replace("</body></html>", "")
page.body = page.body.replace("></embed>", "/>")
page.save()
@hooks.register('construct_whitelister_element_rules')
def whitelister_element_rules():
return {
'a': attribute_rule({'href': check_url, 'target': True}),
}
Problems:
Beautiful soup messes with the output, adding
html, head & body
tags - Don't put html, head and body tags automatically, beautifulsoupIt also messes with the embed tags - How to get BeautifulSoup 4 to respect a self-closing tag?
Hence my crappy "
fix
" manually replacing parts of the output with blank strings.
Question:
What is the correct and best way to do this?