DESCRIPTION.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. MarkupSafe
  2. ==========
  3. Implements a unicode subclass that supports HTML strings:
  4. >>> from markupsafe import Markup, escape
  5. >>> escape("<script>alert(document.cookie);</script>")
  6. Markup(u'&lt;script&gt;alert(document.cookie);&lt;/script&gt;')
  7. >>> tmpl = Markup("<em>%s</em>")
  8. >>> tmpl % "Peter > Lustig"
  9. Markup(u'<em>Peter &gt; Lustig</em>')
  10. If you want to make an object unicode that is not yet unicode
  11. but don't want to lose the taint information, you can use the
  12. `soft_unicode` function. (On Python 3 you can also use `soft_str` which
  13. is a different name for the same function).
  14. >>> from markupsafe import soft_unicode
  15. >>> soft_unicode(42)
  16. u'42'
  17. >>> soft_unicode(Markup('foo'))
  18. Markup(u'foo')
  19. HTML Representations
  20. --------------------
  21. Objects can customize their HTML markup equivalent by overriding
  22. the `__html__` function:
  23. >>> class Foo(object):
  24. ... def __html__(self):
  25. ... return '<strong>Nice</strong>'
  26. ...
  27. >>> escape(Foo())
  28. Markup(u'<strong>Nice</strong>')
  29. >>> Markup(Foo())
  30. Markup(u'<strong>Nice</strong>')
  31. Silent Escapes
  32. --------------
  33. Since MarkupSafe 0.10 there is now also a separate escape function
  34. called `escape_silent` that returns an empty string for `None` for
  35. consistency with other systems that return empty strings for `None`
  36. when escaping (for instance Pylons' webhelpers).
  37. If you also want to use this for the escape method of the Markup
  38. object, you can create your own subclass that does that::
  39. from markupsafe import Markup, escape_silent as escape
  40. class SilentMarkup(Markup):
  41. __slots__ = ()
  42. @classmethod
  43. def escape(cls, s):
  44. return cls(escape(s))
  45. New-Style String Formatting
  46. ---------------------------
  47. Starting with MarkupSafe 0.21 new style string formats from Python 2.6 and
  48. 3.x are now fully supported. Previously the escape behavior of those
  49. functions was spotty at best. The new implementations operates under the
  50. following algorithm:
  51. 1. if an object has an ``__html_format__`` method it is called as
  52. replacement for ``__format__`` with the format specifier. It either
  53. has to return a string or markup object.
  54. 2. if an object has an ``__html__`` method it is called.
  55. 3. otherwise the default format system of Python kicks in and the result
  56. is HTML escaped.
  57. Here is how you can implement your own formatting::
  58. class User(object):
  59. def __init__(self, id, username):
  60. self.id = id
  61. self.username = username
  62. def __html_format__(self, format_spec):
  63. if format_spec == 'link':
  64. return Markup('<a href="/user/{0}">{1}</a>').format(
  65. self.id,
  66. self.__html__(),
  67. )
  68. elif format_spec:
  69. raise ValueError('Invalid format spec')
  70. return self.__html__()
  71. def __html__(self):
  72. return Markup('<span class=user>{0}</span>').format(self.username)
  73. And to format that user:
  74. >>> user = User(1, 'foo')
  75. >>> Markup('<p>User: {0:link}').format(user)
  76. Markup(u'<p>User: <a href="/user/1"><span class=user>foo</span></a>')