3 2kSb( @sdZddlZddlZddlmZddlmZmZmZm Z m Z m Z ddddgZ ej d Zej d ZGd ddeZGd d d eZeedrGdddejZddZGdddeZyddlmZmZmZWn(ek rddlmZmZmZYnXe s eZe jddS)z markupsafe ~~~~~~~~~~ Implements a Markup string. :copyright: (c) 2010 by Armin Ronacher. :license: BSD, see LICENSE for more details. N)Mapping) text_type string_types int_typesunichr iteritemsPY2Markup soft_unicodeescape escape_silentz(|<[^>]*>)z &([^;]+);cs2eZdZdZfZd>ddZddZfd d Zd d Zd dZ e Z ddZ ddZ ddZ ej je _ddZejje_ddZejje_ddZejje_ddZddZedd Zd!d"Zxd?D]Zeeee<qWeed3rd4d5Zd6d7Zeed8rd9d:Zd;d<Zeed=r&ed=Z[[ZS)@r aMarks a string as being safe for inclusion in HTML/XML output without needing to be escaped. This implements the `__html__` interface a couple of frameworks and web applications use. :class:`Markup` is a direct subclass of `unicode` and provides all the methods of `unicode` just that it escapes arguments passed and always returns `Markup`. The `escape` function returns markup objects so that double escaping can't happen. The constructor of the :class:`Markup` class can be used for three different things: When passed an unicode object it's assumed to be safe, when passed an object with an HTML representation (has an `__html__` method) that representation is used, otherwise the object passed is converted into a unicode string and then assumed to be safe: >>> Markup("Hello World!") Markup(u'Hello World!') >>> class Foo(object): ... def __html__(self): ... return 'foo' ... >>> Markup(Foo()) Markup(u'foo') If you want object passed being always treated as unsafe you can use the :meth:`escape` classmethod to create a :class:`Markup` object: >>> Markup.escape("Hello World!") Markup(u'Hello <em>World</em>!') Operations on a markup string are markup aware which means that all arguments are passed through the :func:`escape` function: >>> em = Markup("%s") >>> em % "foo & bar" Markup(u'foo & bar') >>> strong = Markup("%(text)s") >>> strong % {'text': 'hacker here'} Markup(u'<blink>hacker here</blink>') >>> Markup("Hello ") + "" Markup(u'Hello <foo>') NstrictcCs6t|dr|j}|dkr&tj||Stj||||S)N__html__)hasattrrr__new__)clsbaseencodingerrorsr /usr/lib64/python3.6/__init__.pyrFs   zMarkup.__new__cCs|S)Nr)selfrrrrMszMarkup.__html__cs4t|tst|dr0|jtt|j|j|StS)Nr) isinstancerr __class__superr __add__r NotImplemented)rother)rrrrPszMarkup.__add__cCs(t|dst|tr$|j|j|StS)Nr)rrrr rr)rrrrr__radd__UszMarkup.__radd__cCs t|tr|jtj||StS)N)rrrr__mul__r)rZnumrrrr Zs zMarkup.__mul__cs@t|tr"tfdd|D}n t|j}jtj|S)Nc3s|]}t|jVqdS)N)_MarkupEscapeHelperr ).0x)rrr bsz!Markup.__mod__..)rtupler!r rr__mod__)rargr)rrr&`s  zMarkup.__mod__cCsd|jjtj|fS)Nz%s(%s))r__name__r__repr__)rrrrr)gszMarkup.__repr__cCs|jtj|t|j|S)N)rrjoinmapr )rseqrrrr*msz Markup.joincOstt|jtj|f||S)N)listr+rrsplit)rargskwargsrrrr.qsz Markup.splitcOstt|jtj|f||S)N)r-r+rrrsplit)rr/r0rrrr1usz Markup.rsplitcOstt|jtj|f||S)N)r-r+rr splitlines)rr/r0rrrr2ys zMarkup.splitlinescs(ddlmfdd}tj|t|S)zUnescape markup again into an text_type string. This also resolves known HTML4 and XHTML entities: >>> Markup("Main » About").unescape() u'Main \xbb About' r) HTML_ENTITIESc s|jd}|krt|SyH|dddkrFtt|dddS|jdrdtt|ddSWntk rzYnXdS) N#x#X#r )r6r7)grouprint startswith ValueError)mname)r3rr handle_matchs   z%Markup.unescape..handle_match)Zmarkupsafe._constantsr3 _entity_resubr)rr@r)r3runescape~s  zMarkup.unescapecCs"djtjd|j}t|jS)aUnescape markup into an text_type string and strip all tags. This also resolves known HTML4 and XHTML entities. Whitespace is normalized to one: >>> Markup("Main » About").striptags() u'Main \xbb About'  r )r* _striptags_rerBr.r rC)rstrippedrrr striptagsszMarkup.striptagscCst|}|j|k r||S|S)zEscape the string. Works like :func:`escape` with the difference that for subclasses of :class:`Markup` this function would return the correct subclass. )r r)rsrvrrrr s z Markup.escapecs*tt|fdd}j|_j|_|S)Ncs>tt|t||j}t|t||j|j|f||S)N)_escape_argspecr- enumerater rr)rr/r0)origrrfuncsz1Markup.make_simple_escaping_wrapper..func)getattrrr(__doc__)r?rMr)rLrmake_simple_escaping_wrappers   z#Markup.make_simple_escaping_wrapper __getitem__ capitalizetitlelowerupperreplaceljustrjustlstriprstripcenterstrip translate expandtabsswapcasezfill partitioncCstt|jtj||j|S)N)r%r+rrrar )rseprrrraszMarkup.partitioncCstt|jtj||j|S)N)r%r+rr rpartitionr )rrbrrrrcszMarkup.rpartitionformatcOs>|d|dd}}t|j}t||}|j|j|||S)Nrr4)EscapeFormatterr _MagicFormatMappingrZvformat)r/r0rZ formatterrrrrds  z Markup.formatcCs|r td|S)Nz,Unsupported format specification for Markup.)r=)r format_specrrr__html_format__szMarkup.__html_format__ __getslice__)r Nr)rQrRrSrTrUrVrWrXrYrZr[r\r]r^r_r`) r( __module__ __qualname__rO __slots__rrrrr __rmul__r&r)r*rr.r1r2rCrG classmethodr rPmethodlocalsrrarcrdrhri __classcell__rr)rrr sH*           c@s0eZdZdZddZddZddZdd Zd S) rfzThis class implements a dummy wrapper to fix a bug in the Python standard library for string formatting. See http://bugs.python.org/issue13598 for information about why this is necessary. cCs||_||_d|_dS)Nr)_args_kwargs _last_index)rr/r0rrr__init__sz_MagicFormatMapping.__init__c CsN|dkrD|j}|jd7_y |j|Stk r:YnXt|}|j|S)Nr r4)rtrr LookupErrorstrrs)rkeyidxrrrrQs z_MagicFormatMapping.__getitem__cCs t|jS)N)iterrs)rrrr__iter__sz_MagicFormatMapping.__iter__cCs t|jS)N)lenrs)rrrr__len__sz_MagicFormatMapping.__len__N)r(rjrkrOrurQr{r}rrrrrfs  rfrdc@seZdZddZddZdS)recCs ||_dS)N)r )rr rrrruszEscapeFormatter.__init__cCsTt|dr|j|}n0t|dr6|r,td|j}ntjj|||}t|j|S)NrhrzSNo format specification allowed when formatting an object with its __html__ method.) rrhr=rstring Formatter format_fieldrr )rvaluergrIrrrrs    zEscapeFormatter.format_fieldN)r(rjrkrurrrrrresrecCs6x0|D](\}}t|ds"t|tr||||<qW|S)z,Helper for various string-wrapped functions.r)rrr)objiterabler rxrrrrrJ srJc@sDeZdZdZddZddZddZZddZddZ d dZ d S) r!zHelper for Markup.__mod__cCs||_||_dS)N)rr )rrr rrrrusz_MarkupEscapeHelper.__init__cCst|j||jS)N)r!rr )rHr#rrrsz_MarkupEscapeHelper.cCst|j|jS)N)rr r)rHrrrrscCst|jt|jS)N)rwr reprr)rHrrrrscCs t|jS)N)r;r)rHrrrrscCs t|jS)N)floatr)rHrrrrsN) r(rjrkrOrurQZ __unicode____str__r)__int__ __float__rrrrr!s r!)r r r soft_str) rOrer~ collectionsrZmarkupsafe._compatrrrrrr__all__compilerErAr rfrrrerJobjectr!Zmarkupsafe._speedupsr r r ImportErrorZmarkupsafe._nativerappendrrrr s*     @