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
def escape(txt): """ Escape "&", "<", ">" and django template tags chars like "{" and "}" defined in ENTITIES to the HTML character entity. >>> escape("<test1> & {{ test2 }} {% test3 %}") '<test1> & {{ test2 }} {% test3 %}' """ txt = sax_escape(txt, entities=ENTITIES) return mark_safe(txt)
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("&", "&") return s
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, {"'": "'", "\"": """})
def _escape(text): """ saxutil escape tool """ return sax_escape(text, {"'": "'", "\"": """})
def escape_xml(xml_str): return sax_escape(xml_str, ADDL_XML_ESC_CHARS)