PKG-INFO 4.6 KB

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