
    w:i0                         d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZ ddlT ddlmZ g d	Zd
Z G d de          Z G d de          ZeZdS )a  Plain 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_type)TEXT)BadDirectiveErrorTemplateTemplateSyntaxErrorEXECINCLUDESUB)Suite)*)interpolate)NewTextTemplateOldTextTemplateTextTemplatezrestructuredtext enc                       e Zd ZdZdefdefdefdefdefde	fde
fgZd	Zd
ZdZ	 	 	 ddZd Zd Z eeed          Zd ZdS )r   a  Implementation 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,
    <BLANKLINE>
    <BLANKLINE>
    We have the following items for you:
    <BLANKLINE>
     * Item 1
    <BLANKLINE>
     * Item 2
    <BLANKLINE>
     * Item 3
    <BLANKLINE>
    <BLANKLINE>
    
    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,
    <BLANKLINE>
    We have the following items for you:
     * 1
     * 2
     * 3
    <BLANKLINE>
    
    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,
    <BLANKLINE>
    {# This is a comment #}
    We have the following items for you:
     * 1
     * 2
     * 3
    <BLANKLINE>
    
    :since: version 0.5
    defwhen	otherwiseforifchoosewithtextz/((?<!\\)%s\s*(\w+)\s*(.*?)\s*%s|(?<!\\)%s.*?%s)z \\\n|\\\r\n|\\(\\)|\\(%s)|\\(%s)NstrictFz{%z%}z{#z#}c	           	      J    || _         t          j        | ||||||           d S )N)filepathfilenameloaderencodinglookup)
delimitersr   __init__)	selfsourcer   r   r   r    r!   
allow_execdelimss	            L/var/www/html/trac/venv/lib/python3.11/site-packages/genshi/template/text.pyr#   zNewTextTemplate.__init__   sD     !$H!'(6	K 	K 	K 	K 	K 	K    c                     | j         S )N)_delims)r$   s    r(   _get_delimszNewTextTemplate._get_delims   s
    |r)   c           
      `   t          |          dk    rt          d          || _        t          j        | j        t          d |D                       z  t          j                  | _        t          j        | j	        t          d |d d d         D                       z            | _
        d S )N   z/delimiers tuple must have exactly four elementsc                 6    g | ]}t          j        |          S  reescape.0ds     r(   
<listcomp>z/NewTextTemplate._set_delims.<locals>.<listcomp>   s     ***aRYq\\***r)   c                 6    g | ]}t          j        |          S r0   r1   r4   s     r(   r7   z/NewTextTemplate._set_delims.<locals>.<listcomp>   s     ///aRYq\\///r)      )len
ValueErrorr+   r2   compile_DIRECTIVE_REtupleDOTALL_directive_re
_ESCAPE_RE
_escape_re)r$   r'   s     r(   _set_delimszNewTextTemplate._set_delims   s    v;;!NOOOZ(:U**6***>
 >
 )
9  *T_u//6##A#;///8
 8
 &
  r)   z    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           
      .   g }i }d}|                                 }t          |t                    s|                    |pdd          }d}d}| j        j        }d }	t          | j                            |                    D ]\  }
}|	                    d          \  }}||k    rt ||	|||                   }t          || j        || j                  D ]\  }}}|                    |||f           |t          |                                          z  }|t          |||                                                   z  }|                    dd          \  }}|d	k    r| j        |df}t%          t          || j        |d| j                            }t          |          dk    r#|d         d         t&          u r|d         d         }|                    t(          |d
g f|f           n<|dk    r| j        st-          d| j        |          	 t/          || j        || j                  }n5# t0          $ r(}t-          || j        ||j        pdz   dz
            d
}~ww xY w| j        |df}|                    t4          ||f           n|dk    rG|dz  }||v r=|                    |          \  }}||d
         }t8          |g|f| j        |dffg||d
<   nP|rN|                     |          }|t=          |          d||d
| j        |dff}|t          |          f||<   |dz  }|}|t          |          k     rP ||	||d
                   }t          || j        || j                  D ]\  }}}|                    |||f           |S )#Parse the template from text input.r   utf-8replace   c                 V    d |                                  D             }|sdS |d         S )Nc                     g | ]}||S r0   r0   )r5   gs     r(   r7   z@NewTextTemplate._parse.<locals>._escape_repl.<locals>.<listcomp>   s    222A2a222r)    r   )groups)morM   s     r(   _escape_replz,NewTextTemplate._parse.<locals>._escape_repl   s4    22222F r!9r)   r!   r9      includeNpythonzPython code blocks not allowedend)read
isinstancer   decoderB   sub	enumerater@   finditerspanr   r   r!   appendr:   
splitlinesgroupr   listr   r	   r&   r   r   SyntaxErrorlinenor   popr
   get_directiver   )r$   r%   r    streamdirmapdepthoffsetra   _escape_subrO   idxrN   startrT   r   kinddataposcommandvaluesuiteerr	directivestart_offset	substreamclss                             r(   _parsezNewTextTemplate._parse   sI   &),, 	C]]8#6w	BBFo)	 	 	 !!3!<!<V!D!DEE 1	 1	GCJE3v~~"{<u1EFF'24:>+(G (G (G 5 5OD$MM4s"34444#doo//000c&s+6688999FXXa^^NGU)##}fa0[vq04= = = > >u::??uQx{d':':!!HQKEwb(93?@@@@H$$ E-.N.2mVE E EN!%)-6 6 6EE" N N N-c4=.4
a.H1.LN N NN }fa0tUC01111E!!
F??.4jj.?.?+I| &|}} 5I.1YK3K/3}fa.H.J -KF<==)  ((11;+G444sE4$-1KK	!*CKK 8u
FFCKK;|VFGG_==D#.tT]F6:k$C $C $C 1 1dCtT3/0000s   H44
I&>#I!!I&)NNNNr   Fr   )__name__
__module____qualname____doc__DefDirectiveWhenDirectiveOtherwiseDirectiveForDirectiveIfDirectiveChooseDirectiveWithDirective
directives
serializerr=   rA   r#   r,   rC   propertyr"   rv   r0   r)   r(   r   r   +   s        L LZ ,'=) 23,'%_-=)+J JFM4JDH<A0K K K K  	 	 	 +{ 5 	 	JL L L L Lr)   r   c                   v    e Zd ZdZdefdefdefdefdefde	fde
fgZd	Z ej        d
ej                  Zd ZdS )r   a  Legacy 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,
    <BLANKLINE>
    We have the following items for you:
     * 1
     * 2
     * 3
    <BLANKLINE>
    All the best,
    Foobar
    r   r   r   r   r   r   r   r   zA(?:^[ \t]*(?<!\\)#(end).*\n?)|(?:^[ \t]*(?<!\\)#((?:\w+|#).*)\n?)c                    g }i }d}|                                 }t          |t                    s|                    |pdd          }d}d}t	          | j                            |                    D ]\  }}	|	                                \  }
}|
|k    rj|||
         }t          || j	        || j
                  D ]\  }}}|                    |||f           |t          |                                          z  }||
|                                         dd         }|t          |                                          z  }|                    dd          }t          |          dk    r|\  }}n
|d         d}}|dk    rG|dz  }||v r=|                    |          \  }}||d         }t"          |g|f| j	        |dffg||d<   n|dk    r=| j        |df}|                    t&          |                                dg f|f           nT|d	k    rN|                     |          }|t-          |          d||d| j	        |dff}|t          |          f||<   |dz  }|}|t          |          k     rZ||d                             d
d	          }t          || j	        || j
                  D ]\  }}}|                    |||f           |S )rE   r   rF   rG   rH   rP   NrT   rR   #z\#)rU   rV   r   rW   rY   r=   rZ   r[   r   r   r!   r\   r:   r]   lstripsplitrb   r
   r   r	   striprc   r   rG   )r$   r%   r    rd   re   rf   rg   ra   ri   rN   rj   rT   r   rk   rl   rm   rr   rn   ro   rs   rt   ru   s                         r(   rv   zOldTextTemplate._parse  s2   &),, 	C]]8#6w	BBF !3!<!<V!D!DEE #	 #	GCJE3v~~fUl+'24:>+(G (G (G 5 5OD$MM4s"34444#doo//000%)$++--abb1Dc$//++,,,F

4++I9~~!!!*!*1t%
F??.4jj.?.?+I| &|}} 5I.1YK3K/3}fa.H.J -KF<==)I%%}fa0wb(A3GHHHHC((11;+G444sE4$-1KK	!*CKK 8u
FFCKK&''?**5#66D#.tT]F6:k$C $C $C 1 1dCtT3/0000r)   N)rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r2   r<   	MULTILINEr=   rv   r0   r)   r(   r   r      s         0 ,'=) 23,'%_-=)+J JBJ  F!|- -M7 7 7 7 7r)   r   )rz   r2   genshi.compatr   genshi.corer   genshi.template.baser   r   r   r   r	   r
   genshi.template.evalr   genshi.template.directivesgenshi.template.interpolationr   __all____docformat__r   r   r   r0   r)   r(   <module>r      sO    
			 # # # # # #      I I I I I I I I I I I I I I I I & & & & & & ( ( ( ( 5 5 5 5 5 5
@
@
@%    h   D] ] ] ] ]h ] ] ]@ r)   