
    w:iA                        d Z ddlmZ ddl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mZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZ g dZd	Zd
 Z G d de           Z! G d de"          Z#d Z$ G d dej%        e"          Z%ddZ&d Z'dS )zOSupport for constructing markup streams from files, strings, or other
sources.
    )chainN)expat)html_entitieshtml_parser	text_typeunichrStringIOBytesIO)AttrsQNameStreamstripentities)STARTENDXML_DECLDOCTYPETEXTSTART_NSEND_NSSTART_CDATA	END_CDATAPICOMMENT)ET
ParseError	XMLParserXML
HTMLParserHTMLzrestructuredtext enc              #   |  K   t          | j                            d                    }t          d |                                 D                       }t
          ||fdfV  | j        rt          | j        dfV  | D ]}t          |          D ]}|V  t          |dfV  | j
        rt          | j
        dfV  dS dS )zConvert a given ElementTree element to a markup stream.
    
    :param element: an ElementTree element
    :return: a markup stream
    {c                 \    g | ])\  }}t          |                    d                     |f*S )r!   )r   lstrip).0attrvalues      D/var/www/html/trac/venv/lib/python3.11/site-packages/genshi/input.py
<listcomp>zET.<locals>.<listcomp>(   sE     7 7 7"tU $++c**++U3 7 7 7    Nr+   N)r   tagr#   r   itemsr   textr   r   r   tail)elementtag_nameattrschilditems        r'   r   r   !   s      W['',,--H 7 7&-mmoo7 7 7 8 8E (E"N
2222| 1GL.0000  uII 	 	DJJJJ	
x
''''| 1GL.0000001 1r)   c                       e Zd ZdZddZdS )r   zWException raised when fatal syntax errors are found in the input being
    parsed.
    Nr+   c                     || _         |r|d|z   z  }t                              | |           |pd| _        || _        || _        dS )a:  Exception initializer.
        
        :param message: the error message from the parser
        :param filename: the path to the file that was parsed
        :param lineno: the number of the line on which the error was encountered
        :param offset: the column number where the error was encountered
        z, in z<string>N)msg	Exception__init__filenamelinenooffset)selfmessager:   r;   r<   s        r'   r9   zParseError.__init__;   sV      	*w))G4))) .Jr)   r*   )__name__
__module____qualname____doc__r9    r)   r'   r   r   6   s2              r)   r   c                      e Zd ZdZd ej                                        D             Zd                    e          	                    d          Z
ddZd Zd Zd	 Zdd
Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )r   a  Generator-based XML parser based on roughly equivalent code in
    Kid/ElementTree.
    
    The parsing is initiated by iterating over the parser object:
    
    >>> parser = XMLParser(StringIO('<root id="2"><child>Foo</child></root>'))
    >>> for kind, data, pos in parser:
    ...     print('%s %s' % (kind, data))
    START (QName('root'), Attrs([(QName('id'), '2')]))
    START (QName('child'), Attrs())
    TEXT Foo
    END child
    END root
    c                 "    g | ]\  }}d ||fz  S )z<!ENTITY %s "&#%d;">rC   r$   namer&   s      r'   r(   zXMLParser.<listcomp>\   s2     9 9 9kdE)T5M9 9 9 9r)   
utf-8Nc                 P   || _         || _        t          j        |d          }d|_        t          |d          rd|_        d|_        | j        |_	        | j
        |_        | j        |_        | j        |_        | j        |_        | j        |_        | j        |_        | j        |_        | j        |_        | j        |_        | j        |_        | j        |_        |                     t          j!                   |"                                 | j#        |_$        || _        g | _%        dS )a  Initialize the parser for the given XML input.
        
        :param source: the XML text as a file-like object
        :param filename: the name of the file, if appropriate
        :param encoding: the encoding of the file; if not specified, the
                         encoding is assumed to be ASCII, UTF-8, or UTF-16, or
                         whatever the encoding specified in the XML declaration
                         (if any)
        }Treturns_unicodeN)&sourcer:   r   ParserCreatebuffer_texthasattrrL   ordered_attributes_handle_startStartElementHandler_handle_endEndElementHandler_handle_dataCharacterDataHandler_handle_doctypeStartDoctypeDeclHandler_handle_start_nsStartNamespaceDeclHandler_handle_end_nsEndNamespaceDeclHandler_handle_start_cdataStartCdataSectionHandler_handle_end_cdataEndCdataSectionHandler
_handle_piProcessingInstructionHandler_handle_xml_declXmlDeclHandler_handle_commentCommentHandler_handle_otherDefaultHandlerSetParamEntityParsingXML_PARAM_ENTITY_PARSING_ALWAYSUseForeignDTD_build_foreignExternalEntityRefHandler_queue)r=   rM   r:   encodingparsers        r'   r9   zXMLParser.__init__`   s      #Hc22!6,-- 	*%)F"$(!%)%7"#'#3 &*&7#)-)=&+/+@()-)<&*.*B'(,(>%.2o+ $ 5 $ 4 !% 2$$U%JKKK*.*='
r)   c                 l      fd}t           |                                          t                    S )zGenerator that parses the XML source, yielding markup events.
        
        :return: a markup event stream
        :raises ParseError: if the XML text is not well formed
        c               3   `  K   	 d} d}	 |st          j                  dk    rj                            |           }|s0t	          d          rj                            dd           `d}nEt          |t                    r|	                    d          }j                            |d           |st          j                  dk    j        D ]}|V  g _        |rd S # t
          j
        $ r5}t          |          }t          |j        |j        |j                  d }~ww xY w)	N   F   r   r    TrI   )lenro   rM   readrP   r   Parse
isinstancer   encode
ExpatErrorstrr   r:   r;   r<   )bufsizedonedataeventer7   r=   s         r'   	_generatez"XMLParser.parse.<locals>._generate   sh     I"" 
:s4;'7'71'<'<#{//88# :&tW55 / $
 0 0T : : :$(J#'DD)$	:: <'+{{7';'; J,,T5999 # 
:s4;'7'71'<'< "& $ $#"$DK !" # I I I!ff dmQXqxHHHIs   C!C) (C) )D-80D((D-r   filter	_coalescer=   r   s   ` r'   parsezXMLParser.parse   sE    	I 	I 	I 	I 	I0 iikk"")))444r)   c                 D    t          |                                           S Niterr   r=   s    r'   __iter__zXMLParser.__iter__       DJJLL!!!r)   c                     | j                             |          }|                    t          | j                             dS )Nru   )r   ExternalEntityParserCreate	ParseFiler
   _external_dtd)r=   contextbasesysidpubidrq   s         r'   rm   zXMLParser._build_foreign   s;    66w??!344555qr)   c                 @   ||                                  }|t          u rad|v r2|                                }|d         t          |          z
  dz   }d}n |d         }|d         t          |          z
  }|d         ||f}| j                            |||f           d S )NrH   ru   r+      r   )_getposr   
splitlinesrw   ro   append)r=   kindr   poslinesr;   r<   s          r'   _enqueuezXMLParser._enqueue   s    ;,,..C4<< t||))Q#e**,q0QQ#d))+q666*CD$,-----r)   c                     | j         ddfS )Nr+   )r:   r   s    r'   _getpos_unknownzXMLParser._getpos_unknown   s    r2&&r)   c                 >    | j         | j        j        | j        j        fS r   )r:   r   CurrentLineNumberCurrentColumnNumberr   s    r'   r   zXMLParser._getpos   s     tz;
.0 	0r)   c                     t          d t          t          |          gdz   D                       }|                     t          t          |          |f           d S )Nc                 6    g | ]\  }}t          |          |fS rC   )r   rF   s      r'   r(   z+XMLParser._handle_start.<locals>.<listcomp>   s3     1 1 1edU+ 1 1 1r)   r   )r   zipr   r   r   r   )r=   r,   attribr2   s       r'   rR   zXMLParser._handle_start   sd     1 1T&\\NQ./1 1 1 2 2eeCjj%011111r)   c                 V    |                      t          t          |                     d S r   )r   r   r   )r=   r,   s     r'   rT   zXMLParser._handle_end   s"    c5::&&&&&r)   c                 <    |                      t          |           d S r   r   r   r=   r.   s     r'   rV   zXMLParser._handle_data       dD!!!!!r)   c                 B    |                      t          |||f           d S r   )r   r   )r=   versionrp   
standalones       r'   rd   zXMLParser._handle_xml_decl   s#    h(J ?@@@@@r)   c                 B    |                      t          |||f           d S r   )r   r   )r=   rG   r   r   has_internal_subsets        r'   rX   zXMLParser._handle_doctype   s#    geU344444r)   c                 D    |                      t          |pd|f           d S Nrv   )r   r   )r=   prefixuris      r'   rZ   zXMLParser._handle_start_ns   s%    h2s 344444r)   c                 @    |                      t          |pd           d S r   )r   r   )r=   r   s     r'   r\   zXMLParser._handle_end_ns   s     ffl+++++r)   c                 :    |                      t                     d S r   )r   r   r   s    r'   r^   zXMLParser._handle_start_cdata   s    k"""""r)   c                 :    |                      t                     d S r   )r   r   r   s    r'   r`   zXMLParser._handle_end_cdata   s    i     r)   c                 @    |                      t          ||f           d S r   )r   r   )r=   targetr   s      r'   rb   zXMLParser._handle_pi   s     b64.)))))r)   c                 <    |                      t          |           d S r   r   r   r   s     r'   rf   zXMLParser._handle_comment       gt$$$$$r)   c                    |                     d          r	 t          t          j        |dd                            }|                     t
          |           d S # t          $ rY |                                 \  }}}t          j	        d|||fz            }t          j
        j        |_        ||_        ||_        |w xY wd S )N&ru   r+   z)undefined entity "%s": line %d, column %d)
startswithr   r   name2codepointr   r   KeyErrorr   r   errorerrorsXML_ERROR_UNDEFINED_ENTITYcoder;   r<   )r=   r.   r:   r;   r<   r   s         r'   rh   zXMLParser._handle_other   s    ??3 	
m:4":FGGdD)))))   +/<<>>(&&$O'+VV&<%= > >"\D
%%	 	s   AA A#B>NN)r?   r@   rA   rB   r   r   r-   _entitydefsjoinr{   r   r9   r   r   rm   r   r   r   rR   rT   rV   rd   rX   rZ   r\   r^   r`   rb   rf   rh   rC   r)   r'   r   r   L   s        9 9 /55779 9 9KJJ{++227;;M) ) ) )V5 5 5@" " "  
. . . .&' ' '0 0 02 2 2
' ' '" " "A A A5 5 55 5 5, , ,# # #! ! !* * *% % %    r)   r   c           	      n    t          t          t          t          |                                         S )a?  Parse the given XML source and return a markup stream.
    
    Unlike with `XMLParser`, the returned stream is reusable, meaning it can be
    iterated over multiple times:
    
    >>> xml = XML('<doc><elem>Foo</elem><elem>Bar</elem></doc>')
    >>> print(xml)
    <doc><elem>Foo</elem><elem>Bar</elem></doc>
    >>> print(xml.select('elem'))
    <elem>Foo</elem><elem>Bar</elem>
    >>> print(xml.select('elem/text()'))
    FooBar
    
    :param text: the XML source
    :return: the parsed XML event stream
    :raises ParseError: if the XML text is not well-formed
    )r   listr   r	   )r.   s    r'   r   r      s(    $ $y$0011222r)   c                   x    e Zd ZdZ eg d          ZddZd Zd ZddZ	d Z
d	 Zd
 Zd Zd Zd Zd Zd ZdS )r   aU  Parser for HTML input based on the Python `HTMLParser` module.
    
    This class provides the same interface for generating stream events as
    `XMLParser`, and attempts to automatically balance tags.
    
    The parsing is initiated by iterating over the parser object:
    
    >>> parser = HTMLParser(BytesIO(u'<UL compact><LI>Foo</UL>'.encode('utf-8')), encoding='utf-8')
    >>> for kind, data, pos in parser:
    ...     print('%s %s' % (kind, data))
    START (QName('ul'), Attrs([(QName('compact'), 'compact')]))
    START (QName('li'), Attrs())
    TEXT Foo
    END li
    END ul
    )arear   basefontbrcolframehrimginputisindexlinkmetaparamNc                     t           j                            |            || _        || _        || _        g | _        g | _        dS )a  Initialize the parser for the given HTML input.
        
        :param source: the HTML text as a file-like object
        :param filename: the name of the file, if known
        :param filename: encoding of the file; ignored if the input is unicode
        N)r   r   r9   rM   r:   rp   ro   
_open_tags)r=   rM   r:   rp   s       r'   r9   zHTMLParser.__init__+  sB     	''---  r)   c                 l      fd}t           |                                          t                    S )zGenerator that parses the HTML source, yielding markup events.
        
        :return: a markup event stream
        :raises ParseError: if the HTML text is not well formed
        c               3     K   j         r*t          j        j                   }  | j                  }nj        }	 d}d}	 |st	          j                  dk    r|                    |          }|s                                 d}n9t          |t                    st          d                              |           |st	          j                  dk    j        D ]\  }}}|||fV  g _        |r;j        }|                                 |D ]}t          t          |          |fV  d S # t           $ r)}	t#          |	          }
t%          |
j                  d }	~	ww xY w)Nrt   Fru   r   Tz0source returned bytes, but no encoding specified)rp   codecs	getreaderrM   rw   ro   rx   closerz   r   UnicodeErrorfeedr   reverser   r   r8   r}   r   r:   )readerrM   r~   r   r   r   r   	open_tagsr,   r   r7   r=   s              r'   r   z#HTMLParser.parse.<locals>._generate?  s     } %)$-88,,5"" ,s4;'7'71'<'<%{{733# , JJLLL#'DD#-dI#>#> g&23e&f&f f IIdOOO # ,s4;'7'71'<'< ,0; . .dC"D#o----"$DK $(O	!))+++#, 7 7C"%uSzz3"66666%&  5 5 5
 !ff dm4445s   C7D7 6D7 7
E*$E%%E*r   r   s   ` r'   r   zHTMLParser.parse9  sA    "	5 "	5 "	5 "	5 "	5F iikk"")))444r)   c                 D    t          |                                           S r   r   r   s    r'   r   zHTMLParser.__iter__d  r   r)   c                 l    ||                                  }| j                            |||f           d S r   )r   ro   r   )r=   r   r   r   s       r'   r   zHTMLParser._enqueueg  s7    ;,,..CD$,-----r)   c                 D    |                                  \  }}| j        ||fS r   )getposr:   )r=   r;   columns      r'   r   zHTMLParser._getposl  s"    vv..r)   c                    g }|D ]:\  }}||}|                     t          |          t          |          f           ;|                     t          t          |          t          |          f           || j        v r*|                     t          t          |                     d S | j                             |           d S r   )	r   r   r   r   r   r   _EMPTY_ELEMSr   r   )r=   r,   r   fixed_attribrG   r&   s         r'   handle_starttagzHTMLParser.handle_starttagp  s    ! 	E 	EKD%}tmE.B.B CDDDDeeCjj%*=*=>???$###MM#uSzz*****O""3'''''r)   c                    || j         vr}| j        rx| j                                        }|                     t          t          |                     |                                |                                k    rd S | j        td S d S d S r   )r   r   popr   r   r   lower)r=   r,   open_tags      r'   handle_endtagzHTMLParser.handle_endtag}  s    d'''/ ?..00c5??333>>##syy{{22E	 /    (' r)   c                 <    |                      t          |           d S r   r   r   s     r'   handle_datazHTMLParser.handle_data  r   r)   c                    |                                                     d          r&t          t          |dd          d                    }nt          t          |                    }|                     t
          |           d S )Nxru      )r   r   r   intr   r   r=   rG   r.   s      r'   handle_charrefzHTMLParser.handle_charref  sn    ::<<""3'' 	%#d122h++,,DD#d))$$DdD!!!!!r)   c                     	 t          t          j        |                   }n# t          $ r d|z  }Y nw xY w|                     t
          |           d S )Nz&%s;)r   r   r   r   r   r   r   s      r'   handle_entityrefzHTMLParser.handle_entityref  s`    	!-6t<==DD 	! 	! 	!D=DDD	!dD!!!!!s   " 44c                 $   |                     d          r
