o
    hm                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ edZ	edZ
edZd	ed
efddZG dd dZG dd dZG dd deZG dd deZG dd dZdS )z=
Utility classes for rewriting elements of HTML-like strings
    N)defaultdict)Callable)cached_propertyz<a(\b[^>]*)>z<embed(\b[^>]*)/>z([\w-]+)\="([^"]*)"attr_stringreturnc                 C   sH   i }t | D ]\}}|dddddddd}|||< q|S )	zR
    helper method to extract tag attributes, as a dict of un-escaped strings
    z&lt;<z&gt;>z&quot;"z&amp;&)
FIND_ATTRSfindallreplace)r   
attributesnameval r   [/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/rich_text/rewriters.pyextract_attrs   s   

r   c                   @   s<   e Zd ZdZdd Zedd Zedd Zedd	 Z	d
S )TagMatchz5Represents a single matched tag in a rich text stringc                 C   s   || _ d | _d S N)matchreplacement)selfr   r   r   r   __init__#   s   
zTagMatch.__init__c                 C   s   t | jdS )N   )r   r   groupr   r   r   r   attrs'   s   zTagMatch.attrsc                 C   
   | j  S r   )r   startr   r   r   r   r   +      
zTagMatch.startc                 C   r   r   )r   endr   r   r   r   r!   /   r    zTagMatch.endN)
__name__
__module____qualname____doc__r   r   r   propertyr   r!   r   r   r   r   r       s    

r   c                   @   sx   e Zd ZdddZdd Zdd Zdd	 Zd
edefddZd
ede	ee
e f fddZdedefddZdd ZdS )TagRewriterNc                 C   s"   |pi | _ |pi | _|pi | _d S r   )rules
bulk_rulesreference_extractors)r   r(   r)   r*   r   r   r   r   5   s   

zTagRewriter.__init__c                 C      t r   NotImplementedErrorr   r   r   r   get_opening_tag_regex:      z!TagRewriter.get_opening_tag_regexc                 C   r+   )z;Given a dict of attributes from a tag, return the tag type.r,   r   r   r   r   r   get_tag_type_from_attrs=   s   z#TagRewriter.get_tag_type_from_attrsc                 C   r+   )zGiven a list of attribute dicts, all taken from tags of the same type, return a
        corresponding list of replacement strings to replace the tags with.

        Return an empty list for cases when you don't want any replacements made.
        r,   )r   tag_type
attrs_listr   r   r   get_tag_replacementsA   s   z TagRewriter.get_tag_replacementshtmlr   c                 C   s   |  |}g }| D ]&\}}dd |D }| ||}|sqt||D ]\}}	|	|_|| q$q|jdd d d}
|D ]$}|d |j|
  |j ||j|
 d   }|
t	|j|j |j 7 }
q>|S )Nc                 S   s   g | ]}|j qS r   )r   ).0r   r   r   r   
<listcomp>O   s    z(TagRewriter.__call__.<locals>.<listcomp>c                 S   s   | j S r   )r   )r   r   r   r   <lambda>Z   s    z&TagRewriter.__call__.<locals>.<lambda>)keyr   )
extract_tagsitemsr4   zipr   appendsortr   r!   len)r   r5   matches_by_tag_typematches_to_replacer2   tag_matches
attr_dictsreplacementsr   r   offsetr   r   r   __call__I   s,   
zTagRewriter.__call__c                 C   sF   t t}|  }||D ]}t|}| |j}|| | q|S )zHelper method to extract and group HTML tags and their attributes.

        Returns a dict of TagMatch objects, mapping tag types to a list of all TagMatch objects of that tag type.
        )r   listr.   finditerr   r1   r   r=   )r   r5   r@   
re_patternre_match	tag_matchr2   r   r   r   r:   h   s   zTagRewriter.extract_tagsrulec                    s    fdd}|S )Nc                    s   t t | S r   )rG   map)argsrL   r   r   	bulk_rule{   s   z8TagRewriter.convert_rule_to_bulk_rule.<locals>.bulk_ruler   )r   rL   rP   r   rO   r   convert_rule_to_bulk_rulez   s   z%TagRewriter.convert_rule_to_bulk_rulec                 c   sP    |   }||D ]}t|}| |}|| jvrq
| j| |E d H  q
g S r   )r.   r   r   r1   r*   )r   r5   rI   r   r   r2   r   r   r   extract_references   s   

zTagRewriter.extract_references)NNN)r"   r#   r$   r   r.   r1   r4   strrF   dictrG   r   r:   r   rQ   rR   r   r   r   r   r'   4   s    
r'   c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	EmbedRewriterz
    Rewrites <embed embedtype="foo" /> tags within rich text into the HTML
    fragment given by the embed rule for 'foo'. Each embed rule is a function
    that takes a dict of attributes and returns the HTML fragment.
    c                 C      t S r   )FIND_EMBED_TAGr   r   r   r   r.      r/   z#EmbedRewriter.get_opening_tag_regexc                 C   s
   | dS )N	embedtype)getr0   r   r   r   r1         
z%EmbedRewriter.get_tag_type_from_attrsc                 C   sp   z| j | }W n ty   d }Y nw |s+z| j| }W n	 ty%   Y nw | |}|r1||S dgt| S )N r)   KeyErrorr(   rQ   r?   r   r2   r3   rL   r   r   r   r4      s   
z"EmbedRewriter.get_tag_replacementsNr"   r#   r$   r%   r.   r1   r4   r   r   r   r   rV      s
    rV   c                   @   rU   )	LinkRewriterz
    Rewrites <a linktype="foo"> tags within rich text into the HTML fragment
    given by the rule for 'foo'. Each link rule is a function that takes a dict
    of attributes and returns the HTML fragment for the opening tag (only).
    c                 C   rW   r   )
FIND_A_TAGr   r   r   r   r.      r/   z"LinkRewriter.get_opening_tag_regexc                 C   sf   z|d W S  t y2   |dd }|r,|drY dS |dr$Y dS |dr/Y dS Y d S Y d S w )	Nlinktypehref)zhttp:zhttps:externalzmailto:email#anchor)r^   rZ   
startswith)r   r   rd   r   r   r   r1      s   



z$LinkRewriter.get_tag_type_from_attrsc                 C   s   |sg S z| j | }W n ty   d }Y nw |s7z| j| }W n ty1   |dv r/g  Y S Y nw | |}|r=||S dgt| S )N)rf   re   rh   z<a>r]   r_   r   r   r   r4      s"   
	z!LinkRewriter.get_tag_replacementsNr`   r   r   r   r   ra      s
    ra   c                   @   rU   )	MultiRuleRewriterz:Rewrites HTML by applying a sequence of rewriter functionsc                 C   s
   || _ d S r   	rewriters)r   rl   r   r   r   r      r[   zMultiRuleRewriter.__init__c                 C   s   | j D ]}||}q|S r   rk   )r   r5   rewriter   r   r   rF      s   

zMultiRuleRewriter.__call__c                 c   s"    | j D ]
}||E d H  qd S r   )rl   rR   )r   r5   rewriterr   r   r   rR      s   
z$MultiRuleRewriter.extract_referencesN)r"   r#   r$   r%   r   rF   rR   r   r   r   r   rj      s
    rj   )r%   recollectionsr   typingr   django.utils.functionalr   compilerb   rX   r   rS   rT   r   r   r'   rV   ra   rj   r   r   r   r   <module>   s    


Z8