callbacks.py 752 B

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