__init__.py 1016 B

12345678910111213141516171819202122232425262728293031
  1. from __future__ import absolute_import
  2. from future.utils import PY3
  3. __future_module__ = True
  4. if PY3:
  5. from html import *
  6. else:
  7. # cgi.escape isn't good enough for the single Py3.3 html test to pass.
  8. # Define it inline here instead. From the Py3.4 stdlib. Note that the
  9. # html.escape() function from the Py3.3 stdlib is not suitable for use on
  10. # Py2.x.
  11. """
  12. General functions for HTML manipulation.
  13. """
  14. def escape(s, quote=True):
  15. """
  16. Replace special characters "&", "<" and ">" to HTML-safe sequences.
  17. If the optional flag quote is true (the default), the quotation mark
  18. characters, both double quote (") and single quote (') characters are also
  19. translated.
  20. """
  21. s = s.replace("&", "&amp;") # Must be done first!
  22. s = s.replace("<", "&lt;")
  23. s = s.replace(">", "&gt;")
  24. if quote:
  25. s = s.replace('"', "&quot;")
  26. s = s.replace('\'', "&#x27;")
  27. return s
  28. __all__ = ['escape']