o
    g'                     @   sH  d Z g dZddlZddlmZmZ zddlmZmZm	Z	m
Z
mZmZmZ eefZW n eyE   ddlmZmZm	Z	m
Z
mZmZ eZY nw dddZdd	d
ZdddZdd ZedejjZG dd dZdd Zdd ZzddlmZ W n ey   ddlmZ Y nw edjZze  W n e!y   e"Z Y nw dd Z#dS )z5External interface to the BeautifulSoup HTML parser.
)
fromstringparseconvert_tree    N)etreehtml)BeautifulSoupTagCommentProcessingInstructionNavigableStringDeclarationDoctype)r   r   r	   r
   r   r   c                 K   s   t | ||fi |S )a  Parse a string of HTML data into an Element tree using the
    BeautifulSoup parser.

    Returns the root ``<html>`` Element of the tree.

    You can pass a different BeautifulSoup parser through the
    `beautifulsoup` keyword, and a diffent Element factory function
    through the `makeelement` keyword.  By default, the standard
    ``BeautifulSoup`` class and the default factory of `lxml.html` are
    used.
    )_parse)databeautifulsoupmakeelementbsargs r   M/home/ubuntu/webapp/venv/lib/python3.10/site-packages/lxml/html/soupparser.pyr      s   r   c                 K   s0   t | ds	t| } t| ||fi |}t|S )aY  Parse a file into an ElemenTree using the BeautifulSoup parser.

    You can pass a different BeautifulSoup parser through the
    `beautifulsoup` keyword, and a diffent Element factory function
    through the `makeelement` keyword.  By default, the standard
    ``BeautifulSoup`` class and the default factory of `lxml.html` are
    used.
    read)hasattropenr   r   ElementTree)filer   r   r   rootr   r   r   r   $   s   
	
r   c                 C   s*   t | |}| }|D ]}|| q|S )a  Convert a BeautifulSoup tree to a list of Element trees.

    Returns a list instead of a single root Element to support
    HTML-like soup with more than one root element.

    You can pass a different Element factory through the `makeelement`
    keyword.
    )_convert_treegetchildrenremove)beautiful_soup_treer   r   childrenchildr   r   r   r   3   s
   
	r   c                 K   s   |d u rt }t|drd|vrd|d< t|dr d|vr d|d< || fi |}t||}t|dkr>|d jdkr>|d S d|_|S )	NHTML_ENTITIESconvertEntitiesr   DEFAULT_BUILDER_FEATURESfeatureszhtml.parser   r   )r   r   r   lentag)sourcer   r   r   treer   r   r   r   r   E   s   


r   z`(?:\s|[<!])*DOCTYPE\s*HTML(?:\s+PUBLIC)?(?:\s+(\'[^\']*\'|"[^"]*"))?(?:\s+(\'[^\']*\'|"[^"]*"))?c                   @   s   e Zd Zdd Zdd ZdS )
_PseudoTagc                 C   s   d| _ g | _|| _d S )Nr   )nameattrscontents)selfr-   r   r   r   __init__`   s   
z_PseudoTag.__init__c                 C   s
   | j  S N)r-   __iter__)r.   r   r   r   r1   e   s   
z_PseudoTag.__iter__N)__name__
__module____qualname__r/   r1   r   r   r   r   r*   ^   s    r*   c                 C   s  |d u rt jj}d  }}d  }}t| D ]-\}}t|tr6|d u r#|}|}|d u r5|jr5|j dkr5|}q|d u rAt|trA|}q|d u rNg  }}	| j	}
n| j	d | }| j	||d  }
| j	|d d  }	|d ur|

