
    Mh                     :   U d Z dZddlmZ ddlmZ ddlZddlmZ ddlZddl	m
Z
mZ ddl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 dd
lmZ ddlmZmZ ddlZdZee         e d<   	 ddl!Z!e!Zn1# e"$ r) 	 ddl#Z#e#Zn# e"$ r 	 ddl$Z$e$Zn# e"$ r Y nw xY wY nw xY wY nw xY wde%dee&         fdZ'dZ(e&e d<   dZ)e&e d<    e*            Z+eeee&ef         f         e d<    ej,        e)-                    d          ej.                   ej,        e(-                    d          ej.                  de+e%<    ej,        e)ej.                   ej,        e(ej.                  de+e&<    G d de/          Z0e01                                  G d d          Z2 G d d          Z3dS )a~  Beautiful Soup bonus library: Unicode, Dammit

This library converts a bytestream to Unicode through any means
necessary. It is heavily based on code from Mark Pilgrim's `Universal
Feed Parser <https://pypi.org/project/feedparser/>`_, now maintained
by Kurt McKee. It does not rewrite the body of an XML or HTML document
to reflect a new encoding; that's the job of `TreeBuilder`.

MIT    )codepoint2name)defaultdictN)html5)Logger	getLogger)
ModuleType)
DictIteratorListOptionalPatternSetTupleTypeUnioncast)Literal)	_Encoding
_Encodingschardet_modulesreturnc                     t           t          | t                    rdS t           }|                    |           d         S )z?Try as hard as possible to detect the encoding of a bytestring.Nencoding)r   
isinstancestrdetect)r   modules     J/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/bs4/dammit.py_chardet_dammitr!   G   s8    As!3!3tF==J''    z$^\s*<\?.*encoding=['"](.*?)['"].*\?>xml_encodingz0<\s*meta[^>]+charset\s*=\s*["']?([^>]*?)[ /;'">]	html_metaencoding_resascii)htmlxmlc                      e Zd ZU dZeeef         ed<   eeef         ed<   ee         ed<   ee         ed<   ed$d            Z	d	d
ddddZ
eeef         ed<    ej        dej                  Z ej        d          Zee         ed<    ej        d          Zee         ed<   edej        defd            Zedej        defd            Zedej        defd            Zedej        defd            Zededefd            Zed%dededefd            Ze	 d%dededefd            Zed edefd!            Zed edefd"            Zed edefd#            ZdS )&EntitySubstitutionzFThe ability to substitute XML or HTML entities for certain characters.HTML_ENTITY_TO_CHARACTERCHARACTER_TO_HTML_ENTITYCHARACTER_TO_HTML_ENTITY_RE*CHARACTER_TO_HTML_ENTITY_WITH_AMPERSAND_REr   Nc                 "   i }i }t                      }t          t                     }t          t          j                              D ]\  }}|                    d          r|dd         }n|}||vr|||<   |||<   t          |          dk    rt          |          dk     r|dvr`t          |          dk    rt          d |D                       rt          |          dk    r|dk    r|	                    |           ||d	                  	                    |           t                      }|D ]\}	||	         }
|
s|	                    |	           "d

                    d |
D                       }|	                    |	d|d           ]t          |                                          D ]}|D ]}|	                    |           dd
                    |          z  }|	                    d           dd
                    |          z  }t          t          j                              D ]\  }}t          |          }|||<   || _        || _        t#          j        |          | _        t#          j        |          | _        dS )u  Initialize variables used by this class to manage the plethora of
        HTML5 named entities.

        This function sets the following class variables:

        CHARACTER_TO_HTML_ENTITY - A mapping of Unicode strings like "⦨" to
        entity names like "angmsdaa". When a single Unicode string has
        multiple entity names, we try to choose the most commonly-used
        name.

        HTML_ENTITY_TO_CHARACTER: A mapping of entity names like "angmsdaa" to
        Unicode strings like "⦨".

        CHARACTER_TO_HTML_ENTITY_RE: A regular expression matching (almost) any
        Unicode string that corresponds to an HTML5 named entity.

        CHARACTER_TO_HTML_ENTITY_WITH_AMPERSAND_RE: A very similar
        regular expression to CHARACTER_TO_HTML_ENTITY_RE, but which
        also matches unescaped ampersands. This is used by the 'html'
        formatted to provide backwards-compatibility, even though the HTML5
        spec allows most ampersands to go unescaped.
        ;N      z<>c              3   <   K   | ]}t          |          d k     V  dS )r3   N)ord.0xs     r    	<genexpr>z?EntitySubstitution._populate_class_variables.<locals>.<genexpr>   s,      )J)J1#a&&3,)J)J)J)J)J)Jr"   &r    c                     g | ]
}|d          S )r2    r6   s     r    
<listcomp>z@EntitySubstitution._populate_class_variables.<locals>.<listcomp>   s    !>!>!>1!A$!>!>!>r"   z(?![z])z(%s)|)setr   sortedr   itemsendswithlenr5   alladdjoinlistvaluesr   chrr,   r+   recompiler-   r.   )clsunicode_to_namename_to_unicodeshort_entities long_entities_by_first_charactername_with_semicolon	charactername	particlesshortlong_versionsignorelong_entitieslong_entityre_definitionre_definition_with_ampersand	codepoints                    r    _populate_class_variablesz,EntitySubstitution._populate_class_variables   s   0 +6s+;+;(.4U[]].C.C =	N =	N* #++C00 +*3B3/*
 ?**(1%
 *.OI& 9~~""s9~~';';	QU@U@U
 9~~!!c)J)J	)J)J)J&J&J!  9~~""yC'7'7""9----01>BB9MMMM EE	# 	> 	>E<UCM  >e$$$$!>!>!>!>!>?? eeeVVV<====!"B"I"I"K"KLL 	+ 	+M, + +k****+ )!4!44c'-0C0C'C$  $N$8$:$:;; 	. 	.OItII)-OI&&'6$'6$*,*]*C*C'9;(:
 :
666r"   aposquotampltgt)'"r:   <>CHARACTER_TO_XML_ENTITYz&(#\d+|#x[0-9a-fA-F]+|\w+);z&([<>]|&(?!#\d+;|#x[0-9a-fA-F]+;|\w+;))BARE_AMPERSAND_OR_BRACKETz([<>&])AMPERSAND_OR_BRACKETmatchobjc                 x    |                     d          }| j                            |          }|d|z  S d|z  S )zpUsed with a regular expression to substitute the
        appropriate HTML entity for a special character string.r   N&amp;%s;&%s;)groupr,   get)rM   rk   original_entityentitys       r    _substitute_html_entityz*EntitySubstitution._substitute_html_entity  sE     #..++-11/BB>//r"   c                 L    | j         |                    d                   }d|z  S )zoUsed with a regular expression to substitute the
        appropriate XML entity for a special character string.r   rn   )rh   ro   )rM   rk   rr   s      r    _substitute_xml_entityz)EntitySubstitution._substitute_xml_entity)  s'     ,X^^A->->?r"   c                 2    d|                     d          z  S )Nrm   r2   )ro   )rM   rk   s     r    _escape_entity_namez&EntitySubstitution._escape_entity_name0  s    HNN1----r"   c                 R    |                     d          }|| j        v rd|z  S d|z  S )Nr2   rn   rm   )ro   r+   )rM   rk   possible_entitys      r     _escape_unrecognized_entity_namez3EntitySubstitution._escape_unrecognized_entity_name4  s7    "..++c:::O++O++r"   valuec                 \    d}d|v rd|v rd}|                     d|          }nd}||z   |z   S )a  Make a value into a quoted XML attribute, possibly escaping it.

         Most strings will be quoted using double quotes.

          Bob's Bar -> "Bob's Bar"

         If a string contains double quotes, it will be quoted using
         single quotes.

          Welcome to "my bar" -> 'Welcome to "my bar"'

         If a string contains both single and double quotes, the
         double quotes will be escaped, and the string will be quoted
         using double quotes.

          Welcome to "Bob's Bar" -> Welcome to &quot;Bob's bar&quot;

        :param value: The XML attribute value to quote
        :return: The quoted value
        re   rd   z&quot;)replace)rM   r{   
quote_withreplace_withs       r    quoted_attribute_valuez)EntitySubstitution.quoted_attribute_value;  sN    , 
%<<e||  (c<88 !
E!J..r"   Fmake_quoted_attributec                 t    | j                             | j        |          }|r|                     |          }|S )av  Replace special XML characters with named XML entities.

        The less-than sign will become &lt;, the greater-than sign
        will become &gt;, and any ampersands will become &amp;. If you
        want ampersands that seem to be part of an entity definition
        to be left alone, use `substitute_xml_containing_entities`
        instead.

        :param value: A string to be substituted.

        :param make_quoted_attribute: If True, then the string will be
         quoted, as befits an attribute value.

        :return: A version of ``value`` with special characters replaced
         with named entities.
        )rj   subru   r   rM   r{   r   s      r    substitute_xmlz!EntitySubstitution.substitute_xmlc  s?    & (,,S-GOO  	6..u55Er"   c                 t    | j                             | j        |          }|r|                     |          }|S )a  Substitute XML entities for special XML characters.

        :param value: A string to be substituted. The less-than sign will
          become &lt;, the greater-than sign will become &gt;, and any
          ampersands that are not part of an entity defition will
          become &amp;.

        :param make_quoted_attribute: If True, then the string will be
         quoted, as befits an attribute value.
        )ri   r   ru   r   r   s      r    "substitute_xml_containing_entitiesz5EntitySubstitution.substitute_xml_containing_entities|  s?      -11#2LeTT  	6..u55Er"   r   c                 B    | j                             | j        |          S )a  Replace certain Unicode characters with named HTML entities.

        This differs from ``data.encode(encoding, 'xmlcharrefreplace')``
        in that the goal is to make the result more readable (to those
        with ASCII displays) rather than to recover from
        errors. There's absolutely nothing wrong with a UTF-8 string
        containg a LATIN SMALL LETTER E WITH ACUTE, but replacing that
        character with "&eacute;" will make it more readable to some
        people.

        :param s: The string to be modified.
        :return: The string with some Unicode characters replaced with
           HTML entities.
        )r.   r   rs   rM   r   s     r    substitute_htmlz"EntitySubstitution.substitute_html  s'    " =AA'
 
 	
r"   c                     | j                             | j        |          }| j                            | j        |          }|S )aH  Replace certain Unicode characters with named HTML entities
        using HTML5 rules.

        Specifically, this method is much less aggressive about
        escaping ampersands than substitute_html. Only ambiguous
        ampersands are escaped, per the HTML5 standard:

        "An ambiguous ampersand is a U+0026 AMPERSAND character (&)
        that is followed by one or more ASCII alphanumerics, followed
        by a U+003B SEMICOLON character (;), where these characters do
        not match any of the names given in the named character
        references section."

        Unlike substitute_html5_raw, this method assumes HTML entities
        were converted to Unicode characters on the way in, as
        Beautiful Soup does. By the time Beautiful Soup does its work,
        the only ambiguous ampersands that need to be escaped are the
        ones that were escaped in the original markup when mentioning
        HTML entities.

        :param s: The string to be modified.
        :return: The string with some Unicode characters replaced with
           HTML entities.
        )ANY_ENTITY_REr   rw   r-   rs   r   s     r    substitute_html5z#EntitySubstitution.substitute_html5  sA    6 !!#"91== +//0KQOOr"   c                     | j                             | j        |          }| j                            | j        |          }|S )a  Replace certain Unicode characters with named HTML entities
        using HTML5 rules.

        substitute_html5_raw is similar to substitute_html5 but it is
        designed for standalone use (whereas substitute_html5 is
        designed for use with Beautiful Soup).

        :param s: The string to be modified.
        :return: The string with some Unicode characters replaced with
           HTML entities.
        )r   r   rz   r-   rs   r   s     r    substitute_html5_rawz'EntitySubstitution.substitute_html5_raw  sA      !!#"FJJ +//0KQOOr"   )r   N)F)__name__
__module____qualname____doc__r
   r   __annotations__r   classmethodr^   rh   rK   rL   Ir   ri   rj   Matchrs   ru   rw   rz   r   boolr   r   r   r   r   r=   r"   r    r*   r*   b   s"        PP
 #38n,,, #38n,,, ")--- 18<<<A
 A
 A
 [A
N / /T#s(^    BJ>EEM /9bj8/ /ws|    *4I)>)>'#,>>>rx C    [ bh 3    [ .28 . . . . [. , ,S , , , [, %/3 %/3 %/ %/ %/ [%/N  3 t PS    [0 7< 04	   [* 
 
 
 
 
 [
(      [B S S    [  r"   r*   c                      e Zd ZU dZ	 	 	 	 	 ddedee         dee         dee         dee         d	ee         fd
Zee	d<   ee	d<   ee	d<   ee
         e	d<   ee	d<   ee
         e	d<   ee	d<   ee
         e	d<   dee
         dee
         defdZedee
         fd            Zededeeee
         f         fd            Ze	 	 ddeeef         dededee
         fd            ZdS )EncodingDetectora  This class is capable of guessing a number of possible encodings
    for a bytestring.

    Order of precedence:

    1. Encodings you specifically tell EncodingDetector to try first
       (the ``known_definite_encodings`` argument to the constructor).

    2. An encoding determined by sniffing the document's byte-order mark.

    3. Encodings you specifically tell EncodingDetector to try if
       byte-order mark sniffing fails (the ``user_encodings`` argument to the
       constructor).

    4. An encoding declared within the bytestring itself, either in an
       XML declaration (if the bytestring is to be interpreted as an XML
       document), or in a <meta> tag (if the bytestring is to be
       interpreted as an HTML document.)

    5. An encoding detected through textual analysis by chardet,
       cchardet, or a similar external library.

    6. UTF-8.

    7. Windows-1252.

    :param markup: Some markup in an unknown encoding.

    :param known_definite_encodings: When determining the encoding
        of ``markup``, these encodings will be tried first, in
        order. In HTML terms, this corresponds to the "known
        definite encoding" step defined in `section 13.2.3.1 of the HTML standard <https://html.spec.whatwg.org/multipage/parsing.html#parsing-with-a-known-character-encoding>`_.

    :param user_encodings: These encodings will be tried after the
        ``known_definite_encodings`` have been tried and failed, and
        after an attempt to sniff the encoding by looking at a
        byte order mark has failed. In HTML terms, this
        corresponds to the step "user has explicitly instructed
        the user agent to override the document's character
        encoding", defined in `section 13.2.3.2 of the HTML standard <https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding>`_.

    :param override_encodings: A **deprecated** alias for
        ``known_definite_encodings``. Any encodings here will be tried
        immediately after the encodings in
        ``known_definite_encodings``.

    :param is_html: If True, this markup is considered to be
        HTML. Otherwise it's assumed to be XML.

    :param exclude_encodings: These encodings will not be tried,
        even if they otherwise would be.

    NFmarkupknown_definite_encodingsis_htmlexclude_encodingsuser_encodingsoverride_encodingsc                 Z   t          |pg           | _        |r,t          j        dt          d           | xj        |z  c_        |pg | _        |pg }t          d |D                       | _        d | _        |dn|| _	        d | _
        |                     |          \  | _        | _        d S )NzcThe 'override_encodings' argument was deprecated in 4.10.0. Use 'known_definite_encodings' instead.   )
