html.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import re
  2. import nh3
  3. from django.utils.html import escape
  4. from .constants import HTML_ALLOWED_ATTRIBUTES, HTML_ALLOWED_TAGS
  5. __all__ = (
  6. 'clean_html',
  7. 'foreground_color',
  8. 'highlight',
  9. )
  10. def clean_html(html, schemes):
  11. """
  12. Sanitizes HTML based on a whitelist of allowed tags and attributes.
  13. Also takes a list of allowed URI schemes.
  14. """
  15. return nh3.clean(
  16. html,
  17. tags=HTML_ALLOWED_TAGS,
  18. attributes=HTML_ALLOWED_ATTRIBUTES,
  19. url_schemes=set(schemes)
  20. )
  21. def foreground_color(bg_color, dark='000000', light='ffffff'):
  22. """
  23. Return the ideal foreground color (dark or light) for a given background color in hexadecimal RGB format.
  24. :param dark: RBG color code for dark text
  25. :param light: RBG color code for light text
  26. """
  27. THRESHOLD = 150
  28. bg_color = bg_color.strip('#')
  29. r, g, b = [int(bg_color[c:c + 2], 16) for c in (0, 2, 4)]
  30. if r * 0.299 + g * 0.587 + b * 0.114 > THRESHOLD:
  31. return dark
  32. return light
  33. def highlight(value, highlight, trim_pre=None, trim_post=None, trim_placeholder='...'):
  34. """
  35. Highlight a string within a string and optionally trim the pre/post portions of the original string.
  36. Args:
  37. value: The body of text being searched against
  38. highlight: The string of compiled regex pattern to highlight in `value`
  39. trim_pre: Maximum length of pre-highlight text to include
  40. trim_post: Maximum length of post-highlight text to include
  41. trim_placeholder: String value to swap in for trimmed pre/post text
  42. """
  43. # Split value on highlight string
  44. try:
  45. if type(highlight) is re.Pattern:
  46. pre, match, post = highlight.split(value, maxsplit=1)
  47. else:
  48. highlight = re.escape(highlight)
  49. pre, match, post = re.split(fr'({highlight})', value, maxsplit=1, flags=re.IGNORECASE)
  50. except ValueError:
  51. # Match not found
  52. return escape(value)
  53. # Trim pre/post sections to length
  54. if trim_pre and len(pre) > trim_pre:
  55. pre = trim_placeholder + pre[-trim_pre:]
  56. if trim_post and len(post) > trim_post:
  57. post = post[:trim_post] + trim_placeholder
  58. return f'{escape(pre)}<mark>{escape(match)}</mark>{escape(post)}'