Python markupsafe 模块,Markup() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用markupsafe.Markup()

项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_complex_custom_formatting(self):
        class User(object):
            def __init__(self, id, username):
                self.id = id
                self.username = username
            def __html_format__(self, format_spec):
                if format_spec == 'link':
                    return Markup('<a href="/user/{0}">{1}</a>').format(
                        self.id,
                        self.__html__(),
                    )
                elif format_spec:
                    raise ValueError('Invalid format spec')
                return self.__html__()
            def __html__(self):
                return Markup('<span class=user>{0}</span>').format(self.username)

        user = User(1, 'foo')
        assert Markup('<p>User: {0:link}').format(user) == \
            Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_complex_custom_formatting(self):
        class User(object):
            def __init__(self, id, username):
                self.id = id
                self.username = username
            def __html_format__(self, format_spec):
                if format_spec == 'link':
                    return Markup('<a href="/user/{0}">{1}</a>').format(
                        self.id,
                        self.__html__(),
                    )
                elif format_spec:
                    raise ValueError('Invalid format spec')
                return self.__html__()
            def __html__(self):
                return Markup('<span class=user>{0}</span>').format(self.username)

        user = User(1, 'foo')
        assert Markup('<p>User: {0:link}').format(user) == \
            Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_complex_custom_formatting(self):
        class User(object):
            def __init__(self, id, username):
                self.id = id
                self.username = username
            def __html_format__(self, format_spec):
                if format_spec == 'link':
                    return Markup('<a href="/user/{0}">{1}</a>').format(
                        self.id,
                        self.__html__(),
                    )
                elif format_spec:
                    raise ValueError('Invalid format spec')
                return self.__html__()
            def __html__(self):
                return Markup('<span class=user>{0}</span>').format(self.username)

        user = User(1, 'foo')
        assert Markup('<p>User: {0:link}').format(user) == \
            Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
项目:Texty    作者:sarthfrey    | 项目源码 | 文件源码
def test_complex_custom_formatting(self):
        class User(object):
            def __init__(self, id, username):
                self.id = id
                self.username = username
            def __html_format__(self, format_spec):
                if format_spec == 'link':
                    return Markup('<a href="/user/{0}">{1}</a>').format(
                        self.id,
                        self.__html__(),
                    )
                elif format_spec:
                    raise ValueError('Invalid format spec')
                return self.__html__()
            def __html__(self):
                return Markup('<span class=user>{0}</span>').format(self.username)

        user = User(1, 'foo')
        assert Markup('<p>User: {0:link}').format(user) == \
            Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
项目:tesismometro    作者:joapaspe    | 项目源码 | 文件源码
def test_complex_custom_formatting(self):
        class User(object):
            def __init__(self, id, username):
                self.id = id
                self.username = username
            def __html_format__(self, format_spec):
                if format_spec == 'link':
                    return Markup('<a href="/user/{0}">{1}</a>').format(
                        self.id,
                        self.__html__(),
                    )
                elif format_spec:
                    raise ValueError('Invalid format spec')
                return self.__html__()
            def __html__(self):
                return Markup('<span class=user>{0}</span>').format(self.username)

        user = User(1, 'foo')
        assert Markup('<p>User: {0:link}').format(user) == \
            Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
项目:isni-reconcile    作者:cmh2166    | 项目源码 | 文件源码
def test_complex_custom_formatting(self):
        class User(object):
            def __init__(self, id, username):
                self.id = id
                self.username = username
            def __html_format__(self, format_spec):
                if format_spec == 'link':
                    return Markup('<a href="/user/{0}">{1}</a>').format(
                        self.id,
                        self.__html__(),
                    )
                elif format_spec:
                    raise ValueError('Invalid format spec')
                return self.__html__()
            def __html__(self):
                return Markup('<span class=user>{0}</span>').format(self.username)

        user = User(1, 'foo')
        assert Markup('<p>User: {0:link}').format(user) == \
            Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_complex_custom_formatting(self):
        class User(object):
            def __init__(self, id, username):
                self.id = id
                self.username = username
            def __html_format__(self, format_spec):
                if format_spec == 'link':
                    return Markup('<a href="/user/{0}">{1}</a>').format(
                        self.id,
                        self.__html__(),
                    )
                elif format_spec:
                    raise ValueError('Invalid format spec')
                return self.__html__()
            def __html__(self):
                return Markup('<span class=user>{0}</span>').format(self.username)

        user = User(1, 'foo')
        assert Markup('<p>User: {0:link}').format(user) == \
            Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
