def escape(cls, text, quotes=True): """Create a Markup instance from a string and escape special characters it may contain (<, >, & and \"). >>> escape('"1 < 2"') <Markup u'"1 < 2"'> If the `quotes` parameter is set to `False`, the \" character is left as is. Escaping quotes is generally only required for strings that are to be used in attribute values. >>> escape('"1 < 2"', quotes=False) <Markup u'"1 < 2"'> :param text: the text to escape :param quotes: if ``True``, double quote characters are escaped in addition to the other special characters :return: the escaped `Markup` string :rtype: `Markup` """ if not text: return cls() if type(text) is cls: return text if hasattr(text, '__html__'): return Markup(text.__html__()) text = text.replace('&', '&') \ .replace('<', '<') \ .replace('>', '>') if quotes: text = text.replace('"', '"') return cls(text)
def striptags(self): """Return a copy of the text with all XML/HTML tags removed. :return: a `Markup` instance with all tags removed :rtype: `Markup` :see: `genshi.util.striptags` """ return Markup(striptags(self))
def __mod__(self, args): if isinstance(args, dict): args = dict(zip(args.keys(), map(escape, args.values()))) elif isinstance(args, (list, tuple)): args = tuple(map(escape, args)) else: args = escape(args) return Markup(six.text_type.__mod__(self, args))
def stripentities(self, keepxmlentities=False): """Return a copy of the text with any character or numeric entities replaced by the equivalent UTF-8 characters. If the `keepxmlentities` parameter is provided and evaluates to `True`, the core XML entities (``&``, ``'``, ``>``, ``<`` and ``"``) are not stripped. :return: a `Markup` instance with entities removed :rtype: `Markup` :see: `genshi.util.stripentities` """ return Markup(stripentities(self, keepxmlentities=keepxmlentities))
def join(self, seq, escape_quotes=True): """Return a `Markup` object which is the concatenation of the strings in the given sequence, where this `Markup` object is the separator between the joined elements. Any element in the sequence that is not a `Markup` instance is automatically escaped. :param seq: the sequence of strings to join :param escape_quotes: whether double quote characters in the elements should be escaped :return: the joined `Markup` object :rtype: `Markup` :see: `escape` """ escaped_items = [escape(item, quotes=escape_quotes) for item in seq] return Markup(six.text_type.join(self, escaped_items))
def __mul__(self, num): return Markup(six.text_type.__mul__(self, num))
def __radd__(self, other): return Markup(six.text_type.__add__(escape(other), self))
def __mul__(self, num): return Markup(unicode.__mul__(self, num))
def __radd__(self, other): return Markup(unicode.__add__(escape(other), self))
def __add__(self, other): return Markup(unicode.__add__(self, escape(other)))
def __mul__(self, num): return Markup(str.__mul__(self, num))
def __radd__(self, other): return Markup(str.__add__(escape(other), self))
def __add__(self, other): return Markup(str.__add__(self, escape(other)))
def __rmul__(self, num): return Markup(num * unicode(self))
def __mul__(self, num): return Markup(unicode(self) * num)
def __radd__(self, other): return Markup(unicode(escape(other)) + unicode(self))