stacklevelc                 6    g | ]}|                                 S r=   )lowerr6   s     r    r>   z-EncodingDetector.__init__.<locals>.<listcomp>/  s     %K%K%KAaggii%K%K%Kr"   F)rH   r   warningswarnDeprecationWarningr   r@   r   chardet_encodingr   declared_encodingstrip_byte_order_markr   sniffed_encoding)selfr   r   r   r   r   r   s          r    __init__zEncodingDetector.__init__  s     )--E-K(L(L% 	@Mu"   
 ))-??)),2-3!$%K%K9J%K%K%K!L!L $ 'uuW04 .2-G-G-O-O*T***r"   r   r   r   r   triedr   c                     |dS |                                 }|| j        v rdS ||vr|                    |           dS dS )zShould we even bother to try this encoding?

        :param encoding: Name of an encoding.
        :param tried: Encodings that have already been tried. This
            will be modified as a side effect.
        NFT)r   r   rF   )r   r   r   s      r    _usablezEncodingDetector._usable@  sX     5>>##t---55  IIh4ur"   c              #     K   t                      }| j        D ]}|                     ||          r|V  | j        $|                     | j        |          r	| j        V  | j        D ]}|                     ||          r|V  | j        %|                     | j        | j                  | _        | j        $|                     | j        |          r	| j        V  | j	        t          | j                  | _	        | j	        $|                     | j	        |          r	| j	        V  dD ]}|                     ||          r|V  dS )zYield a number of encodings that might work for this markup.

        :yield: A sequence of strings. Each is the name of an encoding
           that *might* work to convert a bytestring into Unicode.
        N)utf-8windows-1252)r@   r   r   r   r   r   find_declared_encodingr   r   r   r!   )r   r   es      r    	encodingszEncodingDetector.encodingsQ  s      !$ . 	 	A||Au%%   ,!52
 2
