You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
804 B

4 years ago
  1. """A set of basic callbacks for bleach.linkify."""
  2. from __future__ import unicode_literals
  3. def nofollow(attrs, new=False):
  4. href_key = (None, u'href')
  5. if href_key not in attrs:
  6. return attrs
  7. if attrs[href_key].startswith(u'mailto:'):
  8. return attrs
  9. rel_key = (None, u'rel')
  10. rel_values = [val for val in attrs.get(rel_key, u'').split(u' ') if val]
  11. if u'nofollow' not in [rel_val.lower() for rel_val in rel_values]:
  12. rel_values.append(u'nofollow')
  13. attrs[rel_key] = u' '.join(rel_values)
  14. return attrs
  15. def target_blank(attrs, new=False):
  16. href_key = (None, u'href')
  17. if href_key not in attrs:
  18. return attrs
  19. if attrs[href_key].startswith(u'mailto:'):
  20. return attrs
  21. attrs[(None, u'target')] = u'_blank'
  22. return attrs