|d d         }	 |                    d d          \  }}n# t          $ r |}d}Y nw xY w|                     t          |                                |                                f           d S )N?r+   ru   rv   )endswithsplit
ValueErrorr   r   strip)r=   r   r   s      r'   	handle_pizHTMLParser.handle_pi  s    == 	9D	::dA..LFDD 	 	 	FDDD	 	b6<<>>4::<<899999s   ; AAc                 <    |                      t          |           d S r   r   r   s     r'   handle_commentzHTMLParser.handle_comment  r   r)   r   r   )r?   r@   rA   rB   	frozensetr   r9   r   r   r   r   r   r   r   r   r   r  r  rC   r)   r'   r   r     s         " 9 ' ' ' ( (L   )5 )5 )5V" " ". . . .
/ / /( ( (  " " "" " "" " "	: 	: 	:% % % % %r)   r   c           	         t          | t                    r8t          t          t	          t          |           d                              S t          t          t	          t          |           |                              S )aM  Parse the given HTML source and return a markup stream.
    
    Unlike with `HTMLParser`, the returned stream is reusable, meaning it can be
    iterated over multiple times:
    
    >>> html = HTML('<body><h1>Foo</h1></body>', encoding='utf-8')
    >>> print(html)
    <body><h1>Foo</h1></body>
    >>> print(html.select('h1'))
    <h1>Foo</h1>
    >>> print(html.select('h1/text()'))
    Foo
    
    :param text: the HTML source
    :return: the parsed XML event stream
    :raises ParseError: if the HTML text is not well-formed, and error recovery
                        fails
    N)rp   )rz   r   r   r   r   r	   r
   )r.   rp   s     r'   r   r     sk    & $	"" G d:htnntDDDEEFFF$z'$--(CCCDDEEEr)   c              #      K   g }d}t          | dg          D ]Z\  }}}|t          u r|                    |           ||})|r&t          d                    |          |fV  |dd= d}|r|||fV  [dS )z3Coalesces adjacent TEXT events into a single event.N)NNNrv   )r   r   r   r   )streamtextbuftextposr   r   r   s         r'   r   r     s      GG *<)=>> & &dC4<<NN4    BGGG,,g5555AAAJ &D#o%%%& &r)   r   )(rB   	itertoolsr   r   xml.parsersr   genshi.compatr   r   r   r   r	   r
   genshi.corer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __all____docformat__r   r8   r   objectr   r   r   r   r   rC   r)   r'   <module>r     sE                , , , , , , , , , , , , , , , , ; ; ; ; ; ; ; ; ; ; ; ;D D D D D D D D D D D D D D D D D D D D D D D D D D I
H
H%1 1 1*       ,q q q q q q q qh3 3 3*M% M% M% M% M%' M% M% M%`F F F F6& & & & &r)   