, '''' $ 	 	A||Au%%  !)%)%@%@T\& &D" !-$,,"E3
 3
- ((((  ($3DK$@$@D! ,!52
 2
, '''' + 	 	A||Au%% 	 	r"   datac                    d}t          |t                    r||fS t          |          dk    r)|dd         dk    r|dd         dk    rd}|dd         }nt          |          dk    r)|dd         dk    r|dd         dk    rd}|dd         }nP|dd	         d
k    rd}|d	d         }n5|dd         dk    rd}|dd         }n|dd         dk    rd}|dd         }||fS )a  If a byte-order mark is present, strip it and return the encoding it implies.

        :param data: A bytestring that may or may not begin with a
           byte-order mark.

        :return: A 2-tuple (data stripped of byte-order mark, encoding implied by byte-order mark)
        N      s   s     zutf-16bes   zutf-16ler   s   ﻿r   s     zutf-32bes     zutf-32le)r   r   rD   )rM   r   r   s      r    r   z&EncodingDetector.strip_byte_order_mark  s6    dC   	">!YY!^^bqb[((ack))!H8DDYY!^^bqb[((ack))!H8DD"1"X((H8DD"1"X,,,!H8DD"1"X,,,!H8DX~r"   search_entire_documentc                 B   |rt          |          x}}n/d}t          dt          t          |          dz                      }t          |t                    rt
          t                   }nt
          t                   }|d         }|d         }d}	|                    ||          }
