callbacks.py 792 B

123456789101112131415161718192021222324252627282930313233
  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, 'href')
  5. if href_key not in attrs:
  6. return attrs
  7. if attrs[href_key].startswith('mailto:'):
  8. return attrs
  9. rel_key = (None, 'rel')
  10. rel_values = [val for val in attrs.get(rel_key, '').split(' ') if val]
  11. if 'nofollow' not in [rel_val.lower() for rel_val in rel_values]:
  12. rel_values.append('nofollow')
  13. attrs[rel_key] = ' '.join(rel_values)
  14. return attrs
  15. def target_blank(attrs, new=False):
  16. href_key = (None, 'href')
  17. if href_key not in attrs:
  18. return attrs
  19. if attrs[href_key].startswith('mailto:'):
  20. return attrs
  21. attrs[(None, 'target')] = '_blank'
  22. return attrs