项目:RealtimePythonChat    作者:quangtqag    | 项目源码 | 文件源码
def test_complex_custom_formatting(self):
        class User(object):
            def __init__(self, id, username):
                self.id = id
                self.username = username
            def __html_format__(self, format_spec):
                if format_spec == 'link':
                    return Markup('<a href="/user/{0}">{1}</a>').format(
                        self.id,
                        self.__html__(),
                    )
                elif format_spec:
                    raise ValueError('Invalid format spec')
                return self.__html__()
            def __html__(self):
                return Markup('<span class=user>{0}</span>').format(self.username)

        user = User(1, 'foo')
        assert Markup('<p>User: {0:link}').format(user) == \
            Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
项目:chihu    作者:yelongyu    | 项目源码 | 文件源码
def test_complex_custom_formatting(self):
        class User(object):
            def __init__(self, id, username):
                self.id = id
                self.username = username
            def __html_format__(self, format_spec):
                if format_spec == 'link':
                    return Markup('<a href="/user/{0}">{1}</a>').format(
                        self.id,
                        self.__html__(),
                    )
                elif format_spec:
                    raise ValueError('Invalid format spec')
                return self.__html__()
            def __html__(self):
                return Markup('<span class=user>{0}</span>').format(self.username)

        user = User(1, 'foo')
        assert Markup('<p>User: {0:link}').format(user) == \
            Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