|
s|r|                    ||          }
|
|
                                d         }	|	r?t          |	t                    r|		                    d	d
          }	|	
                                S dS )a'  Given a document, tries to find an encoding declared within the
        text of the document itself.

        An XML encoding is declared at the beginning of the document.

        An HTML encoding is declared in a <meta> tag, hopefully near the
        beginning of the document.

        :param markup: Some markup.
        :param is_html: If True, this markup is considered to be HTML. Otherwise
            it's assumed to be XML.
        :param search_entire_document: Since an encoding is supposed
            to declared near the beginning of the document, most of
            the time it's only necessary to search a few kilobytes of
            data.  Set this to True to force this method to search the
            entire document.
        :return: The declared encoding, if one is found.
        i   i   g?r(   r'   N)endposr   r&   r}   )rD   maxintr   bytesr%   r   searchgroupsdecoder   )rM   r   r   r   
xml_endposhtml_endposresxml_rehtml_rer   declared_encoding_matchs              r    r   z'EncodingDetector.find_declared_encoding  s(   2 " 	='*6{{2JJdCFd(:$;$;<<Kfe$$ 	$u%CCs#CUf+15"(--z-"J"J& 	Q7 	Q&-nnVKn&P&P#". 7 > > @ @ C 	-+U33 Q$5$<$<Wi$P$P!$**,,,tr"   )NFNNN)FF)r   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r=   r"   r    r   r     s        4 4r :>"'26/337P PP #+:"6P $	P
 $J/P !,P %Z0P P P P6 )(((!!!!y))))MMM	****MMMy)))) 3 C	N t    " 18I. 1 1 1 X1f # #5@S9S3T # # # [#J  ',	/ /eSj!/ / !%	/
 
)	/ / / [/ / /r"   r   c                      e Zd ZU dZg ddg ddfdedee         deed                  ded	ee         d
ee         dee         fdZ	ee
d<   ee         e
d<   ee
d<   ee         e
d<   ee         e
d<   eeeef                  e
d<   ee
d<   dej        defdZdddZeeef         e
d<   g dZee
d<   	 ddededee         fdZ	 dded ededefd!Zedee         fd"            Zd#edee         fd$Zd#edee         fd%Zi d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdAdEdAdFdGdHdIdJdKdLdMdNdOdPdQdAdRdSdTZeeeeeeef         f         f         e
dU<   i d&dVd(d)d*dWd,dXd.dYd0dZd2d[d4d\d6d]d8d^d:d_d<d`d>dad@dAdBdbdDdAdEdAi dcdddedddfdgdhdgdidjdkdldmdndodpdqdrdsdtdudvdwdxdydAdzd{d|d}d~d)ddi dddddddddddd_dddddddddddd)ddddlddddddi dddddddddddjddWddddddddddddddAddddddddi ddddddddddddddddddœddœddœddœddʓdd̓ddΓddΓddΓi ddΓddΓddjddΓdd֓dd֓dd֓dd֓dd}ddܓddޓddddddddddddi dddddddddddddddddddddddddddddddddddddddddddd	Zeeef         e
d<   i 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dddddddd i d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBi dCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcddi dedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|d}d~ddddddddi 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ddddddddi ddddddddddddddddddddddddddddÓdĐdœdƐdǓdȐdɓi 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ddddddddddddZee ef         e
d<   g dZ!eee e e f                  e
d<   e!d         d         Z"e e
d<   e!d         d         Z#e e
d<   e$	 	 ddedededefd            Z%dS (   UnicodeDammita#  A class for detecting the encoding of a bytestring containing an
    HTML or XML document, and decoding it to Unicode. If the source
    encoding is windows-1252, `UnicodeDammit` can also replace
    Microsoft smart quotes with their HTML or XML equivalents.

    :param markup: HTML or XML markup in an unknown encoding.

    :param known_definite_encodings: When determining the encoding
        of ``markup``, these encodings will be tried first, in
        order. In HTML terms, this corresponds to the "known
        definite encoding" step defined in `section 13.2.3.1 of the HTML standard <https://html.spec.whatwg.org/multipage/parsing.html#parsing-with-a-known-character-encoding>`_.

    :param user_encodings: These encodings will be tried after the
        ``known_definite_encodings`` have been tried and failed, and
        after an attempt to sniff the encoding by looking at a
        byte order mark has failed. In HTML terms, this
        corresponds to the step "user has explicitly instructed
        the user agent to override the document's character
        encoding", defined in `section 13.2.3.2 of the HTML standard <https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding>`_.

    :param override_encodings: A **deprecated** alias for
        ``known_definite_encodings``. Any encodings here will be tried
        immediately after the encodings in
        ``known_definite_encodings``.

    :param smart_quotes_to: By default, Microsoft smart quotes will,
       like all other characters, be converted to Unicode
       characters. Setting this to ``ascii`` will convert them to ASCII
       quotes instead.  Setting it to ``xml`` will convert them to XML
       entity references, and setting it to ``html`` will convert them
       to HTML entity references.

    :param is_html: If True, ``markup`` is treated as an HTML
       document. Otherwise it's treated as an XML document.

    :param exclude_encodings: These encodings will not be considered,
       even if the sniffing code thinks they might make sense.

    NFr   r   smart_quotes_to)r&   r(   r'   r   r   r   r   c                    || _         g | _        d| _        || _        t	          t
                    | _        t          ||||||          | _        t          |t                    s|dk    r$|| _        t          |          | _        d | _        d S | j        j        | _        d }| j        j        D ]'}	| j        j        }|                     |	          }| n(|sP| j        j        D ]C}	|	dk    r|                     |	d          }|#| j                            d           d| _         nD|d | _        d | _        d S || _        d S )NFr"   r&   r}   zSSome characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.T)r   tried_encodingscontains_replacement_charactersr   r   r   logr   detectorr   r   r   unicode_markuporiginal_encodingr   _convert_fromwarning)
