README.rst 890 B

123456789101112131415161718192021222324252627282930313233
  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:
  13. >>> from markupsafe import soft_unicode
  14. >>> soft_unicode(42)
  15. u'42'
  16. >>> soft_unicode(Markup('foo'))
  17. Markup(u'foo')
  18. Objects can customize their HTML markup equivalent by overriding
  19. the `__html__` function:
  20. >>> class Foo(object):
  21. ... def __html__(self):
  22. ... return '<strong>Nice</strong>'
  23. ...
  24. >>> escape(Foo())
  25. Markup(u'<strong>Nice</strong>')
  26. >>> Markup(Foo())
  27. Markup(u'<strong>Nice</strong>')