當前位置: 首頁>>代碼示例>>Python>>正文


Python markupsafe.escape方法代碼示例

本文整理匯總了Python中markupsafe.escape方法的典型用法代碼示例。如果您正苦於以下問題:Python markupsafe.escape方法的具體用法?Python markupsafe.escape怎麽用?Python markupsafe.escape使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在markupsafe的用法示例。


在下文中一共展示了markupsafe.escape方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: import_string

# 需要導入模塊: import markupsafe [as 別名]
# 或者: from markupsafe import escape [as 別名]
def import_string(import_name, silent=False):
    """Imports an object based on a string.  This is useful if you want to
    use import paths as endpoints or something similar.  An import path can
    be specified either in dotted notation (``xml.sax.saxutils.escape``)
    or with a colon as object delimiter (``xml.sax.saxutils:escape``).

    If the `silent` is True the return value will be `None` if the import
    fails.

    :return: imported object
    """
    try:
        if ':' in import_name:
            module, obj = import_name.split(':', 1)
        elif '.' in import_name:
            items = import_name.split('.')
            module = '.'.join(items[:-1])
            obj = items[-1]
        else:
            return __import__(import_name)
        return getattr(__import__(module, None, None, [obj]), obj)
    except (ImportError, AttributeError):
        if not silent:
            raise 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:26,代碼來源:utils.py

示例2: htmlentityreplace_errors

# 需要導入模塊: import markupsafe [as 別名]
# 或者: from markupsafe import escape [as 別名]
def htmlentityreplace_errors(ex):
    """An encoding error handler.

    This python codecs error handler replaces unencodable
    characters with HTML entities, or, if no HTML entity exists for
    the character, XML character references::

        >>> u'The cost was \u20ac12.'.encode('latin1', 'htmlentityreplace')
        'The cost was €12.'
    """
    if isinstance(ex, UnicodeEncodeError):
        # Handle encoding errors
        bad_text = ex.object[ex.start : ex.end]
        text = _html_entities_escaper.escape(bad_text)
        return (compat.text_type(text), ex.end)
    raise ex 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:18,代碼來源:filters.py

示例3: escape

# 需要導入模塊: import markupsafe [as 別名]
# 或者: from markupsafe import escape [as 別名]
def escape(self, text):
        """Replace characters with their character references.

        Replace characters by their named entity references.
        Non-ASCII characters, if they do not have a named entity reference,
        are replaced by numerical character references.

        The return value is guaranteed to be ASCII.
        """
        return self.__escapable.sub(
            self.__escape, compat.text_type(text)
        ).encode("ascii")

    # XXX: This regexp will not match all valid XML entity names__.
    # (It punts on details involving involving CombiningChars and Extenders.)
    #
    # .. __: http://www.w3.org/TR/2000/REC-xml-20001006#NT-EntityRef 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:19,代碼來源:filters.py

示例4: escape

# 需要導入模塊: import markupsafe [as 別名]
# 或者: from markupsafe import escape [as 別名]
def escape(self, text):
        """Replace characters with their character references.

        Replace characters by their named entity references.
        Non-ASCII characters, if they do not have a named entity reference,
        are replaced by numerical character references.

        The return value is guaranteed to be ASCII.
        """
        return self.__escapable.sub(self.__escape, compat.text_type(text)
                                    ).encode('ascii')

    # XXX: This regexp will not match all valid XML entity names__.
    # (It punts on details involving involving CombiningChars and Extenders.)
    #
    # .. __: http://www.w3.org/TR/2000/REC-xml-20001006#NT-EntityRef 
開發者ID:jpush,項目名稱:jbox,代碼行數:18,代碼來源:filters.py

示例5: htmlentityreplace_errors

# 需要導入模塊: import markupsafe [as 別名]
# 或者: from markupsafe import escape [as 別名]
def htmlentityreplace_errors(ex):
    """An encoding error handler.

    This python `codecs`_ error handler replaces unencodable
    characters with HTML entities, or, if no HTML entity exists for
    the character, XML character references.

    >>> u'The cost was \u20ac12.'.encode('latin1', 'htmlentityreplace')
    'The cost was €12.'
    """
    if isinstance(ex, UnicodeEncodeError):
        # Handle encoding errors
        bad_text = ex.object[ex.start:ex.end]
        text = _html_entities_escaper.escape(bad_text)
        return (compat.text_type(text), ex.end)
    raise ex 
開發者ID:tp4a,項目名稱:teleport,代碼行數:18,代碼來源:filters.py

示例6: _output_child_to_const

# 需要導入模塊: import markupsafe [as 別名]
# 或者: from markupsafe import escape [as 別名]
def _output_child_to_const(self, node, frame, finalize):
        """Try to optimize a child of an ``Output`` node by trying to
        convert it to constant, finalized data at compile time.

        If :exc:`Impossible` is raised, the node is not constant and
        will be evaluated at runtime. Any other exception will also be
        evaluated at runtime for easier debugging.
        """
        const = node.as_const(frame.eval_ctx)

        if frame.eval_ctx.autoescape:
            const = escape(const)

        # Template data doesn't go through finalize.
        if isinstance(node, nodes.TemplateData):
            return text_type(const)

        return finalize.const(const) 
開發者ID:pypa,項目名稱:pipenv,代碼行數:20,代碼來源:compiler.py

示例7: htmlsafe_json_dumps

# 需要導入模塊: import markupsafe [as 別名]
# 或者: from markupsafe import escape [as 別名]
def htmlsafe_json_dumps(obj, dumper=None, **kwargs):
    """Works exactly like :func:`dumps` but is safe for use in ``<script>``
    tags.  It accepts the same arguments and returns a JSON string.  Note that
    this is available in templates through the ``|tojson`` filter which will
    also mark the result as safe.  Due to how this function escapes certain
    characters this is safe even if used outside of ``<script>`` tags.

    The following characters are escaped in strings:

    -   ``<``
    -   ``>``
    -   ``&``
    -   ``'``

    This makes it safe to embed such strings in any place in HTML with the
    notable exception of double quoted attributes.  In that case single
    quote your attributes or HTML escape it in addition.
    """
    if dumper is None:
        dumper = json.dumps
    rv = dumper(obj, **kwargs) \
        .replace(u'<', u'\\u003c') \
        .replace(u'>', u'\\u003e') \
        .replace(u'&', u'\\u0026') \
        .replace(u"'", u'\\u0027')
    return Markup(rv) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:28,代碼來源:utils.py

示例8: legacy_html_escape

# 需要導入模塊: import markupsafe [as 別名]
# 或者: from markupsafe import escape [as 別名]
def legacy_html_escape(s):
    """legacy HTML escape for non-unicode mode."""
    s = s.replace("&", "&amp;")
    s = s.replace(">", "&gt;")
    s = s.replace("<", "&lt;")
    s = s.replace('"', "&#34;")
    s = s.replace("'", "&#39;")
    return s 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:10,代碼來源:filters.py


注:本文中的markupsafe.escape方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。