Пример #1
0
    def escape_html(html, convert_new_lines=False, whitespace=True):
        """
        Takes html text as input and escapes it so that it won't
        conflict with any xml/html wrapping characters.

        Args:
            html (str): The HTML code to escape
            convert_new_lines (:obj:`bool`, optional): escape new lines (\n)
            whitespace (:obj:`bool`, optional): escape whitespace

        Returns:
            str: The escaped html
        """
        if not isinstance(html, six.string_types) or not html:
            return ''

        # Escape HTML
        escaped = sax_escape(html, {"'": "'", "\"": """})

        if whitespace:
            # Tidy up whitespace too
            escaped = escaped\
                .replace(u'\t', u' ')\
                .replace(u' ', u' ')

        if convert_new_lines:
            return escaped.replace(u'\n', u'<br/>')

        return escaped
Пример #2
0
def escape(txt):
    """
    Escape "&", "<", ">" and django template tags chars like "{" and "}"
    defined in ENTITIES to the HTML character entity.
    >>> escape("<test1> & {{ test2 }} {% test3 %}")
    '&lt;test1&gt; &amp; &#x7B;&#x7B; test2 &#x7D;&#x7D; &#x7B;% test3 %&#x7D;'
    """
    txt = sax_escape(txt, entities=ENTITIES)
    return mark_safe(txt)
Пример #3
0
def escape(txt):
    """
    Escape "&", "<", ">" and django template tags chars like "{" and "}"
    defined in ENTITIES to the HTML character entity.
    >>> escape("<test1> & {{ test2 }} {% test3 %}")
    '&lt;test1&gt; &amp; &#x7B;&#x7B; test2 &#x7D;&#x7D; &#x7B;% test3 %&#x7D;'
    """
    txt = sax_escape(txt, entities=ENTITIES)
    return mark_safe(txt)
Пример #4
0
def escape(s):
    """
    @param      s       string to escape
    @return             escaped string
    """
    if isinstance(s, list):
        return [escape(_) for _ in s]
    else:
        s = sax_escape(s)
        s = s.replace("&", "&amp;")
        return s
Пример #5
0
    def escape_xml(self, unescaped_xml, encoding=None):
        """
        A Simple wrapper to Escape XML charaters from a string
        """

        if isinstance(unescaped_xml, unicode):
            # Encode content as per defined in our XML
            if encoding is None:
                encoding = self.encoding
            unescaped_xml = unescaped_xml.encode(encoding)

        return sax_escape(unescaped_xml, {"'": "&apos;", "\"": "&quot;"})
Пример #6
0
    def escape_xml(self, unescaped_xml, encoding=None):
        """
        A Simple wrapper to Escape XML charaters from a string
        """

        if isinstance(unescaped_xml, unicode):
            # Encode content as per defined in our XML
            if encoding is None:
                encoding = self.encoding
            unescaped_xml = unescaped_xml.encode(encoding)

        return sax_escape(unescaped_xml, {"'": "&apos;", "\"": "&quot;"})
Пример #7
0
def _escape(text):
    """
    saxutil escape tool
    """
    return sax_escape(text, {"'": "&apos;", "\"": "&quot;"})
Пример #8
0
def escape_xml(xml_str):
    return sax_escape(xml_str, ADDL_XML_ESC_CHARS)