lfIgdZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z dd l m Z dd l mZdd l mZdd l mZdd l mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddlmZddlmZddlmZddlmZddl m!Z!ddl"m#Z#ddl"m$Z$dZ%ej&dZ'Gdd e(Z)Gd!d"e e)e*Z+e#d#Z,d$Z-d%Z.Gd&d'e+Z/Gd(d)e+Z0Gd*d+e+Z1Gd,d-e+Z2Gd.d/e+Z3Gd0d1e+Z4e%d2fd3Z5Gd4d5e*Z6d6Z7e/Z8e0Z9e1Z:e2Z;e3ZFz IId6I * *         r0r8)NNNNN)r(r'r1r2settagspriorityr<rBrFrIrMrRrYr3r0r/r5r52s& 355DH'''$$$NNNNTX      r0r5cJ|j|dg|Ri|S)Nr)callresolve) __contextrUrVs r/_gettext_aliasras2 9>)++I66 H H H H H HHr0c,tfd}|S)Ncp||}|jjrt|}||zSr8)r^eval_ctx autoescaper)r`__string variablesr.funcs r/rz"_make_new_gettext..gettexts; ^^D( + +   ( BI~r0r)rhrs` r/_make_new_gettextris-_ Nr0c,tfd}|S)Nc|d|||||}|jjrt |}||zS)Nnum) setdefaultr^rdrer)r` __singular__plural__numrgr.rhs r/r z$_make_new_ngettext..ngettextsSUE*** ^^D*h > >   ( BI~r0r)rhr s` r/_make_new_ngettextrqs-_ Or0cbeZdZdZdhZdZddZddZddZdZ e fd Z d Z e fd Zd Zd ZdS)InternationalizationExtensionz-This extension adds gettext support to Jinja.transct||t|jd<||j|j|j|j|j ddS)NrF)install_gettext_translationsinstall_null_translationsinstall_gettext_callablesuninstall_gettext_translationsextract_translationsnewstyle_gettext) r5r<raglobalsextend_install _install_null_install_callables _uninstall_extractr:s r/r<z&InternationalizationExtension.__init__sj4---#1 C )-&*&8&*&=+/?!%"      r0Nct|dd}||j}t|dd}||j}||||dS)Nugettext ungettext)getattrrr r)r; translationsnewstylerr s r/r~z&InternationalizationExtension._installs], D99 ?"*G<d;;  #,H 8<<<<z=InternationalizationExtension._install_null..sar0c&|dkr|fp|fdS)Nrrr3)spns r/rz=InternationalizationExtension._install_null..s!q&/aT*AaT1)Er0)r)r;rs r/rz+InternationalizationExtension._install_nulls1  KEEx     r0c| ||j_|jjrt|}t|}|jj||dSN)rr )r9r{rirqr|rA)r;rr rs r/rz0InternationalizationExtension._install_callabless`  08D  -   , 4'00G)(33H  ''('KKKKKr0cPdD]"}|jj|d#dSr)r9r|pop)r;rkeys r/rz(InternationalizationExtension._uninstalls:( 4 4C   $ ( (d 3 3 3 3 4 4r0ct|tr|j|}t ||Sr8) isinstancerr9rMextract_from_ast)r;rDgettext_functionss r/rz&InternationalizationExtension._extracts; fl + + 4%++F33F(9:::r0c ht|jj}d}d}d}i}d}|jjjdkr|r|jd|jdrnW|jd}|j|vr*|d|jz|jt|jjjd kr3t|j| x||j<} n;||jd vr |jd k}tj |jd x||j<} |ot| tjrHtj d d }|||j<tjtj d d| }n| }|jdk}|jjjdk|jdd} d} t!} ||d\} }| r>| | |'tj | dd }| ddk}|jjdrd} t|j|jjjdkre|jd}|j|vr*|d|jz|jt||j}|jdk}|jd||d\}} t|j| |nt|j| D]} | |vrtj | d || <| sd}n||d|||jjd}|r,||}| r|| } ||| ||t1| |o| }|||||gS|S)zParse a translatable tag.FN block_endcommacolonr+z'translatable variable %r defined twice.)excassign)trimmed notrimmedrload_transstorerlTrname:pluralizez%unknown variable %r for pluralizationzpluralize without variablesext.i18n.trimmed)nextrHrPcurrentr%expectskip_ifvaluefailrparse_expressionrNamerrTAssignrZ _parse_blockrAtestr9policies_trim_whitespace _make_nodebool set_lineno)r;rLrPnum_called_num plural_exprplural_expr_assignmentrgrr+varplural have_plural referencedsingular_namessingular plural_namesnodes r/rMz#InternationalizationExtension.parsesfm$$+  !% m#(K77 . $$W---}$$W-- =''//DzY&& = JK.}$)X55V]###.4.E.E.G.GG $*%TZ3K%K%K* 1.3jV.L.LL $*%"c5:..&"'*Xv">">K,7Idj)-2\ 8W55s..**#&K!%u!4Gm#(K77J  [))) UU $(#4#4VT#B#B   <   n - - -"#j):FCC !/!2e!; = % %&6 7 7 K    }$)[88}++F33:Y..KK?$*L 2  ( 3 !%u!4 M  - - -#'#4#4VU#C#C L&       l + + + +     9 9C)##!&C!8!8 # ?KK  KK5v > > > ?&/0BCG  7,,X66H 7..v66         *{     ! -*D1 1Kr0cR|d|S)N )substrip)r;string_ws_res r/rz.InternationalizationExtension._trim_whitespaceXszz#v||~~...r0cg}g} |jjjdkrN||jjjddt |jnW|jjjdkr{t |j|jdj}|||d|z|jdn|jjjd krt |j|jjd rn|jjd r|rn^| d | d n1|jj r| dntd|t|fS)z1Parse until the next block tag with a given name.rdata%%%variable_beginr+z%%(%s)s variable_end block_beginz name:endtransrz:a translatable section can have only one pluralize sectionz;control structures in translatable sections are not allowedzunclosed translation blockzinternal parser error) rHrr%appendrreplacerrrreos RuntimeErrorr)r;rLallow_pluralizerbufr+s r/rz*InternationalizationExtension._parse_block[s  <}$)V33 6=06>>sDIIJJJV]####&+/???V]###}++F339!!$''' 9t+,,, $$^4444&+}<<V]###=(--o>>]*//0@AA&KKT Q" < 89999"#:;;;5 <86#;;&&r0c4|s:|jjs.|dd}|r|dd}|Atjdd}tj|tj|ggdd}nTtjdd} tj| tj|tj||ggdd}|jjrLt|D];\} } |r| dkr|j tj | | z.sB *U"Ju{3'7'7??r0)r9r{rrrrTrrrVrKeywordMarkSafeIfAutoescapeModDictitemsOutput) r;rrrgrvars_referencedrrrr rrs r/rz(InternationalizationExtension._make_node}s  3t'7'H 3''c22H 3c22  jF33G:g H(=(='>D$OODDz*f55H:X&& F(;(;[I D   , ' 22 > > U"cUll ""5=e#<#<====  >-d33D yJ.7oo.?.?|TF###r0r8)r(r'r1r2r[r<r~rrrGETTEXT_FUNCTIONSrrMrrrrr3r0r/rsrss77 9D    ====    LLLL4442C;;;; qqqf/5//// ' ' 'D5$5$5$5$5$r0rsc0eZdZdZedgZdZdS)ExprStmtExtensionzrAdds a `do` tag to Jinja that works like the print statement just that it doesn't print the return value. doctjt|jj}||_|S)NrO)rExprStmtrrHrP parse_tupler)r;rLrs r/rMzExprStmtExtension.parses9~T&-%8%8%?@@@&&((  r0Nr(r'r1r2rZr[rMr3r0r/rrs> 3v;;Dr0rc2eZdZdZeddgZdZdS)LoopControlExtensionz/Adds break and continue to the template engine.breakcontinuect|j}|jdkrtj|jStj|jS)NrrO)rrHrrBreakrPContinue)r;rLtokens r/rMzLoopControlExtension.parsesHV]## ;' ! !;el333 3~U\2222r0Nrr3r0r/rrs=99 3$ % %D33333r0rceZdZdS) WithExtensionNr(r'r1r3r0r/rrDr0rceZdZdS)AutoEscapeExtensionNrr3r0r/rrrr0rc$eZdZdZdhZdZdZdS)DebugExtensionaA ``{% debug %}`` tag that dumps the available variables, filters, and tests. .. code-block:: html+jinja
{% debug %}
.. code-block:: text {'context': {'cycler': , ..., 'namespace': }, 'filters': ['abs', 'attr', 'batch', 'capitalize', 'center', 'count', 'd', ..., 'urlencode', 'urlize', 'wordcount', 'wordwrap', 'xmlattr'], 'tests': ['!=', '<', '<=', '==', '>', '>=', 'callable', 'defined', ..., 'odd', 'sameas', 'sequence', 'string', 'undefined', 'upper']} .. versionadded:: 2.11.0 debugc|jdj}t}|d|g|}t j|g|S)Nz name:debug_renderrO)rHrrPrrYrr)r;rLrPcontextresults r/rMzDebugExtension.parsesX%%l33:"$$!!)gYv!FF|VHV4444r0cV|t|jjt|jjd}t dddkrtj|ddStj|dS)N)rfilterstests)rT)depthcompact)r) get_allsortedr9rkeysrrpprintpformat)r;rrs r/rzDebugExtension._renders((d.6;;==>>D,27799::    v % %>&4@@@ @>&222 2r0N)r(r'r1r2r[rMrr3r0r/rrsE( 9D555 3 3 3 3 3r0rTc#K|tjD]Y}t|jtjr|jj|vr1g}|jD]f}t|tjr5t|j tr| |j Q| dg|j D]}| d|j | d|j| d|std|D}|sn+t!|dkr |d}nt|}|j|jj|fV[dS)aExtract localizable strings from the given template node. Per default this function returns matches in babel style that means non string parameters as well as keyword arguments are returned as `None`. This allows Babel to figure out what you really meant if you are using gettext functions that allow keyword arguments for placeholder expansion. If you don't want that behavior set the `babel_style` parameter to `False` which causes only strings to be returned and parameters are always stored in tuples. As a consequence invalid gettext calls (calls without a single string parameter or string parameters after non-string parameters) are skipped. This example explains the behavior: >>> from jinja2 import Environment >>> env = Environment() >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}') >>> list(extract_from_ast(node)) [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))] >>> list(extract_from_ast(node, babel_style=False)) [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))] For every string found this function yields a ``(lineno, function, message)`` tuple, where: * ``lineno`` is the number of the line on which the string was found, * ``function`` is the name of the ``gettext`` function used (if the string was extracted from embedded Python code), and * ``message`` is the string itself (a ``unicode`` object, or a tuple of ``unicode`` objects for functions with multiple string arguments). This extraction function operates on the AST and is because of that unable to extract any comments. For comment support you have to use the babel extraction interface or extract comments yourself. Nc3K|]}||V dSr8r3)rrs r/ z#extract_from_ast..:s"@@!!-A----@@r0rr)find_allrrTrrrr+rUrrrrrVrWrXtuplelenrP)rr babel_stylestringsargrs r/rrsF ej))3349ej11 y~%666 9 % %C#u{++ % 39l0S0S %sy))))t$$$$ ! !A NN4 = $ NN4 ? & NN4  )@@w@@@@@G  7||q  !!*..k49>722222=33r0c$eZdZdZdZdZdZdS)_CommentFinderzHelper class to find comments in a token stream. Can only find comments for gettext calls forwards. Once the comment from line 4 is found, a comment for line 1 will not return a usable value. c>||_||_d|_d|_dS)Nr)tokens comment_tagsoffset last_lineno)r;rrs r/r<z_CommentFinder.__init__Ls& ( r0c* t|j|j|D]\\}}}|dvrR |dd\}}n#t$rY1wxYw||jvr|gc||_S]g||_S#||_wxYw)N)comment linecommentr)reversedrrsplit ValueErrorrrstrip)r;rr token_type token_valueprefixrs r/find_backwardsz_CommentFinder.find_backwardsRs !.6 DK&01// 2 2*:{!;;;!*5*;*;D!*D*D%!!! !!222 ' 0 0111!DKK DKK&DK s4+B AB  AB A"B ?B Bc|jr |j|krgSt|j|jdD].\}\}}}||kr||j|zcS/|t |jSr8)rr enumeraterrr&r)r;rPidx token_linenors r/ find_commentsz_CommentFinder.find_commentsbs  D$4v$=$=I)24;t{}}3M)N)N > > %C%,1f$$**4;+<=====%""3t{#3#3444r0N)r(r'r1r2r<r&r+r3r0r/rrEsK  !!! 55555r0rc#Kt}|dddD];}|}|s|t |.getbools.{{3G --33559SSSr0silentTblock_start_stringblock_end_stringvariable_start_stringvariable_end_stringcomment_start_stringcomment_end_stringline_statement_prefixline_comment_prefix trim_blocks lstrip_blockskeep_trailing_newliner) cache_size auto_reloadrrr{encodingzutf-8N)F)!rZr5r raddrrsrr r rrr r rrrrrr frozensetrr{readdecoderMlistlexrFrrrr+)fileobjkeywordsrr8r- extensionr:r;r9rDrrfinderrPrhmessages r/ babel_extractrUks8J[[r2288==11 OO%%   }Y//0000$J664555TTTTWWh - -F (*<== &(899 +-BCC )+>?? *,@AA (*<== +,,E0E )**A.A 44-8802GHH*K$ww ""837 /0ww*++,'+ $ \\^^ " "7;;z7#C#C D DF  ((kook&<&r2r resysr markupsaferr.r_compatrrr defaultsr r r r rrrrrrrrr9r exceptionsrrrruntimerutilsrrrcompilerr%r"r>r5rarirqrsrrrrrrrrUi18nr loopcontrolswith_rerr3r0r/rbs)88 !!!!!!######&&&&&&((((((((((((******++++++))))))++++++######&&&&&&!!!!!!))))))++++++$$$$$$......++++++######"""""" 1 K Y Y Y Y Y 0&99Y Y Y xIII      D$D$D$D$D$ID$D$D$N         3 3 3 3 39 3 3 3     I        )   (3(3(3(3(3Y(3(3(3V.?DA3A3A3A3H#5#5#5#5#5V#5#5#5LLBLBLB`%#  r0