项目:tellmeabout.coffee    作者:billyfung    | 项目源码 | 文件源码
def test_complex_custom_formatting(self):
        class User(object):
            def __init__(self, id, username):
                self.id = id
                self.username = username
            def __html_format__(self, format_spec):
                if format_spec == 'link':
                    return Markup('<a href="/user/{0}">{1}</a>').format(
                        self.id,
                        self.__html__(),
                    )
                elif format_spec:
                    raise ValueError('Invalid format spec')
                return self.__html__()
            def __html__(self):
                return Markup('<span class=user>{0}</span>').format(self.username)

        user = User(1, 'foo')
        assert Markup('<p>User: {0:link}').format(user) == \
            Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_adding(self):
        # adding two strings should escape the unsafe one
        unsafe = '<script type="application/x-some-script">alert("foo");</script>'
        safe = Markup('<em>username</em>')
        assert unsafe + safe == text_type(escape(unsafe)) + text_type(safe)
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_string_interpolation(self):
        # string interpolations are safe to use too
        assert Markup('<em>%s</em>') % '<bad user>' == \
               '<em>&lt;bad user&gt;</em>'
        assert Markup('<em>%(username)s</em>') % {
            'username': '<bad user>'
        } == '<em>&lt;bad user&gt;</em>'

        assert Markup('%i') % 3.14 == '3'
        assert Markup('%.2f') % 3.14 == '3.14'
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_type_behavior(self):
        # an escaped object is markup too
        assert type(Markup('foo') + 'bar') is Markup

        # and it implements __html__ by returning itself
        x = Markup("foo")
        assert x.__html__() is x
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_html_interop(self):
        # it also knows how to treat __html__ objects
        class Foo(object):
            def __html__(self):
                return '<em>awesome</em>'
            def __unicode__(self):
                return 'awesome'
            __str__ = __unicode__
        assert Markup(Foo()) == '<em>awesome</em>'
        assert Markup('<strong>%s</strong>') % Foo() == \
            '<strong><em>awesome</em></strong>'
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_tuple_interpol(self):
        self.assertEqual(Markup('<em>%s:%s</em>') % (
            '<foo>',
            '<bar>',
        ), Markup(u'<em>&lt;foo&gt;:&lt;bar&gt;</em>'))
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_escaping(self):
        # escaping and unescaping
        assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'
        assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"
        assert Markup("&lt;test&gt;").unescape() == "<test>"
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_formatting(self):
        for actual, expected in (
            (Markup('%i') % 3.14, '3'),
            (Markup('%.2f') % 3.14159, '3.14'),
            (Markup('%s %s %s') % ('<', 123, '>'), '&lt; 123 &gt;'),
            (Markup('<em>{awesome}</em>').format(awesome='<awesome>'),
             '<em>&lt;awesome&gt;</em>'),
            (Markup('{0[1][bar]}').format([0, {'bar': '<bar/>'}]),
             '&lt;bar/&gt;'),
            (Markup('{0[1][bar]}').format([0, {'bar': Markup('<bar/>')}]),
             '<bar/>')):
            assert actual == expected, "%r should be %r!" % (actual, expected)

    # This is new in 2.7
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_formatting_empty(self):
            formatted = Markup('{}').format(0)
            assert formatted == Markup('0')
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_custom_formatting(self):
        class HasHTMLOnly(object):
            def __html__(self):
                return Markup('<foo>')

        class HasHTMLAndFormat(object):
            def __html__(self):
                return Markup('<foo>')
            def __html_format__(self, spec):
                return Markup('<FORMAT>')

        assert Markup('{0}').format(HasHTMLOnly()) == Markup('<foo>')
        assert Markup('{0}').format(HasHTMLAndFormat()) == Markup('<FORMAT>')
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_splitting(self):
        self.assertEqual(Markup('a b').split(), [
            Markup('a'),
            Markup('b')
        ])
        self.assertEqual(Markup('a b').rsplit(), [
            Markup('a'),
            Markup('b')
        ])
        self.assertEqual(Markup('a\nb').splitlines(), [
            Markup('a'),
            Markup('b')
        ])
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def test_mul(self):
        self.assertEqual(Markup('a') * 3, Markup('aaa'))
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def escape(s):
    """Convert the characters &, <, >, ' and " in string s to HTML-safe
    sequences.  Use this if you need to display text that might contain
    such characters in HTML.  Marks return value as markup string.
    """
    if hasattr(s, '__html__'):
        return s.__html__()
    return Markup(text_type(s)
        .replace('&', '&amp;')
        .replace('>', '&gt;')
        .replace('<', '&lt;')
        .replace("'", '&#39;')
        .replace('"', '&#34;')
    )
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def escape_silent(s):
    """Like :func:`escape` but converts `None` into an empty
    markup string.
    """
    if s is None:
        return Markup()
    return escape(s)
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_adding(self):
        # adding two strings should escape the unsafe one
        unsafe = '<script type="application/x-some-script">alert("foo");</script>'
        safe = Markup('<em>username</em>')
        assert unsafe + safe == text_type(escape(unsafe)) + text_type(safe)
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_string_interpolation(self):
        # string interpolations are safe to use too
        assert Markup('<em>%s</em>') % '<bad user>' == \
               '<em>&lt;bad user&gt;</em>'
        assert Markup('<em>%(username)s</em>') % {
            'username': '<bad user>'
        } == '<em>&lt;bad user&gt;</em>'

        assert Markup('%i') % 3.14 == '3'
        assert Markup('%.2f') % 3.14 == '3.14'
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_type_behavior(self):
        # an escaped object is markup too
        assert type(Markup('foo') + 'bar') is Markup

        # and it implements __html__ by returning itself
        x = Markup("foo")
        assert x.__html__() is x
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_html_interop(self):
        # it also knows how to treat __html__ objects
        class Foo(object):
            def __html__(self):
                return '<em>awesome</em>'
            def __unicode__(self):
                return 'awesome'
            __str__ = __unicode__
        assert Markup(Foo()) == '<em>awesome</em>'
        assert Markup('<strong>%s</strong>') % Foo() == \
            '<strong><em>awesome</em></strong>'
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_tuple_interpol(self):
        self.assertEqual(Markup('<em>%s:%s</em>') % (
            '<foo>',
            '<bar>',
        ), Markup(u'<em>&lt;foo&gt;:&lt;bar&gt;</em>'))
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_escaping(self):
        # escaping and unescaping
        assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'
        assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"
        assert Markup("&lt;test&gt;").unescape() == "<test>"
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_formatting(self):
        for actual, expected in (
            (Markup('%i') % 3.14, '3'),
            (Markup('%.2f') % 3.14159, '3.14'),
            (Markup('%s %s %s') % ('<', 123, '>'), '&lt; 123 &gt;'),
            (Markup('<em>{awesome}</em>').format(awesome='<awesome>'),
             '<em>&lt;awesome&gt;</em>'),
            (Markup('{0[1][bar]}').format([0, {'bar': '<bar/>'}]),
             '&lt;bar/&gt;'),
            (Markup('{0[1][bar]}').format([0, {'bar': Markup('<bar/>')}]),
             '<bar/>')):
            assert actual == expected, "%r should be %r!" % (actual, expected)

    # This is new in 2.7
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_formatting_empty(self):
            formatted = Markup('{}').format(0)
            assert formatted == Markup('0')
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_custom_formatting(self):
        class HasHTMLOnly(object):
            def __html__(self):
                return Markup('<foo>')

        class HasHTMLAndFormat(object):
            def __html__(self):
                return Markup('<foo>')
            def __html_format__(self, spec):
                return Markup('<FORMAT>')

        assert Markup('{0}').format(HasHTMLOnly()) == Markup('<foo>')
        assert Markup('{0}').format(HasHTMLAndFormat()) == Markup('<FORMAT>')
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_splitting(self):
        self.assertEqual(Markup('a b').split(), [
            Markup('a'),
            Markup('b')
        ])
        self.assertEqual(Markup('a b').rsplit(), [
            Markup('a'),
            Markup('b')
        ])
        self.assertEqual(Markup('a\nb').splitlines(), [
            Markup('a'),
            Markup('b')
        ])
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def test_mul(self):
        self.assertEqual(Markup('a') * 3, Markup('aaa'))
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def escape(s):
    """Convert the characters &, <, >, ' and " in string s to HTML-safe
    sequences.  Use this if you need to display text that might contain
    such characters in HTML.  Marks return value as markup string.
    """
    if hasattr(s, '__html__'):
        return s.__html__()
    return Markup(text_type(s)
        .replace('&', '&amp;')
        .replace('>', '&gt;')
        .replace('<', '&lt;')
        .replace("'", '&#39;')
        .replace('"', '&#34;')
    )
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def escape_silent(s):
    """Like :func:`escape` but converts `None` into an empty
    markup string.
    """
    if s is None:
        return Markup()
    return escape(s)