r   r   r   r   r   r   r   r   ur   s
             r    r   zUnicodeDammit.__init__  s     /!/4,X&&($
 
 fc"" 	fmm DK"%f++D%)D"F m*/ 	 	H])F""8,,A}   	 !M3 
 
w&&**8Y??A=H$$?  
 <@D8E !" 9%)D""&D"#Dr"   r   r   r   r   r   matchr   c                    |                     d          }| j        dk    r,|| j        v r | j        |                                         }n|}n|| j        v r| j        |         }t          |          t          u rM| j        dk    r!d|d                                         z   dz   }nMd|d                                         z   dz   }n,t          t          |          }|                                }n|}|S )zChanges a MS smart quote character to an XML or HTML
        entity, or an ASCII character.

        TODO: Since this is only used to convert smart quotes, it
        could be simplified, and MS_CHARS_TO_ASCII made much less
        parochial.
        r2   r&   r(   s   &#x   ;   &r   )	ro   r   MS_CHARS_TO_ASCIIencodeMS_CHARStypetupler   r   )r   r   origr   substitutionss        r    _sub_ms_charzUnicodeDammit._sub_ms_charl  s     kk!nn7**t---,T299;; t}$$ $d 3&&%//+u44$}Q'7'>'>'@'@@4G"]1%5%<%<%>%>>E$(m$<$<M'..00CC 
r"   z	mac-romanz	shift-jis)	macintoshzx-sjisCHARSET_ALIASES)r   z
iso-8859-1z
iso-8859-2ENCODINGS_WITH_SMART_QUOTESstrictproposederrorsc                    |                      |          }|||f| j        v rdS |}| j                            ||f           | j        }| j        :|| j        v r1d}t          j        |          }|                    | j	        |          }	 | 
                    |||          }|| _        || _        n# t          $ r Y dS w xY w| j        S )aB  Attempt to convert the markup to the proposed encoding.

        :param proposed: The name of a character encoding.
        :param errors: An error handling strategy, used when calling `str`.
        :return: The converted markup, or `None` if the proposed
           encoding/error handling strategy didn't work.
        Ns   ([-]))
