예제 #1
0
파일: core.py 프로젝트: zu1kbackup/Canvas
    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'&#34;1 &lt; 2&#34;'>
        
        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 &lt; 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('&', '&amp;') \
                   .replace('<', '&lt;') \
                   .replace('>', '&gt;')
        if quotes:
            text = text.replace('"', '&#34;')
        return cls(text)
예제 #2
0
 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))
예제 #3
0
 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))
예제 #4
0
 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 (``&amp;``, ``&apos;``, ``&gt;``, ``&lt;`` and
     ``&quot;``) are not stripped.
     
     :return: a `Markup` instance with entities removed
     :rtype: `Markup`
     :see: `genshi.util.stripentities`
     """
     return Markup(stripentities(self, keepxmlentities=keepxmlentities))
예제 #5
0
 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))
예제 #6
0
 def __mul__(self, num):
     return Markup(six.text_type.__mul__(self, num))
예제 #7
0
 def __radd__(self, other):
     return Markup(six.text_type.__add__(escape(other), self))
예제 #8
0
파일: core.py 프로젝트: catb0t/tart
 def __mul__(self, num):
     return Markup(unicode.__mul__(self, num))
예제 #9
0
파일: core.py 프로젝트: catb0t/tart
 def __radd__(self, other):
     return Markup(unicode.__add__(escape(other), self))
예제 #10
0
파일: core.py 프로젝트: catb0t/tart
 def __add__(self, other):
     return Markup(unicode.__add__(self, escape(other)))
예제 #11
0
 def __mul__(self, num):
     return Markup(str.__mul__(self, num))
예제 #12
0
 def __radd__(self, other):
     return Markup(str.__add__(escape(other), self))
예제 #13
0
 def __add__(self, other):
     return Markup(str.__add__(self, escape(other)))
예제 #14
0
 def __rmul__(self, num):
     return Markup(num * unicode(self))
예제 #15
0
 def __mul__(self, num):
     return Markup(unicode(self) * num)
예제 #16
0
 def __radd__(self, other):
     return Markup(unicode(escape(other)) + unicode(self))