|}|
d | |j	 |
|d d   |_	nt|
}t|}||}|}t|D ]}||}|d ur|| |}q|}|	D ]}||}|d ur|| |}q|d urz| }W n ty   |j}Y nw t|}|s	 |S | \}}| j}|o|dd |_|o|dd |_|S )Nr   r%   )r   html_parserr   	enumerate
isinstancer   r+   lower_DECLARATION_OR_DOCTYPEr-   indexr*   _init_node_convertersreversedaddpreviousaddnextoutput_readyAttributeErrorstring_parse_doctype_declarationgroupsgetroottreedocinfo	public_id
system_url)r   r   first_element_idxlast_element_idx	html_rootdeclarationiepre_root	post_rootrootsconvert_noderes_rootprev	converteddoctype_stringmatchexternal_idsys_urirF   r   r   r   r   i   sp   

&



r   c                    s   i g fdd}fdddfdd	dd d	d
  |t tfdd}|tdd }|tdd }|t fdd}S )Nc                     s    fdd}|S )Nc                    s    D ]}|  |<  | q| S r0   )append)handlert)
convertersordered_node_typestypesr   r   add   s   z5_init_node_converters.<locals>.converter.<locals>.addr   )r_   r`   r]   r^   )r_   r   	converter   s   z(_init_node_converters.<locals>.converterc                    s$   D ]}t | |r |   S qd S r0   )r8   )noder\   ra   r   r   find_best_converter   s
   
z2_init_node_converters.<locals>.find_best_converterc                    sN   z t |  }W n ty   |  } t | < Y nw |d u r"d S || |S r0   )typeKeyError)bs_nodeparentr[   )r]   rd   r   r   rR      s   
z+_init_node_converters.<locals>.convert_nodec                 S   sV   t | tr"i }|  D ]\}}t |trd|}t|||< q|S dd | D }|S )N c                 S   s   i | ]	\}}|t |qS r   unescape).0kvr   r   r   
<dictcomp>   s    z<_init_node_converters.<locals>.map_attrs.<locals>.<dictcomp>)r8   dictitemslistjoinrk   )bs_attrsattribsrm   rn   r   r   r   	map_attrs   s   


z(_init_node_converters.<locals>.map_attrsc                 S   s<   t | dkr| jp
d| | _d S | d jpd| | d _d S )Nr    r5   )r&   texttail)rh   rx   r   r   r   append_text   s   z*_init_node_converters.<locals>.append_textc              	      s   | j }|d ur|r|nd }tj|| j|d}n|r|ni }| j|d}| D ]#}zt| }W n	 ty=   Y nw |d urG||| q* || q*|S )N)attrib)r,   r   
SubElementr+   re   rf   )rg   rh   r,   ru   resr    r[   )rR   r]   r   rv   r   r   convert_tag   s"   
z*_init_node_converters.<locals>.convert_tagc                 S   s    t | }|d ur|| |S r0   )r   HtmlCommentrZ   rg   rh   r}   r   r   r   convert_comment  s   

z._init_node_converters.<locals>.convert_commentc                 S   s>   |  dr| d d } tj| dd }|d ur|| |S )N?r5   ri   r%   )endswithr   r
   splitrZ   r   r   r   r   
convert_pi  s   

z)_init_node_converters.<locals>.convert_pic                    s   |d ur |t |  d S r0   rj   )rg   rh   )rz   r   r   convert_text  s   z+_init_node_converters.<locals>.convert_textr0   )r   r*   r	   r
   r   )r   rb   r~   r   r   r   r   )rz   rR   r]   rd   r   rv   r^   r   r<      s    



r<   )name2codepointz&(\w+);c                 C   s   | sdS dd }t || S )Nrw   c                 S   s2   z
t t| d W S  ty   | d Y S w )Nr%   r   )unichrr   grouprf   )mr   r   r   unescape_entity5  s
   z!unescape.<locals>.unescape_entity)handle_entities)rB   r   r   r   r   rk   1  s   
rk   )NNr0   )$__doc____all__relxmlr   r   bs4r   r   r	   r
   r   r   r   r:   ImportErrorr   r   r   r   compile
IGNORECASErW   rC   r*   r   r<   html.entitiesr   htmlentitydefssubr   r   	NameErrorchrrk   r   r   r   r   <module>   sH    $ 


Uc