METADATA 4.2 KB

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