项目:sublime-text-3-packages    作者:nickjj    | 项目源码 | 文件源码
def escape(s):
    """Convert the characters &, <, >, ' and " in string s to HTML-safe
    sequences.  Use this if you need to display text that might contain
    such characters in HTML.  Marks return value as markup string.
    """
    if hasattr(s, '__html__'):
        return s.__html__()
    return Markup(text_type(s)
        .replace('&', '&amp;')
        .replace('>', '&gt;')
        .replace('<', '&lt;')
        .replace("'", '&#39;')
        .replace('"', '&#34;')
    )
项目:sublime-text-3-packages    作者:nickjj    | 项目源码 | 文件源码
def escape_silent(s):
    """Like :func:`escape` but converts `None` into an empty
    markup string.
    """
    if s is None:
        return Markup()
    return escape(s)
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_adding(self):
        # adding two strings should escape the unsafe one
        unsafe = '<script type="application/x-some-script">alert("foo");</script>'
        safe = Markup('<em>username</em>')
        assert unsafe + safe == text_type(escape(unsafe)) + text_type(safe)
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_string_interpolation(self):
        # string interpolations are safe to use too
        assert Markup('<em>%s</em>') % '<bad user>' == \
               '<em>&lt;bad user&gt;</em>'
        assert Markup('<em>%(username)s</em>') % {
            'username': '<bad user>'
        } == '<em>&lt;bad user&gt;</em>'

        assert Markup('%i') % 3.14 == '3'
        assert Markup('%.2f') % 3.14 == '3.14'
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_type_behavior(self):
        # an escaped object is markup too
        assert type(Markup('foo') + 'bar') is Markup

        # and it implements __html__ by returning itself
        x = Markup("foo")
        assert x.__html__() is x
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_html_interop(self):
        # it also knows how to treat __html__ objects
        class Foo(object):
            def __html__(self):
                return '<em>awesome</em>'
            def __unicode__(self):
                return 'awesome'
            __str__ = __unicode__
        assert Markup(Foo()) == '<em>awesome</em>'
        assert Markup('<strong>%s</strong>') % Foo() == \
            '<strong><em>awesome</em></strong>'
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_tuple_interpol(self):
        self.assertEqual(Markup('<em>%s:%s</em>') % (
            '<foo>',
            '<bar>',
        ), Markup(u'<em>&lt;foo&gt;:&lt;bar&gt;</em>'))
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_escaping(self):
        # escaping and unescaping
        assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'
        assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"
        assert Markup("&lt;test&gt;").unescape() == "<test>"
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_formatting(self):
        for actual, expected in (
            (Markup('%i') % 3.14, '3'),
            (Markup('%.2f') % 3.14159, '3.14'),
            (Markup('%s %s %s') % ('<', 123, '>'), '&lt; 123 &gt;'),
            (Markup('<em>{awesome}</em>').format(awesome='<awesome>'),
             '<em>&lt;awesome&gt;</em>'),
            (Markup('{0[1][bar]}').format([0, {'bar': '<bar/>'}]),
             '&lt;bar/&gt;'),
            (Markup('{0[1][bar]}').format([0, {'bar': Markup('<bar/>')}]),
             '<bar/>')):
            assert actual == expected, "%r should be %r!" % (actual, expected)

    # This is new in 2.7
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_formatting_empty(self):
            formatted = Markup('{}').format(0)
            assert formatted == Markup('0')
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_custom_formatting(self):
        class HasHTMLOnly(object):
            def __html__(self):
                return Markup('<foo>')

        class HasHTMLAndFormat(object):
            def __html__(self):
                return Markup('<foo>')
            def __html_format__(self, spec):
                return Markup('<FORMAT>')

        assert Markup('{0}').format(HasHTMLOnly()) == Markup('<foo>')
        assert Markup('{0}').format(HasHTMLAndFormat()) == Markup('<FORMAT>')
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_splitting(self):
        self.assertEqual(Markup('a b').split(), [
            Markup('a'),
            Markup('b')
        ])
        self.assertEqual(Markup('a b').rsplit(), [
            Markup('a'),
            Markup('b')
        ])
        self.assertEqual(Markup('a\nb').splitlines(), [
            Markup('a'),
            Markup('b')
        ])
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def test_mul(self):
        self.assertEqual(Markup('a') * 3, Markup('aaa'))
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def escape(s):
    """Convert the characters &, <, >, ' and " in string s to HTML-safe
    sequences.  Use this if you need to display text that might contain
    such characters in HTML.  Marks return value as markup string.
    """
    if hasattr(s, '__html__'):
        return s.__html__()
    return Markup(text_type(s)
        .replace('&', '&amp;')
        .replace('>', '&gt;')
        .replace('<', '&lt;')
        .replace("'", '&#39;')
        .replace('"', '&#34;')
    )