DESCRIPTION.rst 3.4 KB

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