a
    Dgm                     @   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eed	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 ]4\}}|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   Z/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/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   s
   | j  S r   )r   startr   r   r   r   r   +   s    zTagMatch.startc                 C   s
   | j  S r   )r   endr   r   r   r   r   /   s    zTagMatch.endN)
__name__
__module____qualname____doc__r   r   r   propertyr   r   r   r   r   r   r       s   

r   c                   @   sr   e Zd ZdddZdd Zdd Zdd	 Zeed
ddZee	ee
e f d
ddZeed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   s   t d S r   NotImplementedErrorr   r   r   r   get_opening_tag_regex:   s    z!TagRewriter.get_opening_tag_regexc                 C   s   t dS )z;Given a dict of attributes from a tag, return the tag type.Nr)   r   r   r   r   r   get_tag_type_from_attrs=   s    z#TagRewriter.get_tag_type_from_attrsc                 C   s   t dS )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.
        Nr)   )r   tag_type
attrs_listr   r   r   get_tag_replacementsA   s    z TagRewriter.get_tag_replacements)htmlr   c                 C   s   |  |}g }| D ]L\}}dd |D }| ||}|s>qt||D ]\}}	|	|_|| qHq|jdd d d}
|D ]H}|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       z(TagRewriter.__call__.<locals>.<listcomp>c                 S   s   | j S r   )r   )r   r   r   r   <lambda>Z   r4   z&TagRewriter.__call__.<locals>.<lambda>)keyr   )
extract_tagsitemsr0   zipr   appendsortr   r   len)r   r1   matches_by_tag_typeZmatches_to_replacer.   Ztag_matchesZ
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   r-   r   r:   )r   r1   r=   
re_patternre_matchZ	tag_matchr.   r   r   r   r7   h   s    zTagRewriter.extract_tags)ruler   c                    s    fdd}|S )Nc                    s   t t | S r   )rA   map)argsrE   r   r   	bulk_rule{   s    z8TagRewriter.convert_rule_to_bulk_rule.<locals>.bulk_ruler   )r   rE   rI   r   rH   r   convert_rule_to_bulk_rulez   s    z%TagRewriter.convert_rule_to_bulk_rulec                 c   sN   |   }||D ]6}t|}| |}|| jvr4q| j| |E d H  qg S r   )r+   r   r   r-   r(   )r   r1   rC   r   r   r.   r   r   r   extract_references   s    

zTagRewriter.extract_references)NNN)r    r!   r"   r   r+   r-   r0   strr@   dictrA   r   r7   r   rJ   rK   r   r   r   r   r%   4   s   
r%   c                   @   s(   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   s   t S r   )FIND_EMBED_TAGr   r   r   r   r+      s    z#EmbedRewriter.get_opening_tag_regexc                 C   s
   | dS )NZ	embedtype)getr,   r   r   r   r-      s    z%EmbedRewriter.get_tag_type_from_attrsc                 C   sp   z| j | }W n ty$   d }Y n0 |sVz| j| }W n tyJ   Y n0 | |}|rb||S dgt| S )N r'   KeyErrorr&   rJ   r<   r   r.   r/   rE   r   r   r   r0      s    

z"EmbedRewriter.get_tag_replacementsNr    r!   r"   r#   r+   r-   r0   r   r   r   r   rN      s   rN   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	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   s   t S r   )
FIND_A_TAGr   r   r   r   r+      s    z"LinkRewriter.get_opening_tag_regexc                 C   sb   z
|d W S  t y\   |dd }|rX|dr8Y dS |drHY dS |drXY dS Y n0 d S )	Nlinktypehref)zhttp:zhttps:externalzmailto:email#anchor)rS   rP   
startswith)r   r   rY   r   r   r   r-      s    



z$LinkRewriter.get_tag_type_from_attrsc                 C   s   |sg S z| j | }W n ty,   d }Y n0 |snz| j| }W n" tyb   |dv r^g  Y S Y n0 | |}|rz||S dgt| S )N)r[   rZ   r]   z<a>rR   rT   r   r   r   r0      s    

z!LinkRewriter.get_tag_replacementsNrU   r   r   r   r   rV      s   rV   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	MultiRuleRewriterz:Rewrites HTML by applying a sequence of rewriter functionsc                 C   s
   || _ d S r   	rewriters)r   ra   r   r   r   r      s    zMultiRuleRewriter.__init__c                 C   s   | j D ]}||}q|S r   r`   )r   r1   Zrewriter   r   r   r@      s    

zMultiRuleRewriter.__call__c                 c   s    | j D ]}||E d H  qd S r   )ra   rK   )r   r1   Zrewriterr   r   r   rK      s    
z$MultiRuleRewriter.extract_referencesN)r    r!   r"   r#   r   r@   rK   r   r   r   r   r_      s   r_   )r#   recollectionsr   typingr   Zdjango.utils.functionalr   compilerW   rO   r   rL   rM   r   r   r%   rN   rV   r_   r   r   r   r   <module>   s   


Z8