ó
}c‹Ac           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z e
 j Z e	 j d ƒ Z d e j f d „  ƒ  YZ d d d „  ƒ  YZ d d d	 „  ƒ  YZ d
 „  Z d e j f d „  ƒ  YZ d d „ Z d S(   sû   simpleTALUtils

		Copyright (c) 2004 Colin Stewart (http://www.owlfish.com/)
		All rights reserved.
		
		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions
		are met:
		1. Redistributions of source code must retain the above copyright
		   notice, this list of conditions and the following disclaimer.
		2. Redistributions in binary form must reproduce the above copyright
		   notice, this list of conditions and the following disclaimer in the
		   documentation and/or other materials provided with the distribution.
		3. The name of the author may not be used to endorse or promote products
		   derived from this software without specific prior written permission.
		
		THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
		IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
		OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
		IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
		INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
		NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
		DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
		THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
		THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
		
		If you make any bug fixes or feature enhancements please let me know!
		
		This module is holds utilities that make using SimpleTAL easier. 
		Initially this is just the HTMLStructureCleaner class, used to clean
		up HTML that can then be used as 'structure' content.
		
		Module Dependencies: None
iÿÿÿÿNs   \&\S+?;t   HTMLStructureCleanerc           B   sG   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s•    A helper class that takes HTML content and parses it, so converting
			any stray '&', '<', or '>' symbols into their respective entity references.
	c         C   sá   t  | ƒ t  d ƒ k rG t j | ƒ d } t j | | ƒ d ƒ } n^ t  | ƒ t  d ƒ k rq t j | ƒ } n4 | d k	 rŸ t j | ƒ d } | | ƒ } n | } t j d ƒ |  _ |  j | j ƒ  ƒ |  j ƒ  |  j j	 ƒ  S(   s¹   Takes the HTML content given, parses it, and converts stray markup.
				The content can be either:
					 - A unicode string, in which case the encoding parameter is not required
					 - An ordinary string, in which case the encoding will be used
					 - A file-like object, in which case the encoding will be used if present
				
				The method returns a unicode string which is suitable for addition to a
				simpleTALES.Context object.
		t    i   i    u    i   N(
   t   typet   codecst   lookupt   StringIOt   Nonet
   outputFilet   feedt   readt   closet   getvalue(   t   selft   contentt   encodingt	   convertert   filet   converterStream(    (    s   lib/simpletal/simpleTALUtils.pyt   clean1   s    

c         C   s   |  j  j t | | ƒ ƒ d  S(   N(   R   t   writet	   tagAsText(   R   t   tagt
   attributes(    (    s   lib/simpletal/simpleTALUtils.pyt   unknown_starttagN   s    c         C   s   |  j  j d | d ƒ d  S(   Ns   </t   >(   R   R   (   R   R   (    (    s   lib/simpletal/simpleTALUtils.pyt   unknown_endtagQ   s    c         C   s   |  j  j t j | ƒ ƒ d  S(   N(   R   R   t   cgit   escape(   R   t   data(    (    s   lib/simpletal/simpleTALUtils.pyt   handle_dataT   s    c         C   s   |  j  j d | ƒ d  S(   Nu   &#%s;(   R   R   (   R   t   ref(    (    s   lib/simpletal/simpleTALUtils.pyt   handle_charrefW   s    c         C   s   |  j  j d | ƒ d  S(   Nu   &%s;(   R   R   (   R   R   (    (    s   lib/simpletal/simpleTALUtils.pyt   handle_entityrefZ   s    N(
   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R    (    (    (    s   lib/simpletal/simpleTALUtils.pyR    -   s   				t   FastStringOutputc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sx    A very simple StringIO replacement that only provides write() and getvalue()
		and is around 6% faster than StringIO.
	c         C   s   g  |  _  d  S(   N(   R   (   R   (    (    s   lib/simpletal/simpleTALUtils.pyt   __init__b   s    c         C   s   |  j  j | ƒ d  S(   N(   R   t   append(   R   R   (    (    s   lib/simpletal/simpleTALUtils.pyR   e   s    c         C   s   d j  |  j ƒ S(   NR   (   t   joinR   (   R   (    (    s   lib/simpletal/simpleTALUtils.pyR   h   s    (   R!   R"   R#   R%   R   R   (    (    (    s   lib/simpletal/simpleTALUtils.pyR$   ^   s   		t   TemplateCachec           B   s8   e  Z d  Z d „  Z d d „ Z d „  Z d d „ Z RS(   së    A TemplateCache is a multi-thread safe object that caches compiled templates.
			This cache only works with file based templates, the ctime of the file is 
			checked on each hit, if the file has changed the template is re-compiled.
	c         C   s.   i  |  _  t j ƒ  |  _ d |  _ d |  _ d  S(   Ni    (   t   templateCachet	   threadingt   Lockt	   cacheLockt   hitst   misses(   R   (    (    s   lib/simpletal/simpleTALUtils.pyR%   p   s    		s
   ISO-8859-1c         C   sm   |  j  j | ƒ r] |  j  | \ } } t j | ƒ t j } | | k r] |  j d 7_ | Sn  |  j | | ƒ S(   sa   Name should be the path of a template file.  If the path ends in 'xml' it is treated
			as an XML Template, otherwise it's treated as an HTML Template.  If the template file
			has changed since the last cache it will be re-compiled.
			
			inputEncoding is only used for HTML templates, and should be the encoding that the template
			is stored in.
		i   (   R)   t   has_keyt   ost   statt   ST_MTIMER-   t   _cacheTemplate_(   R   t   namet   inputEncodingt   templatet   oldctimet   ctime(    (    s   lib/simpletal/simpleTALUtils.pyt   getTemplatev   s    c         C   ss   |  j  j | ƒ r] |  j  | \ } } t j | ƒ t j } | | k r] |  j d 7_ | Sn  |  j | d d d ƒS(   s6    Name should be the path of an XML template file.  
		i   t   xmlTemplateN(   R)   R/   R0   R1   R2   R-   R3   R   (   R   R4   R6   R7   R8   (    (    s   lib/simpletal/simpleTALUtils.pyt   getXMLTemplateˆ   s    i    c         C   s6  |  j  j ƒ  yï t | d ƒ } | r7 t j | ƒ } nˆ | j ƒ  } | j d ƒ | d d k s› | j ƒ  d  d k s› | d  d k r­ | j d	 ƒ d
 k r­ t j | ƒ } n t j	 | | ƒ } | j
 ƒ  | t j | ƒ t j f |  j | <|  j d 7_ Wn& t k
 r$} |  j  j ƒ  | ‚ n X|  j  j ƒ  | S(   Nt   ri    iýÿÿÿt   xmli   s   <?xmli	   s	   <!DOCTYPEt   XHTMLiÿÿÿÿi   (   R,   t   acquiret   opent	   simpleTALt   compileXMLTemplatet   readlinet   seekt   stript   findt   compileHTMLTemplateR
   R0   R1   R2   R)   R.   t	   Exceptiont   release(   R   R4   R5   R:   t   tempFileR6   t	   firstlinet   e(    (    s   lib/simpletal/simpleTALUtils.pyR3   •   s$    K
#
(   R!   R"   R#   R%   R9   R;   R3   (    (    (    s   lib/simpletal/simpleTALUtils.pyR(   k   s
   		c         C   sx   d |  } x] | D]U \ } } t  j | ƒ d  k	 rI | d | | f 7} q | d | t j | ƒ f 7} q W| d 7} | S(   Nt   <s    %s="%s"R   (   t   ESCAPED_TEXT_REGEXt   searchR   R   R   (   R   t   attst   resultR4   t   value(    (    s   lib/simpletal/simpleTALUtils.pyR   ®   s    
!
t   MacroExpansionInterpreterc           B   s>   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s&  t  j j |  ƒ g  |  _ |  j |  j t  j <|  j |  j t  j <|  j |  j t  j <|  j |  j t  j	 <|  j |  j t  j
 <|  j |  j t  j <|  j |  j t  j <|  j |  j t  j <|  j |  j t  j <|  j |  j t  j <|  j |  j t  j <|  j |  j t  j <|  j |  j t  j <d  |  _ d  |  _ d  S(   N(   RA   t   TemplateInterpreterR%   t   macroStateStackt   cmdNoOpt   commandHandlert
   TAL_DEFINEt   TAL_CONDITIONt
   TAL_REPEATt   TAL_CONTENTt   TAL_ATTRIBUTESt   TAL_OMITTAGt   cmdStartScopet   TAL_START_SCOPEt	   cmdOutputt
   TAL_OUTPUTt   cmdOutputStartTagt   TAL_STARTTAGt   cmdEndTagEndScopet   TAL_ENDTAG_ENDSCOPEt   cmdUseMacrot   METAL_USE_MACROt   cmdDefineSlott   METAL_DEFINE_SLOTt   TAL_NOOPR   t   inMacrot   macroArg(   R   (    (    s   lib/simpletal/simpleTALUtils.pyR%   º   s"    		c         C   s&   |  j  j ƒ  |  _ t j j |  ƒ d  S(   N(   RU   t   popRk   RA   RT   t
   popProgram(   R   (    (    s   lib/simpletal/simpleTALUtils.pyRn   Ñ   s    c         C   s'   |  j  j |  j ƒ t j j |  ƒ d  S(   N(   RU   R&   Rk   RA   RT   t   pushProgram(   R   (    (    s   lib/simpletal/simpleTALUtils.pyRo   Õ   s    c         C   s¾   g  } x‰ |  j  D]~ } |  j d  k	 rN | d d k rN | j d |  j f ƒ q |  j r | d d k r | j d | d f ƒ q | j | ƒ q Wd  |  _ | |  _ t j j |  | | ƒ d  S(   Ni    s   metal:define-macros   metal:use-macros   metal:define-slots   metal:fill-sloti   (	   t   originalAttributesRl   R   R&   Rk   t   currentAttributesRA   RT   Rb   (   R   t   commandt   argst   newAttst   att(    (    s   lib/simpletal/simpleTALUtils.pyRb   Ù   s    		c         C   sB   t  j j |  | | ƒ |  j d  k	 r> d |  _ | d |  _ n  d  S(   Ni   i    (   RA   RT   Rf   t
   tagContentR   Rk   Rl   (   R   Rr   Rs   (    (    s   lib/simpletal/simpleTALUtils.pyRf   æ   s    	c         C   s^  |  j  d  k	 r•|  j  \ } } | rù t | t j ƒ rx |  j ƒ  | j |  j |  j |  ƒ |  j	 ƒ  i  |  _
 d |  _ q’t | ƒ t d ƒ k r£ |  j j | ƒ q’t | ƒ t d ƒ k r× |  j j t | d ƒ ƒ q’|  j j t t | ƒ d ƒ ƒ q•t | ƒ t d ƒ k r-|  j j t j | ƒ ƒ q•t | ƒ t d ƒ k rj|  j j t j t | d ƒ ƒ ƒ q•|  j j t j t t | ƒ d ƒ ƒ ƒ n  |  j rÈ| d rÈ|  j j d | d d ƒ n  |  j d  k	 rç|  j |  _ d  S|  j r |  j j ƒ  n  |  j j ƒ  \
 |  _ |  _ |  _ |  _ |  _ |  _ |  _ |  _ |  _  |  _ |  j d 7_ d  S(   Ni    u    R   t   asciii   s   </R   (   Rv   R   t
   isinstanceRA   t   TemplateRo   t   expandInlinet   contextR   Rn   t   slotParametersRk   R   R   t   unicodet   strR   R   t	   outputTagt
   movePCBackt   programCountert   localVarsDefinedt	   popLocalst
   scopeStackRm   t   movePCForwardRp   Rq   t   repeatVariablet   repeatIndext   repeatSequence(   R   Rr   Rs   t   contentTypet	   resultVal(    (    s   lib/simpletal/simpleTALUtils.pyRd   í   s8    

	"%+	K(   R!   R"   R%   Rn   Ro   Rb   Rf   Rd   (    (    (    s   lib/simpletal/simpleTALUtils.pyRS   ¹   s   					s
   ISO-8859-1c         C   sn   t  j  ƒ  } t ƒ  } | j |  | ƒ | j |  | d | d | ƒ| j ƒ  } t j | ƒ d } | | ƒ d S(   Nt   outputEncodingt   interpreteri    (   R   RS   t
   initialiset   expandR   R   R   (   R{   R6   R‹   t   outt   interpRQ   t	   reencoder(    (    s   lib/simpletal/simpleTALUtils.pyt   ExpandMacros  s    	(    (    (   R#   R   R0   R1   R*   t   sysR   t   sgmllibR   t   ret	   simpletalRA   t   __version__t   compileRN   t
   SGMLParserR    R$   R(   R   RT   RS   R’   (    (    (    s   lib/simpletal/simpleTALUtils.pyt   <module>#   s   l	1C	`