U
    {h                     @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlm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 e Zedddd Zdd Zdd Z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eZ dS )    N)	lru_cache)unescape)List)MaxLengthValidator)Model)render_to_string)
strip_tags)	mark_safe)FeatureRegistry)EmbedRewriterLinkRewriterMultiRuleRewriter)maxsizec                  C   sd   t  } t  }ttdd | D dd | D dtdd |  D dd |  D dgS )Nc                 S   s   i | ]\}}||j qS  expand_db_attributes_many.0linktypehandlerr   r   b/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/rich_text/__init__.py
<dictcomp>   s    z get_rewriter.<locals>.<dictcomp>c                 S   s   i | ]\}}||j qS r   extract_referencesr   r   r   r   r   "   s    )Z
bulk_rulesZreference_extractorsc                 S   s   i | ]\}}||j qS r   r   r   Z	embedtyper   r   r   r   r   (   s    c                 S   s   i | ]\}}||j qS r   r   r   r   r   r   r   ,   s    )featuresZget_embed_typesZget_link_typesr   r   itemsr   )Zembed_rulesZ
link_rulesr   r   r   get_rewriter   s*    
r   c                 C   s   t  }|| S )z\
    Expand database-representation HTML into proper HTML usable on front-end templates
    )r   htmlZrewriterr   r   r   expand_db_html5   s    r    c                 c   s   t  }|| E d H  d S N)r   r   r   r   r   r   !extract_references_from_rich_text=   s    r"   c                 C   s8   t jdd| t jd} t jdd| t jd} tt|  S )z
    Return a plain text version of a rich text string, suitable for search indexing;
    like Django's strip_tags, but ensures that whitespace is left between block elements
    so that <p>hello</p><p>world</p> gives "hello world", not "helloworld".
    z(</(p|h\d|li|blockquote)>)z\1 )flagsz(<(br|hr)\s*/>))resub
IGNORECASEr   r   strip)Zrichtextr   r   r   get_text_for_indexingB   s       r(   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )RichTexta  
    A custom object used to represent a renderable rich text value.
    Provides a 'source' property to access the original source code,
    and renders to the front-end HTML rendering.
    Used as the native value of a wagtailcore.blocks.field_block.RichTextBlock.
    c                 C   s   |pd| _ d S )N )source)selfr+   r   r   r   __init__Y   s    zRichText.__init__c                 C   s   t ddt| jiS )Nz wagtailcore/shared/richtext.htmlr   )r   r    r+   r,   r   r   r   __html__\   s     zRichText.__html__c                 C   s   t |  S r!   )r	   r/   r.   r   r   r   __str__a   s    zRichText.__str__c                 C   s
   t | jS r!   )boolr+   r.   r   r   r   __bool__d   s    zRichText.__bool__c                 C   s   t |tr| j|jkS dS )NF)
isinstancer)   r+   )r,   otherr   r   r   __eq__g   s    
zRichText.__eq__N)	__name__
__module____qualname____doc__r-   r/   r0   r2   r5   r   r   r   r   r)   Q   s   r)   c                   @   s   e Zd ZdZedd ZeeedddZ	ee
e e
e ddd	Zeeedd
dZee
e e
e dddZedd ZdS )EntityHandleraW  
    An 'entity' is a placeholder tag within the saved rich text, which needs to be rewritten
    into real HTML at the point of rendering. Typically (but not necessarily) the entity will
    be a reference to a model to be fetched to have its data output into the rich text content
    (so that we aren't storing potentially changeable data within the saved rich text).

    An EntityHandler defines how this rewriting is performed.

    Currently Wagtail supports two kinds of entity: links (represented as <a linktype="...">...</a>)
    and embeds (represented as <embed embedtype="..." />).
    c                   C   s   t dS )zh
        If supported, returns the type of model able to be handled by this handler, e.g. Page.
        NNotImplementedErrorr   r   r   r   	get_modelz   s    zEntityHandler.get_model)attrsreturnc                 C   s   |   }|jj|d dS )Nidr@   )r=   _default_managerget)clsr>   modelr   r   r   get_instance   s    zEntityHandler.get_instance)
attrs_listr?   c                    sF   |   }dd |D }|j|}dd | D   fdd|D S )Nc                 S   s   g | ]}| d qS rA   )rC   )r   r>   r   r   r   
<listcomp>   s     z*EntityHandler.get_many.<locals>.<listcomp>c                 S   s   i | ]\}}t ||qS r   )str)r   kvr   r   r   r      s      z*EntityHandler.get_many.<locals>.<dictcomp>c                    s   g | ]}  t|qS r   )rC   rI   )r   Zid_Zinstances_by_str_idr   r   rH      s     )r=   rB   Zin_bulkr   )rD   rG   rE   Zinstance_idsZinstances_by_idr   rL   r   get_many   s
    zEntityHandler.get_manyc                 C   s   t dS )z
        Given a dict of attributes from the entity tag
        stored in the database, returns the real HTML representation.
        Nr;   )r>   r   r   r   expand_db_attributes   s    z"EntityHandler.expand_db_attributesc                 C   s   t t| j|S )z
        Given a list of attribute dicts from a list of entity tags stored in
        the database, return the real HTML representation of each one.
        )listmaprN   )rD   rG   r   r   r   r      s    z'EntityHandler.expand_db_attributes_manyc                 C   s   g S )z
        Yields a sequence of (content_type_id, object_id, model_path, content_path) tuples for the
        database objects referenced by this entity, as per
        wagtail.models.ReferenceIndex._extract_references_from_object
        r   )rD   r>   r   r   r   r      s    z EntityHandler.extract_referencesN)r6   r7   r8   r9   staticmethodr=   classmethoddictr   rF   r   rM   rI   rN   r   r   r   r   r   r   r:   m   s   
r:   c                   @   s   e Zd ZdS )LinkHandlerNr6   r7   r8   r   r   r   r   rT      s   rT   c                   @   s   e Zd ZdS )EmbedHandlerNrU   r   r   r   r   rV      s   rV   c                   @   s   e Zd ZdZdd ZdS )RichTextMaxLengthValidatorz
    A variant of MaxLengthValidator that only counts text (not HTML tags) towards the limit
    Un-escapes entities for consistency with client-side character count.
    c                 C   s   t tt|S r!   )lenr   r   )r,   xr   r   r   clean   s    z RichTextMaxLengthValidator.cleanN)r6   r7   r8   r9   rZ   r   r   r   r   rW      s   rW   )!r$   	functoolsr   r   r   typingr   Zdjango.core.validatorsr   Zdjango.db.modelsr   Zdjango.template.loaderr   Zdjango.utils.htmlr   Zdjango.utils.safestringr	   Z"wagtail.rich_text.feature_registryr
   Zwagtail.rich_text.rewritersr   r   r   r   r   r    r"   r(   r)   r:   rT   rV   rW   r   r   r   r   <module>   s*   
;