__init__.py (4826B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\jinja2\_markupsafe\__init__.py 2 import re 3 from itertools import imap 4 __all__ = ['Markup', 5 'soft_unicode', 6 'escape', 7 'escape_silent'] 8 _striptags_re = re.compile('(<!--.*?-->|<[^>]*>)') 9 _entity_re = re.compile('&([^;]+);') 10 11 class Markup(unicode): 12 __slots__ = () 13 14 def __new__(cls, base = u'', encoding = None, errors = 'strict'): 15 if hasattr(base, '__html__'): 16 base = base.__html__() 17 if encoding is None: 18 return unicode.__new__(cls, base) 19 return unicode.__new__(cls, base, encoding, errors) 20 21 def __html__(self): 22 return self 23 24 def __add__(self, other): 25 if hasattr(other, '__html__') or isinstance(other, basestring): 26 return self.__class__(unicode(self) + unicode(escape(other))) 27 return NotImplemented 28 29 def __radd__(self, other): 30 if hasattr(other, '__html__') or isinstance(other, basestring): 31 return self.__class__(unicode(escape(other)) + unicode(self)) 32 return NotImplemented 33 34 def __mul__(self, num): 35 if isinstance(num, (int, long)): 36 return self.__class__(unicode.__mul__(self, num)) 37 return NotImplemented 38 39 __rmul__ = __mul__ 40 41 def __mod__(self, arg): 42 if isinstance(arg, tuple): 43 arg = tuple(imap(_MarkupEscapeHelper, arg)) 44 else: 45 arg = _MarkupEscapeHelper(arg) 46 return self.__class__(unicode.__mod__(self, arg)) 47 48 def __repr__(self): 49 return '%s(%s)' % (self.__class__.__name__, unicode.__repr__(self)) 50 51 def join(self, seq): 52 return self.__class__(unicode.join(self, imap(escape, seq))) 53 54 join.__doc__ = unicode.join.__doc__ 55 56 def split(self, *args, **kwargs): 57 return map(self.__class__, unicode.split(self, *args, **kwargs)) 58 59 split.__doc__ = unicode.split.__doc__ 60 61 def rsplit(self, *args, **kwargs): 62 return map(self.__class__, unicode.rsplit(self, *args, **kwargs)) 63 64 rsplit.__doc__ = unicode.rsplit.__doc__ 65 66 def splitlines(self, *args, **kwargs): 67 return map(self.__class__, unicode.splitlines(self, *args, **kwargs)) 68 69 splitlines.__doc__ = unicode.splitlines.__doc__ 70 71 def unescape(self): 72 from jinja2._markupsafe._constants import HTML_ENTITIES 73 74 def handle_match(m): 75 name = m.group(1) 76 if name in HTML_ENTITIES: 77 return unichr(HTML_ENTITIES[name]) 78 try: 79 if name[:2] in ('#x', '#X'): 80 return unichr(int(name[2:], 16)) 81 if name.startswith('#'): 82 return unichr(int(name[1:])) 83 except ValueError: 84 pass 85 86 return u'' 87 88 return _entity_re.sub(handle_match, unicode(self)) 89 90 def striptags(self): 91 stripped = u' '.join(_striptags_re.sub('', self).split()) 92 return Markup(stripped).unescape() 93 94 @classmethod 95 def escape(cls, s): 96 rv = escape(s) 97 if rv.__class__ is not cls: 98 return cls(rv) 99 return rv 100 101 def make_wrapper(name): 102 orig = getattr(unicode, name) 103 104 def func(self, *args, **kwargs): 105 args = _escape_argspec(list(args), enumerate(args)) 106 _escape_argspec(kwargs, kwargs.iteritems()) 107 return self.__class__(orig(self, *args, **kwargs)) 108 109 func.__name__ = orig.__name__ 110 func.__doc__ = orig.__doc__ 111 return func 112 113 for method in ('__getitem__', 'capitalize', 'title', 'lower', 'upper', 'replace', 'ljust', 'rjust', 'lstrip', 'rstrip', 'center', 'strip', 'translate', 'expandtabs', 'swapcase', 'zfill'): 114 locals()[method] = make_wrapper(method) 115 116 if hasattr(unicode, 'partition'): 117 partition = (make_wrapper('partition'),) 118 rpartition = make_wrapper('rpartition') 119 if hasattr(unicode, 'format'): 120 format = make_wrapper('format') 121 if hasattr(unicode, '__getslice__'): 122 __getslice__ = make_wrapper('__getslice__') 123 del method 124 del make_wrapper 125 126 127 def _escape_argspec(obj, iterable): 128 for key, value in iterable: 129 if hasattr(value, '__html__') or isinstance(value, basestring): 130 obj[key] = escape(value) 131 132 return obj 133 134 135 class _MarkupEscapeHelper(object): 136 137 def __init__(self, obj): 138 self.obj = obj 139 140 __getitem__ = lambda s, x: _MarkupEscapeHelper(s.obj[x]) 141 __str__ = lambda s: str(escape(s.obj)) 142 __unicode__ = lambda s: unicode(escape(s.obj)) 143 __repr__ = lambda s: str(escape(repr(s.obj))) 144 __int__ = lambda s: int(s.obj) 145 __float__ = lambda s: float(s.obj) 146 147 148 try: 149 from jinja2._markupsafe._speedups import escape, escape_silent, soft_unicode 150 except ImportError: 151 from jinja2._markupsafe._native import escape, escape_silent, soft_unicode