~brad-marshall/charms/trusty/apache2-wsgi/fix-haproxy-relations

« back to all changes in this revision

Viewing changes to hooks/lib/markupsafe/_native.py

  • Committer: Robin Winslow
  • Date: 2014-05-27 14:00:44 UTC
  • Revision ID: robin.winslow@canonical.com-20140527140044-8rpmb3wx4djzwa83
Add all files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""
 
3
    markupsafe._native
 
4
    ~~~~~~~~~~~~~~~~~~
 
5
 
 
6
    Native Python implementation the C module is not compiled.
 
7
 
 
8
    :copyright: (c) 2010 by Armin Ronacher.
 
9
    :license: BSD, see LICENSE for more details.
 
10
"""
 
11
from markupsafe import Markup
 
12
from markupsafe._compat import text_type
 
13
 
 
14
 
 
15
def escape(s):
 
16
    """Convert the characters &, <, >, ' and " in string s to HTML-safe
 
17
    sequences.  Use this if you need to display text that might contain
 
18
    such characters in HTML.  Marks return value as markup string.
 
19
    """
 
20
    if hasattr(s, '__html__'):
 
21
        return s.__html__()
 
22
    return Markup(text_type(s)
 
23
        .replace('&', '&amp;')
 
24
        .replace('>', '&gt;')
 
25
        .replace('<', '&lt;')
 
26
        .replace("'", '&#39;')
 
27
        .replace('"', '&#34;')
 
28
    )
 
29
 
 
30
 
 
31
def escape_silent(s):
 
32
    """Like :func:`escape` but converts `None` into an empty
 
33
    markup string.
 
34
    """
 
35
    if s is None:
 
36
        return Markup()
 
37
    return escape(s)
 
38
 
 
39
 
 
40
def soft_unicode(s):
 
41
    """Make a string unicode if it isn't already.  That way a markup
 
42
    string is not converted back to unicode.
 
43
    """
 
44
    if not isinstance(s, text_type):
 
45
        s = text_type(s)
 
46
    return s