U ‚xà[´'ã@s<dZddlZddlZddlmZddlmZddlmZddlmZddlmZdd lm Z dd lm Z d Z d d ddgZ e  d¡Ze  d¡ZGdd „d e ƒZGdd„deƒZee dƒrÈGdd„dejƒZdd„ZGdd„deƒZzddlmZmZmZWn*ek r"ddlmZmZmZYnXes8eZe  d¡dS)uß markupsafe ~~~~~~~~~~ Implements an escape function and a Markup string to replace HTML special characters with safe representations. :copyright: © 2010 by the Pallets team. :license: BSD, see LICENSE for more details. éNé)Ú int_types)Ú iteritems)ÚMapping)ÚPY2)Ú string_types)Ú text_type)Úunichrz1.1.0ÚMarkupÚ soft_unicodeÚescapeÚ escape_silentz(|<[^>]*>)z &([^& ;]+);cseZdZdZdZd.dd„Zdd „Z‡fd d „Zd d „Zdd„Z e Z dd„Z dd„Z dd„Z ej je _dd„Zejje_dd„Zejje_dd„Zejje_dd„Zdd„Zed d!„ƒZd"d#„Zd$D]Zeeƒeƒe<qÂd%d&„Zd'd(„Zd)d*„Zd+d,„Zeed-ƒr ed-ƒZ[[‡ZS)/r aöA string that is ready to be safely inserted into an HTML or XML document, either because it was escaped or because it was marked safe. Passing an object to the constructor converts it to text and wraps it to mark it safe without escaping. To escape the text, use the :meth:`escape` class method instead. >>> Markup('Hello, World!') Markup('Hello, World!') >>> Markup(42) Markup('42') >>> Markup.escape('Hello, World!') Markup('Hello <em>World</em>!') This implements the ``__html__()`` interface that some frameworks use. Passing an object that implements ``__html__()`` will wrap the output of that method, marking it safe. >>> class Foo: ... def __html__(self): ... return 'foo' ... >>> Markup(Foo()) Markup('foo') This is a subclass of the text type (``str`` in Python 3, ``unicode`` in Python 2). It has the same methods as that type, but all methods escape their arguments and return a ``Markup`` instance. >>> Markup('%s') % 'foo & bar' Markup('foo & bar') >>> Markup('Hello ') + '' Markup('Hello <foo>') ©ÚNÚstrictcCs6t|dƒr| ¡}|dkr&t ||¡St ||||¡S©NÚ__html__)ÚhasattrrrÚ__new__)ÚclsÚbaseÚencodingÚerrorsrrú5/usr/lib/python3/dist-packages/markupsafe/__init__.pyrFs   zMarkup.__new__cCs|S©Nr©ÚselfrrrrMszMarkup.__html__cs4t|tƒst|dƒr0| tt|ƒ | |¡¡¡StSr) Ú isinstancerrÚ __class__Úsuperr Ú__add__r ÚNotImplemented©rÚother©rrrr PszMarkup.__add__cCs(t|dƒst|tƒr$| |¡ |¡StSr)rrrr r r!r"rrrÚ__radd__UszMarkup.__radd__cCs t|tƒr| t ||¡¡StSr)rrrrÚ__mul__r!)rZnumrrrr&Zs zMarkup.__mul__cs@t|tƒr"t‡fdd„|Dƒƒ}n t|ˆjƒ}ˆ t ˆ|¡¡S)Nc3s|]}t|ˆjƒVqdSr)Ú_MarkupEscapeHelperr )Ú.0ÚxrrrÚ csz!Markup.__mod__..)rÚtupler'r rrÚ__mod__)rÚargrrrr,as  zMarkup.__mod__cCsd|jjt |¡fS)Nz%s(%s))rÚ__name__rÚ__repr__rrrrr/hszMarkup.__repr__cCs| t |t|j|ƒ¡¡Sr)rrÚjoinÚmapr )rÚseqrrrr0ksz Markup.joincOstt|jtj|f|ž|ŽƒƒSr)Úlistr1rrÚsplit©rÚargsÚkwargsrrrr4psz Markup.splitcOstt|jtj|f|ž|ŽƒƒSr)r3r1rrÚrsplitr5rrrr8usz Markup.rsplitcOstt|jtj|f|ž|ŽƒƒSr)r3r1rrÚ splitlinesr5rrrr9zszMarkup.splitlinescs(ddlm‰‡fdd„}t |t|ƒ¡S)ußConvert escaped markup back into a text string. This replaces HTML entities with the characters they represent. >>> Markup('Main » About').unescape() 'Main » About' r©Ú HTML_ENTITIEScsˆ| d¡}|ˆkrtˆ|ƒSzL|dd…dkrHtt|dd…dƒƒWS| d¡rhtt|dd…ƒƒWSWntk r~YnX| ¡S)Nré)z#xz#Xéú#)Úgroupr ÚintÚ startswithÚ ValueError)ÚmÚnamer:rrÚ handle_matchˆs   z%Markup.unescape..handle_match)Z _constantsr;Ú _entity_reÚsubr)rrErr:rÚunescapes  zMarkup.unescapecCs"d t d|¡ ¡¡}t|ƒ ¡S)u¼:meth:`unescape` the markup, remove tags, and normalize whitespace to single spaces. >>> Markup('Main » About').striptags() 'Main » About' ú r)r0Ú _striptags_rerGr4r rH)rÚstrippedrrrÚ striptags˜szMarkup.striptagscCst|ƒ}|j|k r||ƒS|S)ztEscape a string. Calls :func:`escape` and ensures that for subclasses the correct type is returned. )r r)rÚsÚrvrrrr ¢s z Markup.escapecs*tt|ƒ‰‡fdd„}ˆj|_ˆj|_|S)Ncs>tt|ƒt|ƒ|jƒ}t|t|ƒ|jƒ| ˆ|f|ž|Ž¡Sr)Ú_escape_argspecr3Ú enumerater rrr5©ZorigrrÚfunc¯sz1Markup.make_simple_escaping_wrapper..func)Úgetattrrr.Ú__doc__)rDrRrrQrÚmake_simple_escaping_wrapper¬s   z#Markup.make_simple_escaping_wrapper)Ú __getitem__Ú capitalizeÚtitleÚlowerÚupperÚreplaceÚljustÚrjustÚlstripÚrstripÚcenterÚstripÚ translateÚ expandtabsÚswapcaseÚzfillc Cstt|jt || |¡¡ƒƒSr)r+r1rrÚ partitionr ©rÚseprrrrfÌszMarkup.partitionc Cstt|jt || |¡¡ƒƒSr)r+r1rrÚ rpartitionr rgrrrriÏszMarkup.rpartitioncOs(t|jƒ}t||ƒ}| | |||¡¡Sr)ÚEscapeFormatterr Ú_MagicFormatMappingrZvformat)rr6r7Z formatterrrrÚformatÒs  z Markup.formatcCs|r tdƒ‚|S)Nz,Unsupported format specification for Markup.)rB)rÚ format_specrrrÚ__html_format__×szMarkup.__html_format__Ú __getslice__)rNr) r.Ú __module__Ú __qualname__rTÚ __slots__rrr r%r&Ú__rmul__r,r/r0rr4r8r9rHrLÚ classmethodr rUÚmethodÚlocalsrfrirlrnrroÚ __classcell__rrr$rr s@$          c@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) rkzÍThis 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_indexr5rrrÚ__init__ësz_MagicFormatMapping.__init__cCsP|dkrF|j}|jd7_z |j|WStk r<YnXt|ƒ}|j|S)Nrr)rzrxÚ LookupErrorÚstrry)rÚkeyÚidxrrrrVðs z_MagicFormatMapping.__getitem__cCs t|jƒSr)ÚiterryrrrrÚ__iter__ûsz_MagicFormatMapping.__iter__cCs t|jƒSr)ÚlenryrrrrÚ__len__þsz_MagicFormatMapping.__len__N)r.rprqrTr{rVrrƒrrrrrkãs  rkrlc@seZdZdd„Zdd„ZdS)rjcCs ||_dSr)r )rr rrrr{szEscapeFormatter.__init__cCsdt|dƒr| |¡}n@t|dƒrB|r8td |t|ƒ¡ƒ‚| ¡}ntj ||t |ƒ¡}t |  |¡ƒS)NrnrzžFormat specifier {0} given, but {1} does not define __html_format__. A class that defines __html__ must define __html_format__ to work with format specifiers.) rrnrBrlÚtyperÚstringÚ FormatterÚ format_fieldrr )rÚvaluermrNrrrr‡s   ýÿ zEscapeFormatter.format_fieldN)r.rprqr{r‡rrrrrjsrjcCs2|D](\}}t|dƒs t|tƒr||ƒ||<q|S)z,Helper for various string-wrapped functions.r)rrr)ÚobjÚiterabler r~rˆrrrrOs rOc@sDeZdZdZdd„Zdd„Zdd„ZeZdd „Zd d „Z d d „Z dS)r'zHelper for Markup.__mod__cCs||_||_dSr)r‰r )rr‰r rrrr{(sz_MarkupEscapeHelper.__init__cCst|j||jƒSr)r'r‰r )rÚitemrrrrV,sz_MarkupEscapeHelper.__getitem__cCst| |j¡ƒSr)rr r‰rrrrÚ__str__/sz_MarkupEscapeHelper.__str__cCst| t|jƒ¡ƒSr)r}r Úreprr‰rrrrr/4sz_MarkupEscapeHelper.__repr__cCs t|jƒSr)r@r‰rrrrÚ__int__7sz_MarkupEscapeHelper.__int__cCs t|jƒSr)Úfloatr‰rrrrÚ __float__:sz_MarkupEscapeHelper.__float__N) r.rprqrTr{rVrŒZ __unicode__r/rŽrrrrrr'%sr')r r r Úsoft_str) rTÚrer…Z_compatrrrrrrr Ú __version__Ú__all__ÚcompilerJrFr rkrr†rjrOÚobjectr'Z _speedupsr r r Ú ImportErrorZ_nativer‘ÚappendrrrrÚs6           E