diff options
author | Adam Chainz <me@adamj.eu> | 2016-06-04 05:09:44 +0100 |
---|---|---|
committer | David Lord <davidism@gmail.com> | 2016-06-03 21:09:44 -0700 |
commit | 5fbfcfde2539a00be1b43b51b47190c20a94d212 (patch) | |
tree | 7ed5378144a89a1fb7ca00d9f261f7b543f862ed | |
parent | 9c786053da778906dc34b3c6418f6a486b3140a2 (diff) | |
download | markupsafe-5fbfcfde2539a00be1b43b51b47190c20a94d212.tar.gz |
README - add syntax highlighting (#55)
Add/correct code syntax highlighting to make it a bit easier to read.
-rw-r--r-- | README.rst | 68 |
1 files changed, 40 insertions, 28 deletions
@@ -3,49 +3,57 @@ MarkupSafe Implements a unicode subclass that supports HTML strings: ->>> from markupsafe import Markup, escape ->>> escape("<script>alert(document.cookie);</script>") -Markup(u'<script>alert(document.cookie);</script>') ->>> tmpl = Markup("<em>%s</em>") ->>> tmpl % "Peter > Lustig" -Markup(u'<em>Peter > Lustig</em>') +.. code-block:: python + + >>> from markupsafe import Markup, escape + >>> escape("<script>alert(document.cookie);</script>") + Markup(u'<script>alert(document.cookie);</script>') + >>> tmpl = Markup("<em>%s</em>") + >>> tmpl % "Peter > Lustig" + Markup(u'<em>Peter > Lustig</em>') If you want to make an object unicode that is not yet unicode but don't want to lose the taint information, you can use the -`soft_unicode` function. (On Python 3 you can also use `soft_str` which +``soft_unicode`` function. (On Python 3 you can also use ``soft_str`` which is a different name for the same function). ->>> from markupsafe import soft_unicode ->>> soft_unicode(42) -u'42' ->>> soft_unicode(Markup('foo')) -Markup(u'foo') +.. code-block:: python + + >>> from markupsafe import soft_unicode + >>> soft_unicode(42) + u'42' + >>> soft_unicode(Markup('foo')) + Markup(u'foo') HTML Representations -------------------- Objects can customize their HTML markup equivalent by overriding -the `__html__` function: +the ``__html__`` function: + +.. code-block:: python ->>> class Foo(object): -... def __html__(self): -... return '<strong>Nice</strong>' -... ->>> escape(Foo()) -Markup(u'<strong>Nice</strong>') ->>> Markup(Foo()) -Markup(u'<strong>Nice</strong>') + >>> class Foo(object): + ... def __html__(self): + ... return '<strong>Nice</strong>' + ... + >>> escape(Foo()) + Markup(u'<strong>Nice</strong>') + >>> Markup(Foo()) + Markup(u'<strong>Nice</strong>') Silent Escapes -------------- Since MarkupSafe 0.10 there is now also a separate escape function -called `escape_silent` that returns an empty string for `None` for -consistency with other systems that return empty strings for `None` +called ``escape_silent`` that returns an empty string for ``None`` for +consistency with other systems that return empty strings for ``None`` when escaping (for instance Pylons' webhelpers). If you also want to use this for the escape method of the Markup -object, you can create your own subclass that does that:: +object, you can create your own subclass that does that: + +.. code-block:: python from markupsafe import Markup, escape_silent as escape @@ -71,7 +79,9 @@ following algorithm: 3. otherwise the default format system of Python kicks in and the result is HTML escaped. -Here is how you can implement your own formatting:: +Here is how you can implement your own formatting: + +.. code-block:: python class User(object): @@ -94,8 +104,10 @@ Here is how you can implement your own formatting:: And to format that user: ->>> user = User(1, 'foo') ->>> Markup('<p>User: {0:link}').format(user) -Markup(u'<p>User: <a href="/user/1"><span class=user>foo</span></a>') +.. code-block:: python + + >>> user = User(1, 'foo') + >>> Markup('<p>User: {0:link}').format(user) + Markup(u'<p>User: <a href="/user/1"><span class=user>foo</span></a>') Markupsafe supports Python 2.6, 2.7 and Python 3.3 and higher. |