find_codecr   appendr   r   r   rK   rL   r   r   _to_unicoder   r   	Exception)r   r   r   lookup_resultr   smart_quotes_resmart_quotes_compiledr   s           r    r   zUnicodeDammit._convert_from  s     11 ]F$;t?S$S$S4 ##Xv$6777  ,D<<<.O$&J$?$?!*..t/@&IIF		   6::A"#D%-D"" 	 	 	 44	
 ""s   %B2 2
C ?C r   r   c                 $    t          |||          S )zGiven a bytestring and its encoding, decodes the string into Unicode.

        :param encoding: The name of an encoding.
        :param errors: An error handling strategy, used when calling `str`.
        )r   )r   r   r   r   s       r    r   zUnicodeDammit._to_unicode  s     46***r"   c                 ,    | j         sdS | j        j        S )zqIf the markup is an HTML document, returns the encoding, if any,
        declared *inside* the document.
        N)r   r   r   )r   s    r    declared_html_encodingz$UnicodeDammit.declared_html_encoding  s    
 | 	4}..r"   charsetc                 j   |                      | j                            ||                    pm|o(|                      |                    dd                    pB|o(|                      |                    dd                    p|o|                                p|}|r|                                S dS )zLook up the Python codec corresponding to a given character set.

        :param charset: The name of a character set.
        :return: The name of a Python codec.
        -r;   _N)_codecr   rp   r}   r   )r   r   r{   s      r    r   zUnicodeDammit.find_codec  s     KK,00'BBCC ADKKR(@(@AABDKKS(A(ABB +GMMOO  	  	!;;== tr"   c                 p    |s|S d }	 t          j        |           |}n# t          t          f$ r Y nw xY w|S )N)codecslookupLookupError
ValueError)r   r   codecs      r    r   zUnicodeDammit._codec  sZ     	N	M'"""EEZ( 	 	 	D	s    33   )euro20AC       )sbquo201A   )fnof192   )bdquo201E   )hellip2026   )dagger2020   )Dagger2021   )circ2C6   )permil2030   )Scaron160   )lsaquo2039   )OElig152   ?   )z#x17D17D      )lsquo2018)rsquo2019)ldquo201C)rdquo201D)bull2022)ndash2013)mdash2014)tilde2DC)trade2122)scaron161)rsaquo203A)oelig153)z#x17E17E)Yumlr;   )                                             r   EUR,fz,,z...+z++^%Srf   OEZrG  rd   rH  rI  re   rJ  rK  *rL  r   rM  z--rN  ~rO  z(TM)rP  r   rQ  rg   rR  oerS  rT  zrU  Y      !   c   GBP   $   YEN   r?         z..   r;      z(th)   z<<         z(R)      o   z+-   2   3      r      P         1      z>>   z1/4   z1/2   z3/4      A                  AE   C   E            r               D   N   O                     U               b   B   a                  ae      r               i               n               /y)	                           r   r3   s   €   s   ‚   s   ƒ   s   „   s   …   s   †   s   ‡   s   ˆ   s   ‰   s   Š   s   ‹   s   Œ   s   Ž   s   ‘   s   ’   s   “   s   ”   s   •   s   –   s   —   s   ˜   s   ™   s   š   s   ›   s   œ   s   ž   s   Ÿ   s       s   ¡   s   ¢   s   £   s   ¤   s   ¥   s   ¦   s   §   s   ¨   s   ©   s   ª   s   «   s   ¬   s   ­   s   ®   s   ¯   s   °   s   ±   s   ²   s   ³   s   ´   s   µ   s   ¶   s   ·   s   ¸   s   ¹   s   º   s   »   s   ¼   s   ½   s   ¾   s   ¿   s   À   s   Á   s   Â   s   Ã   s   Ä   s   Å   s   Æ   s   Ç   s   È   s   É   s   Ê   s   Ë   s   Ì   s   Í   s   Î   s   Ï   s   Ð   s   Ñ   s   Ò   s   Ó   s   Ô   s   Õ   s   Ö   s   ×   s   Ø   s   Ù   s   Ú   s   Û   s   Ü   s   Ý   s   Þ   s   ß   s   à      s   â   s   ã   s   ä   s   å   s   æ   s   ç   s   è   s   é   s   ê   s   ë   s   ì   s   í   s   î   s   ï   s   ð   s   ñ   s   ò   s   ó   s   ô   s   õ   s   ö   s   ÷   s   ø   s   ù   s   ú   s   ûs   üs   ýs   þ)         WINDOWS_1252_TO_UTF8))r  r7  r   )r8  rG  r   )rH  rL  r   MULTIBYTE_MARKERS_AND_SIZESr   FIRST_MULTIBYTE_MARKERr1   r2   LAST_MULTIBYTE_MARKERutf8r   in_bytesmain_encodingembedded_encodingc                    |                     dd                                          dvrt          d          |                                dvrt          d          g }d}d}|t          |          k     r||         }|| j        k    r-|| j        k    r"| j        D ]\  }}	}
||k    r||	k    r||
z  } nnY|dk    rN|| j        v rE|                    |||                    |                    | j        |                    |d	z  }|}n|d	z  }|t          |          k     |dk    r|S |                    ||d
                    d	                    |          S )a  Fix characters from one encoding embedded in some other encoding.

        Currently the only situation supported is Windows-1252 (or its
        subset ISO-8859-1), embedded in UTF-8.

        :param in_bytes: A bytestring that you suspect contains
            characters from multiple encodings. Note that this *must*
            be a bytestring. If you've already converted the document
            to Unicode, you're too late.
        :param main_encoding: The primary encoding of ``in_bytes``.
        :param embedded_encoding: The encoding that was used to embed characters
            in the main document.
        :return: A bytestring similar to ``in_bytes``, in which
          ``embedded_encoding`` characters have been converted to
          their ``main_encoding`` equivalents.
        r   r   )r   windows_1252zPWindows-1252 and ISO-8859-1 are the only currently supported embedded encodings.)r[  r   z4UTF-8 is the only currently supported main encoding.r   r3   r2   Nr"   )
r}   r   NotImplementedErrorrD   rY  rZ  rX  rW  r   rG   )rM   r\  r]  r^  byte_chunkschunk_startposbytestartendsizes              r    	detwinglezUnicodeDammit.detwingle;  s   . $$S#..4466 ?
 
 
 &&  
   (999%F   CMM!!C=Ds111dc>W6W6W ),(G  $E3u}}t$#*B"B"B ""8KO#<=== ""3#;D#ABBBq! q+ CMM!!, !O x5666xx$$$r"   )r   )r[  r   )&r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rK   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   rW  r   rX  rY  rZ  r   ri  r=   r"   r    r   r     sG        & &V :<EI24/337F$ F$F$ #+:"6F$ "'*@"AB	F$
 F$ $J/F$ !,F$ %Z0F$ F$ F$ F$V MMM
 SM!!! &*)))  	**** c]""" %	3/0000	KKK "(  u        R !- -OT#y.)   / / /    2:%# %#!%#+.%#	#%# %# %# %#P ?G+ ++%.+8;+	+ + + + /(; / / / X/)     "	i 	HSM 	 	 	 	!:!!:!: 	"!: 		!:
 	"!: 	#!: 	#!: 	#!: 	!: 	#!: 	"!: 	#!: 	!!: 	!: 	!!:  	!!:" 	#!:$ #"""!""!""#!!A!: !: !:Hd5%U38_ 4556 ! ! !VB+B+B+ 	B+ 		B+
 	B+ 	B+ 	B+ 	B+ 	B+ 	B+ 	B+ 	B+ 	B+ 	B+ 	B+  	!B+" 	#B+ B+$ 	%B+& 	'B+( 	)B+* 	+B+, 	-B+. 	/B+0 	1B+2 	3B+4 	5B+6 	7B+8 	9B+: 	;B+< 	=B+> 	?B+@ 	AB+B 	CB+D 	EB+ B+ B+F 	GB+H 	IB+J 	KB+N 	OB+P 	QB+R 	SB+T 	UB+V 	WB+X 	YB+Z 	[B+\ 	]B+^ 	_B+` 	aB+b 	cB+d 	eB+f 	gB+h 	iB+ B+ B+j 	kB+l 	mB+n 	oB+p 	qB+r 	sB+t 	uB+v 	wB+x 	yB+z 	{B+| 	}B+~ 	B+@ 	AB+B 	CB+D 	EB+F 	GB+H 	IB+J 	KB+ B+ B+L 	MB+N 	OB+P 	QB+R 	SB+T 	UB+V 	WB+X 	YB+Z 	[B+\ 	]B+^ 	_B+` 	aB+b 	cB+d 	eB+f 	gB+h 	iB+j 	kB+l 	mB+ B+ B+n 	oB+p 	qB+r 	sB+t 	uB+v 	wB+x 	yB+z 	{B+| 	}B+~ 	B+@ 	AB+B 	CB+D 	EB+F 	GB+H 	IB+J 	KB+L 	MB+N 	OB+ B+ B+P 	QB+R 	SB+T 	UB+V 	WB+X 	YB+Z 	[B+\ 	]B+^ 	_B+` 	aB+b 	cB+d 	eB+f 	gB+h 	iB+j 	kB+l 	mB+n 	oB+p 	qB+ B+r CB+ B+ B+tE3J' B B BV{.o{.o{. 	k{. 	o	{.
 	o{. 	o{. 	o{. 	k{. 	o{. 	k{. 	o{. 	k{. 	k{. 	o{. 	o{.  	o!{." 	o#{. {.$ 	o%{.& 	o'{.( 	o){.* 	k+{., 	o-{.. 	k/{.0 	o1{.2 	k3{.4 	k5{.6 	k7{.8 	k9{.: 	k;{.< 	k={.> 	k?{.@ 	kA{.B 	kC{.D 	kE{. {. {.F 	kG{.H 	kI{.J 	kK{.L 	kM{.N 	kO{.P 	kQ{.R 	kS{.T 	kU{.V 	kW{.X 	kY{.Z 	k[{.\ 	k]{.^ 	k_{.` 	ka{.b 	kc{.d 	ke{.f 	kg{. {. {.h 	ki{.j 	kk{.l 	km{.n 	ko{.p 	kq{.r 	ks{.t 	ku{.v 	kw{.x 	ky{.z 	k{{.| 	k}{.~ 	k{.@ 	kA{.B 	kC{.D 	kE{.F 	kG{.H 	kI{. {. {.J 	kK{.L 	kM{.N 	kO{.P 	kQ{.R 	kS{.T 	kU{.V 	kW{.X 	kY{.Z 	k[{.\ 	k]{.^ 	k_{.` 	ka{.b 	kc{.d 	ke{.f 	kg{.h 	ki{.j 	kk{. {. {.l 	km{.n 	ko{.p 	kq{.r 	ks{.t 	ku{.v 	kw{.x 	ky{.z 	g{{.| 	k}{.~ 	k{.@ 	kA{.B 	kC{.D 	kE{.F 	kG{.H 	kI{.J 	kK{.L 	kM{. {. {.N 	kO{.P 	kQ{.R 	kS{.T 	kU{.V 	kW{.X 	kY{.Z 	k[{.\ 	k]{.^ 	k_{.` 	ka{.b 	kc{.d 	ke{.f 	kg{.h 	ki{.j 	kk{.l 	km{.n 	ko{. {.p u{. {. {.$sEz* { { {|? ? ?eCcM&:!;    #>a"@"CCCCC "=R!@!C3CCC $*'5	D% D%D% !D% %	D%
 
D% D% D% [D% D% D%r"   r   )4r   __license__html.entitiesr   collectionsr   r   r   rK   loggingr   r   typesr	   typingr
   r   r   r   r   r   r   r   r   r   typing_extensionsr   bs4._typingr   r   r   r   r   cchardetImportErrorchardetcharset_normalizerr   r   r!   r#   r$   dictr%   rL   r   r   objectr*   r^   r   r   r=   r"   r    <module>rx     s      ( ( ( ( ( ( # # # # # #        				 % % % % % % % %                              & % % % % %         (,$ + + +OOONN    	    	%%%%/NN 	 	 	D	$(u (# ( ( ( ( @c ? ? ?; 
3   
 04tvvd4c7l++, 5 5 5BJy''00"$772:l))'22BD99 U 
 BJy"$''2:lBD)) S } } } } } } } }@  , , . . .v v v v v v v vrb
% b
% b
% b
% b
% b
% b
% b
% b
% b
%sZ   &A- -B3A:9B:B BBBBBBBBBB