a
    ÷£BeG:  ã                   @   sœ   d Z dZdgZddlmZ ddlZddlZddlmZm	Z	m
Z
mZmZ ddlmZmZ ddlmZmZmZmZmZ d	ZG d
d„ deeƒZG dd„ deƒZdS )zCUse the HTMLParser library to parse HTML files that aren't too bad.ÚMITÚHTMLParserTreeBuilderé    )Ú
HTMLParserN)ÚCDataÚCommentÚDeclarationÚDoctypeÚProcessingInstruction)ÚEntitySubstitutionÚUnicodeDammit)ÚDetectsXMLParsedAsHTMLÚParserRejectedMarkupÚHTMLÚHTMLTreeBuilderÚSTRICTzhtml.parserc                   @   s|   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zddd„Z	ddd„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) ÚBeautifulSoupHTMLParserz¸A subclass of the Python standard library's HTMLParser class, which
    listens for HTMLParser events and translates them into calls
    to Beautiful Soup's tree construction API.
    ÚignoreÚreplacec                 O   s:   |  d| j¡| _tj| g|¢R i |¤Ž g | _|  ¡  dS )a  Constructor.

        :param on_duplicate_attribute: A strategy for what to do if a
            tag includes the same attribute more than once. Accepted
            values are: REPLACE (replace earlier values with later
            ones, the default), IGNORE (keep the earliest value
            encountered), or a callable. A callable must take three
            arguments: the dictionary of attributes already processed,
            the name of the duplicate attribute, and the most recent value
            encountered.           
        Úon_duplicate_attributeN)ÚpopÚREPLACEr   r   Ú__init__Úalready_closed_empty_elementZ_initialize_xml_detector)ÚselfÚargsÚkwargs© r   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/bs4/builder/_htmlparser.pyr   .   s    ÿ	z BeautifulSoupHTMLParser.__init__c                 C   s   t |ƒ‚d S )N)r   )r   Úmessager   r   r   ÚerrorJ   s    zBeautifulSoupHTMLParser.errorc                 C   s   | j ||dd�}|  |¡ dS )zÏHandle an incoming empty-element tag.

        This is only called when the markup looks like <tag/>.

        :param name: Name of the tag.
        :param attrs: Dictionary of the tag's attributes.
        F)Úhandle_empty_elementN)Úhandle_starttagÚhandle_endtag)r   ÚnameÚattrsÚtagr   r   r   Úhandle_startendtagZ   s    z*BeautifulSoupHTMLParser.handle_startendtagTc                 C   sÎ   i }|D ]`\}}|du rd}||v r\| j }|| jkr6qd|d| jfv rN|||< qd||||ƒ n|||< d}q|  ¡ \}	}
| jj|dd||	|
d�}|r¶|jr¶|r¶| j|dd� | j 	|¡ | j
du rÊ|  |¡ dS )a3  Handle an opening tag, e.g. '<tag>'

        :param name: Name of the tag.
        :param attrs: Dictionary of the tag's attributes.
        :param handle_empty_element: True if this tag is known to be
            an empty-element tag (i.e. there is not expected to be any
            closing tag).
        NÚ z"")Ú
sourcelineÚ	sourceposF)Úcheck_already_closed)r   ÚIGNOREr   ÚgetposÚsoupr!   Zis_empty_elementr"   r   ÚappendZ	_root_tagZ_root_tag_encountered)r   r#   r$   r    Ú	attr_dictÚkeyÚvalueZon_dupeÚ	attrvaluer(   r)   r%   r   r   r   r!   i   s.    



þ

z'BeautifulSoupHTMLParser.handle_starttagc                 C   s,   |r|| j v r| j  |¡ n| j |¡ dS )zõHandle a closing tag, e.g. '</tag>'
        
        :param name: A tag name.
        :param check_already_closed: True if this tag is expected to
           be the closing portion of an empty-element tag,
           e.g. '<tag></tag>'.
        N)r   Úremover-   r"   )r   r#   r*   r   r   r   r"       s    	z%BeautifulSoupHTMLParser.handle_endtagc                 C   s   | j  |¡ dS )z4Handle some textual data that shows up between tags.N)r-   Úhandle_data©r   Údatar   r   r   r4   ²   s    z#BeautifulSoupHTMLParser.handle_datac                 C   sî   |  d¡rt| d¡dƒ}n$|  d¡r8t| d¡dƒ}nt|ƒ}d}|dk rž| jjdfD ]D}|sbqXzt|gƒ |¡}W qX tyš } zW Y d}~qXd}~0 0 qX|sØzt|ƒ}W n( t	t
fyÖ } zW Y d}~n
d}~0 0 |pÞd}|  |¡ dS )z×Handle a numeric character reference by converting it to the
        corresponding Unicode character and treating it as textual
        data.

        :param name: Character number, possibly in hexadecimal.
        Úxé   ÚXNé   zwindows-1252u   ï¿½)Ú
startswithÚintÚlstripr-   Úoriginal_encodingÚ	bytearrayÚdecodeÚUnicodeDecodeErrorÚchrÚ
ValueErrorÚOverflowErrorr4   )r   r#   Z	real_namer6   ÚencodingÚer   r   r   Úhandle_charref¶   s*    

z&BeautifulSoupHTMLParser.handle_charrefc                 C   s0   t j |¡}|dur|}nd| }|  |¡ dS )zÈHandle a named entity reference by converting it to the
        corresponding Unicode character(s) and treating it as textual
        data.

        :param name: Name of the entity reference.
        Nz&%s)r
   ZHTML_ENTITY_TO_CHARACTERÚgetr4   )r   r#   Ú	characterr6   r   r   r   Úhandle_entityrefÞ   s
    z(BeautifulSoupHTMLParser.handle_entityrefc                 C   s&   | j  ¡  | j  |¡ | j  t¡ dS )zOHandle an HTML comment.

        :param data: The text of the comment.
        N)r-   ÚendDatar4   r   r5   r   r   r   Úhandle_commentñ   s    
z&BeautifulSoupHTMLParser.handle_commentc                 C   s6   | j  ¡  |tdƒd… }| j  |¡ | j  t¡ dS )zYHandle a DOCTYPE declaration.

        :param data: The text of the declaration.
        zDOCTYPE N)r-   rK   Úlenr4   r   r5   r   r   r   Úhandle_declú   s    
z#BeautifulSoupHTMLParser.handle_declc                 C   sN   |  ¡  d¡r$t}|tdƒd… }nt}| j ¡  | j |¡ | j |¡ dS )z{Handle a declaration of unknown type -- probably a CDATA block.

        :param data: The text of the declaration.
        zCDATA[N)Úupperr;   r   rM   r   r-   rK   r4   )r   r6   Úclsr   r   r   Úunknown_decl  s    
z$BeautifulSoupHTMLParser.unknown_declc                 C   s0   | j  ¡  | j  |¡ |  |¡ | j  t¡ dS )z\Handle a processing instruction.

        :param data: The text of the instruction.
        N)r-   rK   r4   Z_document_might_be_xmlr	   r5   r   r   r   Ú	handle_pi  s    

z!BeautifulSoupHTMLParser.handle_piN)T)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   r   r   r   r&   r!   r"   r4   rG   rJ   rL   rN   rQ   rR   r   r   r   r   r   $   s   
7
(	
r   c                       sN   e Zd ZdZdZdZeZeee	gZ
dZd‡ fdd„	Zddd„Zd	d
„ Z‡  ZS )r   zpA Beautiful soup `TreeBuilder` that uses the `HTMLParser` parser,
    found in the Python standard library.
    FTNc                    sp   t ƒ }dD ]}||v r
| |¡}|||< q
tt| ƒjf i |¤Ž |pFg }|pNi }| |¡ d|d< ||f| _dS )a„  Constructor.

        :param parser_args: Positional arguments to pass into 
            the BeautifulSoupHTMLParser constructor, once it's
            invoked.
        :param parser_kwargs: Keyword arguments to pass into 
            the BeautifulSoupHTMLParser constructor, once it's
            invoked.
        :param kwargs: Keyword arguments for the superclass constructor.
        )r   FÚconvert_charrefsN)Údictr   Úsuperr   r   ÚupdateÚparser_args)r   r[   Zparser_kwargsr   Zextra_parser_kwargsÚargr1   ©Ú	__class__r   r   r   *  s    


zHTMLParserTreeBuilder.__init__c           	      c   s\   t |tƒr|dddfV  dS |g}|g}||g}t|||d|d�}|j|j|j|jfV  dS )aÜ  Run any preliminary steps necessary to make incoming markup
        acceptable to the parser.

        :param markup: Some markup -- probably a bytestring.
        :param user_specified_encoding: The user asked to try this encoding.
        :param document_declared_encoding: The markup itself claims to be
            in this encoding.
        :param exclude_encodings: The user asked _not_ to try any of
            these encodings.

        :yield: A series of 4-tuples:
         (markup, encoding, declared encoding,
          has undergone character replacement)

         Each 4-tuple represents a strategy for converting the
         document to Unicode and parsing it. Each strategy will be tried 
         in turn.
        NFT)Úknown_definite_encodingsÚuser_encodingsZis_htmlÚexclude_encodings)Ú
isinstanceÚstrr   Úmarkupr>   Zdeclared_html_encodingZcontains_replacement_characters)	r   rd   Zuser_specified_encodingZdocument_declared_encodingra   r_   r`   Ztry_encodingsZdammitr   r   r   Úprepare_markupC  s"    
ûþz$HTMLParserTreeBuilder.prepare_markupc              
   C   sn   | j \}}t|i |¤Ž}| j|_z| |¡ W n, tyZ } zt|ƒ‚W Y d}~n
d}~0 0 | ¡  g |_dS )z{Run some incoming markup through some parsing process,
        populating the `BeautifulSoup` object in self.soup.
        N)r[   r   r-   ÚfeedÚAssertionErrorr   Úcloser   )r   rd   r   r   ÚparserrF   r   r   r   rf   t  s    
zHTMLParserTreeBuilder.feed)NN)NNN)rS   rT   rU   rV   Zis_xmlZ	picklableÚ
HTMLPARSERÚNAMEr   r   ÚfeaturesZTRACKS_LINE_NUMBERSr   re   rf   Ú__classcell__r   r   r]   r   r     s   
  ÿ
1)rV   Ú__license__Ú__all__Úhtml.parserr   ÚsysÚwarningsZbs4.elementr   r   r   r   r	   Z
bs4.dammitr
   r   Zbs4.builderr   r   r   r   r   rj   r   r   r   r   r   r   Ú<module>   s   ÿ	 z