o
    h                     @   s   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lmZ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mZmZ d dlmZ G dd dZ dS )    )groupby)
attrgetter)ListOptionalTuple)Command)render_decoratorsshould_render_decorators)	BLOCK_MAP	STYLE_MAP)DOM)EntityState)Options)
StyleState)BlockConfigContentStateElement	EntityMap)WrapperStatec                   @   s   e Zd ZdZdZddee ddfddZddee de	fd	d
Z
dedededefddZdedeee	ee f  fddZdedee fddZdS )HTMLzu
    Entry point of the exporter. Combines entity, wrapper and style state
    to generate the right HTML nodes.
    )composite_decoratorsentity_optionsblock_optionsstyle_optionsNconfigreturnc                 C   sn   |d u ri }| dg | _t| di | _t| dt| _t| dt	| _
t| dtj d S )Nr   entity_decorators	block_map	style_mapengine)getr   r   map_entitiesr   
map_blocksr
   r   
map_stylesr   r   r   useSTRING)selfr    r(   U/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/draftjs_exporter/html.py__init__&   s   zHTML.__init__content_statec           
      C   s   |du ri }| dg }t| j|}t }| di }d}|D ]#}d|v r*|d nd}| |||}	||kr9|}|dkrCt||	 q |dkrY|j dkrYt||j	 j
 t|S )zN
        Starts the export process on a given piece of content state.
        Nblocks	entityMapr   depth)r!   r   r   r   create_elementrender_blockappend_childstacklengthtaileltrender)
r'   r+   r,   wrapper_statedocument
entity_map	min_depthblockr.   r5   r(   r(   r)   r6   2   s$   
zHTML.renderr;   r9   r7   c                 C   sD  d|v o|d }d|v o|d }t | j|d }|s|rt }t| j|}|r-t| jnd }	| |D ]T\}
}|D ]}|	| |	rH|		| q:|rY|
 rYt| j|
||j}n|
}|	rf|	|||j}n|}||||j}|d urt|| ||kr|
 rt|| q4n|rt| j|d ||j}n|d }|||S )NinlineStyleRangesentityRangestext)r	   r   r   r/   r   r   r   r   build_command_groupsapplyhas_no_entityr   r,   render_stylesrender_entitiesr1   element_for)r'   r;   r9   r7   
has_styleshas_entitieshas_decoratorscontententity_statestyle_stater>   commandscommanddecorated_nodestyled_nodeentity_noder(   r(   r)   r0   Q   s\   


"zHTML.render_blockc                 C   s   |d }|  |}t|td}tt|td}g }d}|D ]/\}}	|t|d k rA||d  d }
||||
 t|	f n	|dt|	f |d7 }q|S )zv
        Creates block modification commands, grouped by start index,
        with the text to apply them on.
        r>   indexr       )build_commandsr   r   listlenappend)r'   r;   r>   rK   groupedlistedslicedistart_indexcomms
stop_indexr(   r(   r)   r?      s   

zHTML.build_command_groupsc                 C   sP   t |}t |}|| }|jtdd t ddg| t dt|d g S )z
        Build all of the manipulation commands for a given block.
        - One pair to set the text.
        - Multiple pairs for styles.
        - Multiple pairs for entities.
        rP   )key
start_textr   	stop_textr>   )r   from_style_rangesfrom_entity_rangessortr   rU   )r'   r;   style_commandsentity_commandsstyles_and_entitiesr(   r(   r)   rS      s   


zHTML.build_commands)N)__name__
__module____qualname____doc__	__slots__r   r   r*   r   strr6   r   r   r   r   r0   r   r   r   r?   rS   r(   r(   r(   r)   r      s     
"<r   N)!	itertoolsr   operatorr   typingr   r   r   draftjs_exporter.commandr   %draftjs_exporter.composite_decoratorsr   r	   draftjs_exporter.defaultsr
   r   draftjs_exporter.domr   draftjs_exporter.entity_stater   draftjs_exporter.optionsr   draftjs_exporter.style_stater   draftjs_exporter.typesr   r   r   r   r   draftjs_exporter.wrapper_stater   r   r(   r(   r(   r)   <module>   s    