§
    |Øj²E  ã                   óR  — U d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZmZ d	d
dœZeeeeef         f         ed<   dZ eh d£¦  «        Z ej        dej        ¦  «        Z ej        dej        ¦  «        Z ej        dej        ej        z  ¦  «        Z ej        dej        ¦  «        Z ej        d¦  «        Z  ej        d¦  «        Z! ej        dej        ¦  «        Z" ej        dej        ¦  «        Z# e$ e%dd¦  «        ¦  «        Z&dZ'de$de
dz  fd„Z(dededz  fd„Z)de$de
dz  fd„Z*d e$de$fd!„Z+dededz  fd"„Z,d#e$dee$e$f         fd$„Z-d e$dee$         fd%„Z.de$de
dz  fd&„Z/de$d'e
d(ee         de
fd)„Z0de$d*ede1fd+„Z2de$d*edz  dedz  fd,„Z3dS )-zñStage 1b: charset declaration extraction (HTML/XML/PEP 263) and promotion.

Note: ``from __future__ import annotations`` is intentionally omitted because
this module is compiled with mypyc, which does not support PEP 563 string
annotations.
é    N)ÚIterator©Údecodes_without_error)ÚEncodingEra)ÚDETERMINISTIC_CONFIDENCEÚDetectionResultÚPipelineContext)Úcompute_structural_score)ÚREGISTRYÚlookup_encoding)Ú	shift_jisÚcp932)Úeuc_krÚcp949)Úshift_jis_2004r   Ú_MARKUP_SUPERSET_PROMOTIONSi   >   úutf-16úutf-32ú	utf-16-beú	utf-16-leú	utf-32-beú	utf-32-les-   <\?xml\s[^<>]*encoding\s*=\s*['"]([^'"]+)['"]s   <meta\b([^<>]*)>s4   ([^\s=/>]+)(?:\s*=\s*(?:(["'])(.*?)\2|([^\s>"']+)))?s@   charset[\t\n\f\r ]*=[\t\n\f\r ]*(?:(["'])([^"']+)\1|([^\s;"']+))s&   ^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)s   ^[ \t\f]*(?:[#\r]|$)z<(?:meta|\?xml)[^<>]*zM(?:charset|encoding)\s*=\s*(?:[^\sA-Za-z0-9._-]\s*)?([A-Za-z][A-Za-z0-9._-]+)é€   é   g      Ð?ÚdataÚreturnc                 óÞ  — | dt           …         }t          |¦  «        t          |                     dt          ¦  «        ¦  «        z
  }|t          |¦  «        t          z  k     rdS |                     dd¬¦  «        }t                               |¦  «        D ]Ö}t                               | 	                    d¦  «        ¦  «        D ]¦}t          | 	                    d¦  «                             ¦   «         ¦  «        }|�nt          |         j        t          j        z  rOt!          | |¦  «        r?| 	                    d¦  «        d         dk    }t#          |t$          d|rdnd	¬
¦  «        c c S Œ§Œ×dS )u  Look for a charset declaration in EBCDIC-encoded markup.

    The ASCII regexes cannot see declarations in EBCDIC bytes, so when the
    head looks like EBCDIC text (dominated by high bytes), decode it through
    cp037 â€” the EBCDIC page whose letter and digit positions are shared by
    all variants â€” and scan the decoded text.  Only declarations naming a
    MAINFRAME-era encoding are honoured, and the declared encoding must
    decode the whole window.  The MIME type follows the tag the declaration
    sits in.
    NÚcp037Úreplace)Úerrorsr   é   ú?útext/xmlú	text/html©ÚencodingÚ
