METADATA 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. Metadata-Version: 2.0
  2. Name: MarkupSafe
  3. Version: 0.23
  4. Summary: Implements a XML/HTML/XHTML Markup safe string for Python
  5. Home-page: http://github.com/mitsuhiko/markupsafe
  6. Author: Armin Ronacher
  7. Author-email: armin.ronacher@active-4.com
  8. License: BSD
  9. Platform: UNKNOWN
  10. Classifier: Development Status :: 5 - Production/Stable
  11. Classifier: Environment :: Web Environment
  12. Classifier: Intended Audience :: Developers
  13. Classifier: License :: OSI Approved :: BSD License
  14. Classifier: Operating System :: OS Independent
  15. Classifier: Programming Language :: Python
  16. Classifier: Programming Language :: Python :: 3
  17. Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
  18. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  19. Classifier: Topic :: Text Processing :: Markup :: HTML
  20. MarkupSafe
  21. ==========
  22. Implements a unicode subclass that supports HTML strings:
  23. >>> from markupsafe import Markup, escape
  24. >>> escape("<script>alert(document.cookie);</script>")
  25. Markup(u'&lt;script&gt;alert(document.cookie);&lt;/script&gt;')
  26. >>> tmpl = Markup("<em>%s</em>")
  27. >>> tmpl % "Peter > Lustig"
  28. Markup(u'<em>Peter &gt; Lustig</em>')
  29. If you want to make an object unicode that is not yet unicode
  30. but don't want to lose the taint information, you can use the
  31. `soft_unicode` function. (On Python 3 you can also use `soft_str` which
  32. is a different name for the same function).
  33. >>> from markupsafe import soft_unicode
  34. >>> soft_unicode(42)
  35. u'42'
  36. >>> soft_unicode(Markup('foo'))
  37. Markup(u'foo')
  38. HTML Representations
  39. --------------------
  40. Objects can customize their HTML markup equivalent by overriding
  41. the `__html__` function:
  42. >>> class Foo(object):
  43. ... def __html__(self):
  44. ... return '<strong>Nice</strong>'
  45. ...
  46. >>> escape(Foo())
  47. Markup(u'<strong>Nice</strong>')
  48. >>> Markup(Foo())
  49. Markup(u'<strong>Nice</strong>')
  50. Silent Escapes
  51. --------------
  52. Since MarkupSafe 0.10 there is now also a separate escape function
  53. called `escape_silent` that returns an empty string for `None` for
  54. consistency with other systems that return empty strings for `None`
  55. when escaping (for instance Pylons' webhelpers).
  56. If you also want to use this for the escape method of the Markup
  57. object, you can create your own subclass that does that::
  58. from markupsafe import Markup, escape_silent as escape
  59. class SilentMarkup(Markup):
  60. __slots__ = ()
  61. @classmethod
  62. def escape(cls, s):
  63. return cls(escape(s))
  64. New-Style String Formatting
  65. ---------------------------
  66. Starting with MarkupSafe 0.21 new style string formats from Python 2.6 and
  67. 3.x are now fully supported. Previously the escape behavior of those
  68. functions was spotty at best. The new implementations operates under the
  69. following algorithm:
  70. 1. if an object has an ``__html_format__`` method it is called as
  71. replacement for ``__format__`` with the format specifier. It either
  72. has to return a string or markup object.
  73. 2. if an object has an ``__html__`` method it is called.
  74. 3. otherwise the default format system of Python kicks in and the result
  75. is HTML escaped.
  76. Here is how you can implement your own formatting::
  77. class User(object):
  78. def __init__(self, id, username):
  79. self.id = id
  80. self.username = username
  81. def __html_format__(self, format_spec):
  82. if format_spec == 'link':
  83. return Markup('<a href="/user/{0}">{1}</a>').format(
  84. self.id,
  85. self.__html__(),
  86. )
  87. elif format_spec:
  88. raise ValueError('Invalid format spec')
  89. return self.__html__()
  90. def __html__(self):
  91. return Markup('<span class=user>{0}</span>').format(self.username)
  92. And to format that user:
  93. >>> user = User(1, 'foo')
  94. >>> Markup('<p>User: {0:link}').format(user)
  95. Markup(u'<p>User: <a href="/user/1"><span class=user>foo</span></a>')