o !b0@sdZddlZddlZddlmZddlmZmZmZm Z m Z m Z ddl m Z ddlTddlmZgdZd ZGd d d eZGd d d eZeZdS)aPlain text templating engine. This module implements two template language syntaxes, at least for a certain transitional period. `OldTextTemplate` (aliased to just `TextTemplate`) defines a syntax that was inspired by Cheetah/Velocity. `NewTextTemplate` on the other hand is inspired by the syntax of the Django template language, which has more explicit delimiting of directives, and is more flexible with regards to white space and line breaks. In a future release, `OldTextTemplate` will be phased out in favor of `NewTextTemplate`, as the names imply. Therefore the new syntax is strongly recommended for new projects, and existing projects may want to migrate to the new syntax to remain compatible with future Genshi releases. N)TEXT)BadDirectiveErrorTemplateTemplateSyntaxErrorEXECINCLUDESUB)Suite)*) interpolate)NewTextTemplateOldTextTemplate TextTemplatezrestructuredtext enc@s~eZdZdZdefdefdefdefdefde fde fgZ d Z d Z d Z  dddZddZddZeeedZddZd S)r aImplementation of a simple text-based template engine. This class will replace `OldTextTemplate` in a future release. It uses a more explicit delimiting style for directives: instead of the old style which required putting directives on separate lines that were prefixed with a ``#`` sign, directives and commenbtsr are enclosed in delimiter pairs (by default ``{% ... %}`` and ``{# ... #}``, respectively). Variable substitution uses the same interpolation syntax as for markup languages: simple references are prefixed with a dollar sign, more complex expression enclosed in curly braces. >>> tmpl = NewTextTemplate('''Dear $name, ... ... {# This is a comment #} ... We have the following items for you: ... {% for item in items %} ... * ${'Item %d' % item} ... {% end %} ... ''') >>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None)) Dear Joe, We have the following items for you: * Item 1 * Item 2 * Item 3 By default, no spaces or line breaks are removed. If a line break should not be included in the output, prefix it with a backslash: >>> tmpl = NewTextTemplate('''Dear $name, ... ... {# This is a comment #}\ ... We have the following items for you: ... {% for item in items %}\ ... * $item ... {% end %}\ ... ''') >>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None)) Dear Joe, We have the following items for you: * 1 * 2 * 3 Backslashes are also used to escape the start delimiter of directives and comments: >>> tmpl = NewTextTemplate('''Dear $name, ... ... \\{# This is a comment #} ... We have the following items for you: ... {% for item in items %}\ ... * $item ... {% end %}\ ... ''') >>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None)) Dear Joe, {# This is a comment #} We have the following items for you: * 1 * 2 * 3 :since: version 0.5 defwhen otherwiseforifchoosewithtextz/((?z/NewTextTemplate._set_delims..cSr)r$r*r-r$r$r%r0r1) len ValueErrorr&r+compile _DIRECTIVE_REtupleDOTALL _directive_re _ESCAPE_RE _escape_re)r r#r$r$r% _set_delimss    zNewTextTemplate._set_delimsz The delimiters for directives and comments. This should be a four item tuple of the form ``(directive_start, directive_end, comment_start, comment_end)``, where each item is a string. c Csg}i}d}|}t|tjs||pdd}d}d}|jj}dd} t|j |D]\} } | d\} } | |kre|| ||| }t ||j ||j dD] \}}}||||fqO|t|7}|t|| | 7}| dd \}}|d kr|j|df}tt ||j |d|j d}t|dkr|ddtur|dd}|t|d gf|fn|d kr|jstd |j |z t||j ||j d}Wnty}zt||j ||jpddd }~ww|j|df}|t||fnU|dkr!|d8}||vr ||\}}||d }t|g|f|j |dffg||d <n(|rI||}|d ur2t|d||d |j |dff}|t|f||<|d7}| }q,|t|kru|| ||d }t ||j ||j dD]\}}}||||fqf|S)#Parse the template from text input.rutf-8replacecSs"dd|D}|s dS|dS)NcSsg|]}|r|qSr$r$)r.gr$r$r%r0sz@NewTextTemplate._parse.._escape_repl..r)groups)morCr$r$r% _escape_replsz,NewTextTemplate._parse.._escape_replrr2includeNpythonzPython code blocks not allowedend) read isinstancesix text_typedecoder;sub enumerater9finditerspanr rrappendr3 splitlinesgrouprlistrrr"rr SyntaxErrorlinenorpopr get_directiver)r r!rstreamdirmapdepthoffsetrY _escape_subrEidxrDstartrJrkinddataposcommandvaluesuiteerr directive start_offset substreamclsr$r$r%_parses               zNewTextTemplate._parse)NNNNrFr)__name__ __module__ __qualname____doc__ DefDirective WhenDirectiveOtherwiseDirective ForDirective IfDirectiveChooseDirective WithDirective directives serializerr6r:rr'r<propertyrrnr$r$r$r%r ,s*M  r c@sXeZdZdZdefdefdefdefdefde fde fgZ d Z e d e jZd d Zd S)r aLegacy implementation of the old syntax text-based templates. This class is provided in a transition phase for backwards compatibility. New code should use the `NewTextTemplate` class and the improved syntax it provides. >>> tmpl = OldTextTemplate('''Dear $name, ... ... We have the following items for you: ... #for item in items ... * $item ... #end ... ... All the best, ... Foobar''') >>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None)) Dear Joe, We have the following items for you: * 1 * 2 * 3 All the best, Foobar rrrrrrrrzA(?:^[ \t]*(?r?r@rFNrJrH#z\#)rKrLrMrNrOrQr6rRrSr rrrTr3rUlstripsplitrZrrrstripr[rr?)r r!rr\r]r^r_rYrarDrbrJrrcrdrerjrfrgrkrlrmr$r$r%rnsd             zOldTextTemplate._parseN)rorprqrrrsrtrurvrwrxryrzr{r+r5 MULTILINEr6rnr$r$r$r%r s r )rrr+rM genshi.corergenshi.template.baserrrrrrgenshi.template.evalr genshi.template.directivesgenshi.template.interpolationr __all__ __docformat__r r rr$r$r$r%s     C`