confidenceÚlanguageÚ	mime_type)Ú_SCAN_LIMITÚlenÚ	translateÚ_MARKUP_HIGH_BYTESÚ_EBCDIC_SCAN_MIN_HIGH_FRACTIONÚdecodeÚ_EBCDIC_TAG_REÚfinditerÚ_EBCDIC_DECL_REÚgroupr   Ústripr   Úerar   Ú	MAINFRAMEÚ_validate_bytesr   r   )r   ÚheadÚ
high_countÚdecodedÚtagÚmatchr&   Úis_xmls           úŒ/Users/tedpeters/Documents/Projects/Claude Projects/Misc Tools/notename-sheets/.venv/lib/python3.11/site-packages/chardet/pipeline/markup.pyÚ_detect_ebcdic_declarationr?   f   sj  € ð �•�Ô€DÝ�T‘”�S §¢°Õ6HÑ!IÔ!IÑJÔJÑJ€JØ•C˜‘I”IÕ >Ñ>Ò>Ð>ØˆtØ�kŠk˜'¨)ˆkÑ4Ô4€Gõ
 ×&Ò& wÑ/Ô/ð ð ˆÝ$×-Ò-¨c¯iªi¸©l¬lÑ;Ô;ð 	ð 	ˆEÝ& u§{¢{°1¡~¤~×';Ò';Ñ'=Ô'=Ñ>Ô>ˆHàÐ$Ý˜XÔ&Ô*­[Ô-BÑBð %å# D¨(Ñ3Ô3ð %ð Ÿš 1™œ aœ¨CÒ/�Ý&Ø%Ý7Ø!Ø,2ÐC˜j˜j¸ð	ñ ô ð ð ð ð ð øð	ð ˆ4ó    Úraw_namec                 óð   — | dd…                               ¦   «                              dd¦  «        }|dk    s|                     d¦  «        rdS |dv s|                     d¦  «        rd	S t          | ¦  «        S )
a«  Resolve a coding cookie the way :func:`tokenize.detect_encoding` does.

    Python folds the ``utf-8-*`` spellings to UTF-8 and ``latin-1``,
    ``iso-8859-1``, and ``iso-latin-1`` (with or without a suffix) to
    Latin-1 before it consults the codec registry, so ``iso-latin-1`` is a
    valid cookie even though no codec answers to it, and ``utf-8-sig``
    names plain UTF-8 rather than a file that starts with a mark.
    Né   Ú_ú-úutf-8zutf-8-)zlatin-1z
iso-8859-1ziso-latin-1)zlatin-1-ziso-8859-1-ziso-latin-1-z	iso8859-1)Úlowerr   Ú
startswithr   )rA   Únames     r>   Ú_pep263_encodingrJ   Œ   s‚   € ð �C�R�CŒ=×ÒÑ Ô ×(Ò(¨¨cÑ2Ô2€DØˆw‚€˜$Ÿ/š/¨(Ñ3Ô3€ØˆwØÐ7Ð7Ð7¸4¿?º?Ø3ñ<ô <Ð7ð ˆ{Ý˜8Ñ$Ô$Ð$r@   c                 ó`  — | dt           …         }d|vrdS |                     dd¦  «        }t                               |d         ¦  «        }|€St	          |¦  «        dk    r@t
                               |d         ¦  «        r t                               |d         ¦  «        }|r‹	 |                     d¦  «                             d¦  «                             ¦   «         }n# t          t          f$ r Y dS w xY wt          | t          |¦  «        ¦  «        }|�t          |t          dd¬	¦  «        S dS )
aµ  Check the first two lines of *data* for a PEP 263 encoding declaration.

    PEP 263 declarations (e.g. ``# -*- coding: utf-8 -*-``) are only valid
    on line 1 or line 2 of a Python source file.  Both lines have to start
    inside the first :data:`_SCAN_LIMIT` bytes, the same bound the markup
    scan uses.

    :param data: The raw byte data to scan.
    :returns: A :class:`DetectionResult` with confidence 0.95, or ``None``.
    Nó   #ó   
é   r   r!   Úasciiztext/x-pythonr%   )r*   ÚsplitÚ
_PEP263_REr<   r+   Ú_PEP263_BLANK_REr3   r/   r4   ÚUnicodeDecodeErrorÚ
ValueErrorÚ_declared_encodingrJ   r   r   )r   r8   Úlinesr<   rA   r&   s         r>   Ú_detect_pep263rW   Ÿ   s4  € ð �•�Ô€Dà�4ÐÐØˆtà�JŠJ�u˜aÑ Ô €EÝ×Ò˜U 1œXÑ&Ô&€EØ€}�˜U™œ aš˜Õ,<×,BÒ,BÀ5ÈÄ8Ñ,LÔ,L˜Ý× Ò   q¤Ñ*Ô*ˆØð ð	Ø—{’{ 1‘~”~×,Ò,¨WÑ5Ô5×;Ò;Ñ=Ô=ˆHˆHøÝ"¥JÐ/ð 	ð 	ð 	Ø�4�4ð	øøøå% dÕ,<¸XÑ,FÔ,FÑGÔGˆØÐÝ"Ø!Ý3ØØ)ð	ñ ô ð ð ˆ4s   Â$:C ÃC4Ã3C4r8   c                 ó˜  — |                       d¦  «        }|dk     r| S g }d}|dk    rr|                     | ||…         ¦  «         |                       d|dz   ¦  «        }|dk     rd                     |¦  «        S |dz   }|                       d|¦  «        }|dk    °r|                     | |d…         ¦  «         d                     |¦  «        S )aT  Return *head* without its ``<!-- -->`` comments.

    A declaration inside a comment is not a declaration, and the WHATWG
    prescan skips comments for that reason.  As in the prescan, the dashes
    that open a comment may also close it (``<!-->`` is a whole comment),
    and a comment that never closes runs to the end of the head.
    s   <!--r   s   -->rN   r@   é   N)ÚfindÚappendÚjoin)r8   ÚstartÚpartsÚposÚends        r>   Ú_strip_commentsra   Ã   sÓ   € ð �IŠI�gÑÔ€EØˆq‚y€yØˆØ€EØ
€CØ
�1Š*ˆ*Ø�Š�T˜#˜e˜)”_Ñ%Ô%Ð%Ø�iŠi˜ ¨¡	Ñ*Ô*ˆØ�Š7ˆ7Ø—8’8˜E‘?”?Ð"Ø�A‰gˆØ—	’	˜' 3Ñ'Ô'ˆð �1Š*ˆ*ð 
‡L‚L��c�d�d”ÑÔÐØ�8Š8�E‰?Œ?Ðr@   c                 ón   — |                       ¦   «         dk    rdS t          | ¦  «        }|t          v rdS |S )z9Resolve a charset label found in ASCII-compatible markup.zx-user-definedÚcp1252rF   )rG   r   Ú_SELF_CONTRADICTING_LABELS)rA   r&   s     r>   Ú_markup_encodingre   Û   s@   € à‡~‚~ÑÔÐ+Ò+Ð+ØˆxÝ˜xÑ(Ô(€HØÕ-Ð-Ð-ØˆwØ€Or@   Úbodyc                 ó  — i }t                                | ¦  «        D ]a}|                     d¦  «                             ¦   «         }||v rŒ.|                     d¦  «        }|€|                     d¦  «        }|�|||<   Œb|S )z:Return the attributes of one meta tag, lowercased by name.r!   rY   Né   )Ú_ATTR_REr1   r3   rG   )rf   Úattrsr<   rI   Úvalues        r>   Ú_meta_attributesrl   å   sƒ   € à "€EÝ×"Ò" 4Ñ(Ô(ð  ð  ˆØ�{Š{˜1‰~Œ~×#Ò#Ñ%Ô%ˆØ�5ˆ=ˆ=ØØ—’˜A‘”ˆØˆ=Ø—K’K ‘N”NˆEØÐØˆE�$‰KøØ€Lr@   c              #   ó„  K  — t                                | ¦  «        D �]!}t          |                     d¦  «        ¦  «        }|                     d¦  «        }|�|                     ¦   «         r|V — ŒU|                     d¦  «        }|€|                     d¦  «        }|                     d¦  «        }|�‰|�‡|                     ¦   «                              ¦   «         dk    r]t                               |¦  «        }|rA|                     d¦  «        r|                     d¦  «        n|                     d	¦  «        V — �Œ#dS )
a‹  Yield charset labels from real meta declarations, in document order.

    ``charset=`` inside some other attribute (a refresh URL, a description)
    is not a declaration. A declaration is a ``charset`` attribute, or the
    ``charset`` parameter of an ``http-equiv="content-type"`` tag.
    ``name="content-type"`` is wrong by the spec but common enough in the
    wild that it counts too.
    r!   s   charsetNs
   http-equivs   names   contents   content-typerN   rY   )	Ú_META_TAG_REr1   rl   r3   Úgetr4   rG   Ú_CONTENT_CHARSET_REÚsearch)r8   r;   rj   ÚcharsetÚequivÚcontentÚfounds          r>   Ú_iter_html_charsetsrv   ô   s-  è è € õ ×$Ò$ TÑ*Ô*ð Kñ KˆÝ  §¢¨1¡¤Ñ.Ô.ˆØ—)’)˜JÑ'Ô'ˆØÐ 7§=¢=¡?¤?ÐØˆMˆMˆMØØ—	’	˜-Ñ(Ô(ˆØˆ=Ø—I’I˜gÑ&Ô&ˆEØ—)’)˜JÑ'Ô'ˆàÐØÐ#Ø—’‘”×#Ò#Ñ%Ô%¨Ò8Ð8å'×.Ò.¨wÑ7Ô7ˆEØð KØ(-¯ª°A©¬ÐJ�e—k’k !‘n”n�n¸E¿KºKÈ¹N¼NÐJÐJÐJùð#Kð Kr@   c                 óæ  — | sdS t          | dt          …         ¦  «        }t                               |¦  «        }|r�	 |                     d¦  «                             d¦  «                             ¦   «         }n# t          t          f$ r d}Y nw xY wt          | |rt          |¦  «        nd¦  «        }|�t          |t          dd¬¦  «        S t          |¦  «        D ]t}	 |                     d¦  «                             ¦   «         }n# t          $ r Y Œ7w xY wt          | t          |¦  «        ¦  «        }|�t          |t          dd¬¦  «        c S Œut          | ¦  «        }|�|S t          | ¦  «        S )aì  Scan the first bytes of *data* for a charset declaration.

    Checks for:

    1. ``<?xml ... encoding="..."?>``
    2. ``<meta charset="...">``
    3. ``<meta http-equiv="Content-Type" content="...; charset=...">``
       (also with ``name`` in place of ``http-equiv``)
    4. PEP 263 ``# -*- coding: ... -*-`` (first two lines only)

    Markup inside ``<!-- -->`` comments is not scanned.  A UTF-16 or UTF-32
    label is read as UTF-8 and ``x-user-defined`` as windows-1252, as in the
    WHATWG prescan, since a declaration found in ASCII bytes cannot be
    describing UTF-16 text.

    A declaration counts only if the declared encoding decodes the whole of
    *data*, or, failing that, if its Windows superset does (see
    :func:`_declared_encoding`).  A declaration that fails is skipped and
    the next one is tried, so a stale prolog does not hide a correct meta.

    :param data: The raw byte data to scan.
    :returns: A :class:`DetectionResult` with confidence 0.95, or ``None``.
    Nr!   rO   Ú r#   r%   r$   )ra   r*   Ú_XML_ENCODING_RErq   r3   r/   r4   rS   rT   rU   re   r   r   rv   r?   rW   )r   r8   r<   rA   r&   ÚrawÚebcdic_results          r>   Údetect_markup_charsetr|     sÃ  € ð0 ð Øˆtå˜4 ¥ Ô-Ñ.Ô.€Då×#Ò# DÑ)Ô)€EØð ð	Ø—{’{ 1‘~”~×,Ò,¨WÑ5Ô5×;Ò;Ñ=Ô=ˆHˆHøÝ"¥JÐ/ð 	ð 	ð 	ØˆHˆHˆHð	øøøå%Ø°ÐBÕ" 8Ñ,Ô,Ð,¸dñ
ô 
ˆð ÐÝ"Ø!Ý3ØØ$ð	ñ ô ð õ # 4Ñ(Ô(ð ð ˆð	Ø—z’z 'Ñ*Ô*×0Ò0Ñ2Ô2ˆHˆHøÝ!ð 	ð 	ð 	ØˆHð	øøøå% dÕ,<¸XÑ,FÔ,FÑGÔGˆØÐÝ"Ø!Ý3ØØ%ð	ñ ô ð ð ð ð  õ /¨tÑ4Ô4€MØÐ ØÐå˜$ÑÔÐs#   ¾:A9 Á9BÂBÃ'DÄ
DÄDÚmarkup_resultÚallowedc                 óæ  — |j         €|S t                               |j         ¦  «        }|€|S |\  }}||vr|S t          ||j        |j        |j        ¦  «        }| dt          …         }t          ¦   «         }t          |t          |j                  |¦  «        }	t          |t          |         |¦  «        }
|
|	k    rt          | |¦  «        r|S |S t          | |¦  «        r|S t          | |¦  «        r|S |S )aï  Promote a markup-declared encoding to its superset when structural evidence supports it.

    If the declared encoding has a known superset (per
    :data:`_MARKUP_SUPERSET_PROMOTIONS`), the superset validates the data,
    and the superset's structural score is materially better, return a new
    result using the superset encoding.  Otherwise return *markup_result*
    unchanged.

    The decode checks read the whole input: what a caller can actually
    ``.decode()`` is a fact about their entire input, and a declared
    Shift_JIS page typically reaches its first CP932-only byte well past
    the header.  The structural comparison reads only the first
    :data:`_SCAN_LIMIT` bytes, since multi-byte structure is uniform enough
    that the ranking has converged by then.  It runs first because it is
    cheap and settles which single decode the answer needs: a 200 kB
    window decodes in a few hundred microseconds, and every declared page
    of a promotable encoding comes through here.
    N)r&   r   ro   r   r'   r(   r)   r*   r	   r
   r   r   )r   r}   r~   Ú	promotionÚreported_codecÚsuperset_nameÚpromotedr8   ÚctxÚ
base_scoreÚsuperset_scores              r>   Úpromote_markup_supersetr‡   T  s  € ð. ÔÐ%ØÐÝ+×/Ò/°Ô0FÑGÔG€IØÐØÐØ$-Ñ!€N�MØ˜GÐ#Ð#ØÐÝØØÔ ØÔØÔñ	ô €Hð �•�Ô€DÝ
Ñ
Ô
€CÝ)¨$µ¸Ô9OÔ0PÐRUÑVÔV€JÝ-¨dµH¸]Ô4KÈSÑQÔQ€NØ˜
Ò"Ð"å   }Ñ5Ô5ð 	ØˆOØÐõ
 ˜T >Ñ2Ô2ð ØÐÝ˜T =Ñ1Ô1ð ØˆØÐr@   r&   c                 ó"   — t          | |¦  «        S )a}  Check that the whole of *data* decodes under *encoding*.

    A declaration is an answer only if the caller can decode what chardet
    examined with it.  The decoder stops at the first bad sequence, so a
    declaration that lies about the header fails as fast as a head-only
    check would, and a cut multi-byte sequence at the very end is tolerated
    as everywhere else.
    r   )r   r&   s     r>   r7   r7   �  s   € õ !  xÑ0Ô0Ð0r@   c                 ó¦   — |€dS t          | |¦  «        r|S t                               |¦  «        }|�t          | |d         ¦  «        r|d         S dS )aÑ  Return the name to report for a declaration of *encoding*, or ``None``.

    When the declared encoding cannot decode the window but its Windows
    superset can (a declared EUC-KR page whose first CP949-only hangul sits
    past the header, a declared Shift_JIS page using NEC extensions), the
    superset is the answer the caller can use, so it is reported directly.
    :func:`promote_markup_superset` would reach the same name one window
    decode later.
    Nr!   )r7   r   ro   )r   r&   r€   s      r>   rU   rU   ™  sa   € ð ÐØˆtÝ�t˜XÑ&Ô&ð ØˆÝ+×/Ò/°Ñ9Ô9€IØÐ¥°°yÀ´|Ñ!DÔ!DÐØ˜Œ|ÐØˆ4r@   )4Ú__doc__ÚreÚcollections.abcr   Úchardet._utilsr   Úchardet.enumsr   Úchardet.pipeliner   r   r	   Úchardet.pipeline.structuralr
   Úchardet.registryr   r   r   ÚdictÚstrÚtupleÚ__annotations__r*   Ú	frozensetrd   ÚcompileÚ
IGNORECASEry   rn   ÚDOTALLri   rp   rQ   rR   r0   r2   ÚbytesÚranger-   r.   r?   rJ   rW   ra   re   rl   rv   r|   r‡   Úboolr7   rU   © r@   r>   ú<module>rž      sÒ  ððð ð ð 
€	€	€	Ø $Ð $Ð $Ð $Ð $Ð $à 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø %Ð %Ð %Ð %Ð %Ð %Ø WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WØ @Ð @Ð @Ð @Ð @Ð @Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6ð -Ø!ð;ð ;Ð ˜T # u¨S°#¨X¤Ð"6Ô7ð ð ñ ð
 €ð '˜YØLÐLÐLñô Ð ð �2”:Ø9¸2¼=ñô Ð ð ˆrŒzÐ.°´Ñ>Ô>€àˆ2Œ:Ø@Ø„M�B”IÑñô €ð !�b”jØLØ„Mñô Ð ð ˆRŒZÐBÑCÔC€
Ø�2”:Ð6Ñ7Ô7Ð ð* �”Ð4°b´mÑDÔD€Ø�"”*ØTØ„Mñô €ð �U˜5˜5  uÑ-Ô-Ñ.Ô.Ð ð "&Ð ð# Uð #¨ÀÑ/Eð #ð #ð #ð #ðL%˜sð % s¨T¡zð %ð %ð %ð %ð&!˜ð ! ?°TÑ#9ð !ð !ð !ð !ðH˜%ð  Eð ð ð ð ð0˜sð  s¨T¡zð ð ð ð ð˜5ð  T¨%°¨,Ô%7ð ð ð ð ðK˜eð K¨°¬ð Kð Kð Kð Kð:@  ð @ ¨/¸DÑ*@ð @ ð @ ð @ ð @ ðF6Ø
ð6à"ð6ð �sŒ^ð6ð ð	6ð 6ð 6ð 6ðr	1˜%ð 	1¨3ð 	1°4ð 	1ð 	1ð 	1ð 	1ð˜Uð ¨c°D©jð ¸SÀ4¹Zð ð ð ð ð ð r@   