3 F\' @s<dZddlZddlZddlmZddlmZddlmZddlmZddlmZdd lm Z dd lm Z d Z d d ddgZ ej dZej dZGdd d e ZGdddeZee drGdddejZddZGdddeZyddlmZmZmZWn*ek r"ddlmZmZmZYnXes8eZe jddS)z markupsafe ~~~~~~~~~~ Implements an escape function and a Markup string to replace HTML special characters with safe representations. :copyright: 2010 Pallets :license: BSD-3-Clause N) int_types) iteritems)Mapping)PY2) string_types) text_type)unichrz1.1.1Markup soft_unicodeescape escape_silentz(|<[^>]*>)z &([^& ;]+);cseZdZdZfZdr aA 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>') NstrictcCs6t|dr|j}|dkr&tj||Stj||||S)N__html__)hasattrrr__new__)clsbaseencodingerrorsro/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/MarkupSafe/markupsafe/__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)rnumrrrr!Zs zMarkup.__mul__cs@t|tr"tfdd|D}n t|j}jtj|S)Nc3s|]}t|jVqdS)N)_MarkupEscapeHelperr ).0x)rrr csz!Markup.__mod__..)rtupler#r rr__mod__)rargr)rrr(as  zMarkup.__mod__cCsd|jjtj|fS)Nz%s(%s))r__name__r__repr__)rrrrr+hszMarkup.__repr__cCs|jtj|t|j|S)N)rrjoinmapr )rseqrrrr,ksz Markup.joincOstt|jtj|f||S)N)listr-rrsplit)rargskwargsrrrr0psz Markup.splitcOstt|jtj|f||S)N)r/r-rrrsplit)rr1r2rrrr3usz Markup.rsplitcOstt|jtj|f||S)N)r/r-rr splitlines)rr1r2rrrr4zszMarkup.splitlinescs(ddlmfdd}tj|t|S)uConvert escaped markup back into a text string. This replaces HTML entities with the characters they represent. >>> Markup('Main » About').unescape() 'Main » About' r) HTML_ENTITIESc s|jd}|krt|SyH|dddkrFtt|dddS|jdrdtt|ddSWntk rzYnX|jS)Nr#x#X#)r7r8)groupr int startswith ValueError)mname)r5rr handle_matchs   z%Markup.unescape..handle_match) _constantsr5 _entity_resubr)rrAr)r5runescapes  zMarkup.unescapecCs"djtjd|j}t|jS)u:meth:`unescape` the markup, remove tags, and normalize whitespace to single spaces. >>> Markup('Main » About').striptags() 'Main » About'  r)r, _striptags_rerDr0r rE)rstrippedrrr striptagsszMarkup.striptagscCst|}|j|k r||S|S)ztEscape a string. Calls :func:`escape` and ensures that for subclasses the correct type is returned. )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)rr1r2)origrrfuncsz1Markup.make_simple_escaping_wrapper..func)getattrrr*__doc__)r@rOr)rNrmake_simple_escaping_wrappers   z#Markup.make_simple_escaping_wrapper __getitem__ capitalizetitlelowerupperreplaceljustrjustlstriprstripcenterstrip translate expandtabsswapcasezfillcCstt|jtj||j|S)N)r'r-rr partitionr )rseprrrrcszMarkup.partitioncCstt|jtj||j|S)N)r'r-rr rpartitionr )rrdrrrreszMarkup.rpartitioncOs(t|j}t||}|j|j|||S)N)EscapeFormatterr _MagicFormatMappingrvformat)rr1r2 formatterrrrformats  z Markup.formatcCs|r td|S)Nz,Unsupported format specification for Markup.)r>)r format_specrrr__html_format__szMarkup.__html_format__ __getslice__)rNr)rSrTrUrVrWrXrYrZr[r\r]r^r_r`rarb) r* __module__ __qualname__rQ __slots__rrrr r!__rmul__r(r+r,rr0r3r4rErI classmethodr rRmethodlocalsrcrerjrlrrm __classcell__rr)rrr s^#         c@s0eZdZdZddZddZddZdd Zd S) rgzThis 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)rr1r2rrr__init__sz_MagicFormatMapping.__init__c CsN|dkrD|j}|jd7_y |j|Stk r:YnXt|}|j|S)Nrr)rxrv LookupErrorstrrw)rkeyidxrrrrSs z_MagicFormatMapping.__getitem__cCs t|jS)N)iterrw)rrrr__iter__sz_MagicFormatMapping.__iter__cCs t|jS)N)lenrw)rrrr__len__sz_MagicFormatMapping.__len__N)r*rnrorQryrSrrrrrrrgs  rgrjc@seZdZddZddZdS)rfcCs ||_dS)N)r )rr rrrryszEscapeFormatter.__init__cCsdt|dr|j|}n@t|drB|r8tdj|t||j}ntjj||t |}t |j |S)NrlrzFormat specifier {0} given, but {1} does not define __html_format__. A class that defines __html__ must define __html_format__ to work with format specifiers.) rrlr>rjtyperstring Formatter format_fieldrr )rvaluerkrKrrrrs    zEscapeFormatter.format_fieldN)r*rnroryrrrrrrfsrfcCs6x0|D](\}}t|ds"t|tr||||<qW|S)z,Helper for various string-wrapped functions.r)rrr)objiterabler r|rrrrrLsrLc@sDeZdZdZddZddZddZeZdd Zd d Z d d Z dS)r#zHelper for Markup.__mod__cCs||_||_dS)N)rr )rrr rrrry(sz_MarkupEscapeHelper.__init__cCst|j||jS)N)r#rr )ritemrrrrS,sz_MarkupEscapeHelper.__getitem__cCst|j|jS)N)rr r)rrrr__str__/sz_MarkupEscapeHelper.__str__cCst|jt|jS)N)r{r reprr)rrrrr+4sz_MarkupEscapeHelper.__repr__cCs t|jS)N)r<r)rrrr__int__7sz_MarkupEscapeHelper.__int__cCs t|jS)N)floatr)rrrr __float__:sz_MarkupEscapeHelper.__float__N) r*rnrorQryrSr __unicode__r+rrrrrrr#%sr#)r r r soft_str) rQrer_compatrrrrrrr __version____all__compilerGrCr rgrrrfrLobjectr#Z _speedupsr r r ImportErrorZ_nativerappendrrrr s6          E