a
    Cøg£  ã                   @   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ee ddœdd„Zdee e	dœdd	„Z
eeeed
œdd„Zeeee	ee f  dœdd„Zeee dœ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   Zentity_decoratorsZ	block_mapZ	style_mapZengine)Úgetr   r   Zmap_entitiesr   Z
map_blocksr
   r   Z
map_stylesr   r   r   ZuseÚSTRING)Úselfr   © r    úT/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/draftjs_exporter/html.pyÚ__init__&   s    zHTML.__init__)Úcontent_stater   c           
      C   s¼   |du ri }|  dg ¡}t| j|ƒ}t ¡ }|  di ¡}d}|D ]F}d|v rT|d nd}|  |||¡}	||krr|}|dkr@t ||	¡ q@|dkr²|j ¡ dkr²t ||j 	¡ j
¡ t |¡S )zN
        Starts the export process on a given piece of content state.
        NÚblocksZ	entityMapr   Údepth)r   r   r   r   Úcreate_elementÚrender_blockÚappend_childÚstackÚlengthÚtailÚeltÚrender)
r   r#   r$   Úwrapper_stateÚdocumentÚ
entity_mapZ	min_depthÚblockr%   r,   r    r    r!   r-   2   s"    zHTML.render)r1   r0   r.   r   c                 C   sH  d|v o|d }d|v o|d }t | j|d ƒ}|s:|rt ¡ }t| j|ƒ}|r\t| jƒnd }	|  |¡D ]¨\}
}|D ]}| 	|¡ |	rv|	 	|¡ qv|r´| 
¡ r´t| j|
||jƒ}n|
}|	rÎ|	 |||j¡}n|}| |||j¡}|d urjt ||¡ ||krj| 
¡ rjt ||¡ qjn&|r4t| j|d ||jƒ}n|d }| ||¡S )NZinlineStyleRangesZentityRangesÚtext)r	   r   r   r&   r   r   r   r   Úbuild_command_groupsÚapplyZhas_no_entityr   r$   Zrender_stylesZrender_entitiesr(   Zelement_for)r   r1   r0   r.   Z
has_stylesZhas_entitiesZhas_decoratorsÚcontentZentity_stateZstyle_stater2   ÚcommandsÚcommandZdecorated_nodeZstyled_nodeZentity_noder    r    r!   r'   Q   sV    
ÿ

üÿÿüzHTML.render_block)r1   r   c                 C   s¢   |d }|   |¡}t|tdƒƒ}tt|tdƒƒƒ}g }d}|D ]^\}}	|t|ƒd k r‚||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.
        r2   Úindexr   é   Ú )Úbuild_commandsr   r   ÚlistÚlenÚappend)r   r1   r2   r6   ZgroupedZlistedZslicedÚiÚstart_indexZcommsÚ
stop_indexr    r    r!   r3      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.
        r8   )ÚkeyÚ
start_textr   Z	stop_textr2   )r   Zfrom_style_rangesZfrom_entity_rangesÚsortr   r=   )r   r1   Zstyle_commandsZentity_commandsZstyles_and_entitiesr    r    r!   r;   ¤   s    


ÿþÿzHTML.build_commands)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r"   r   Ústrr-   r   r   r   r   r'   r   r   r   r3   r;   r    r    r    r!   r      s    þ< r   N)!Ú	itertoolsr   Úoperatorr   Útypingr   r   r   Zdraftjs_exporter.commandr   Z%draftjs_exporter.composite_decoratorsr   r	   Zdraftjs_exporter.defaultsr
   r   Zdraftjs_exporter.domr   Zdraftjs_exporter.entity_stater   Zdraftjs_exporter.optionsr   Zdraftjs_exporter.style_stater   Zdraftjs_exporter.typesr   r   r   r   r   Zdraftjs_exporter.wrapper_stater   r   r    r    r    r!   Ú<module>   s   