B
     d(                 @   s  d dl Z d dlmZmZ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 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mZmZ eeZdZ dZ!e "dZ#e "dZ$ed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)List
NamedTupleOptionalTuple)nodes)
directives)
StringList)addnodes)Sphinx)SphinxDirective)	getLogger   )findall)WARNING_TYPEPassthroughTextElementcreate_componentis_componentmake_choicemargin_option
text_aligncardzcard-carouselz^\^{3,}\s*$z^\+{3,}\s*$)appreturnc             C   s   |  tt |  tt dS )zSetup the card components.N)Zadd_directiveDIRECTIVE_NAME_CARDCardDirectiveDIRECTIVE_NAME_CAROUSELCardCarouselDirective)r    r   `/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sphinx_design/cards.pysetup_cards   s    r   c               @   sR   e Zd ZU dZeeef ed< dZe	eeef  ed< dZ
e	eeef  ed< dS )CardContentzWSplit card into header (optional), body, footer (optional).

    (offset, content)
    bodyNheaderfooter)__name__
__module____qualname____doc__r   intr   __annotations__r"   r   r#   r   r   r   r   r    %   s   
r    c               @   s   e Zd ZdZdZdZdZdZeddddd	ge	e
ejejejejejed
dddgejeddddgejejejejejejejdZeej dddZeeee eejdddZeeeedddZeeeeeeej dddZ!edd  Z"d!S )"r   zA card component.Tr   r   autoz25%z50%z75%z100%urlanyrefdocnonesmmdZlg)widthmarginz
text-alignzimg-topz
img-bottomzimg-backgroundzimg-altlinkz	link-typezlink-altshadowz
class-cardzclass-headerz
class-bodyzclass-titlezclass-footerzclass-img-topzclass-img-bottom)r   c             C   s   |  | | j| jgS )N)create_card	argumentsoptions)selfr   r   r   runL   s    zCardDirective.run)instr7   r8   r   c          
   C   s@  ddg}d|kr*|d|d  d g7 }||ddg7 }|d|d	d
 g7 }d|krf|dg7 }td||dg  |dg  }|| |dpd}|}d|kr|tj|d dg|d tddg}|| ||7 }|}d|kr"tjd|d |dg|dg  d}	||	 | |j|j	}
|
j
r^|| |d||
j
d |
j
d  | |d||
jd |
jd }|rtdd d!g|d"g  }|j|d |j\}}t }|| || || |d| || |
jr|| |d#||
jd |
jd  d$|krRtjd|d$ |d%g|d&g  d}|| d|kr<t }d'g}|d(pvd}|d(r|d) |d*d+d+krtj|d|d |d,}|d(r |t|| nJ||d |jj|d* d-krdnd.|d* d/d/d0}tj|t||f|}|| ||7 }|| |S )1zRun the directive.zsd-cardzsd-sphinx-overrider2   zsd-w-%r3   zsd-mb-3z
sd-shadow-r5   r0   r4   zsd-card-hoverr   z
text-alignz
class-cardzimg-alt zimg-backgroundzsd-card-img)uriclassesaltzcard-overlayzsd-card-img-overlayzimg-topzsd-card-img-topzclass-img-top)r>   r@   r?   r"   r   r   r!   z
card-titlezsd-card-titlezsd-font-weight-boldzclass-titler#   z
img-bottomzsd-card-img-bottomzclass-img-bottomzsd-stretched-linkzlink-altzsd-hide-link-textz	link-typer+   )Zrefurir?   r,   ZstdT)r?   Z	reftargetZrefdocZ	refdomainZreftypeZrefexplicitZrefwarn)rstripgetr   set_source_infoappendr   imagesplit_contentcontentcontent_offsetr"   _create_componentr!   stateZinline_textlinenor   extendinsertr#   	referenceinlineenvZdocnamer	   Zpending_xref)clsr;   r7   r8   Zcard_classesr   Zimg_alt	containeroverlayZ	image_top
componentsr!   titleZ	textnodes_Ztitle_containerZimage_bottomZlink_containerZ_classesZ_rawtextr4   r   r   r   r6   O   s    















zCardDirective.create_card)rG   offsetr   c       
      C   s   d\}}}}|}x8t | D ],\}}|dkr8t|r8|}t|r|}qW |dk	rn|| d| f}||d 7 }|dk	r|| d | |d d f}|| |dk	r|d nd| f}	t|	||S )z/Split the content into header, body and footer.)NNNNNr   )	enumerateREGEX_HEADERmatchREGEX_FOOTERr    )
rG   rW   Zheader_indexZfooter_indexr"   r#   Zbody_offsetindexliner!   r   r   r   rF      s     
zCardDirective.split_content)r;   namer8   rW   rG   r   c             C   sR   t d| d| g|d| g  }|| |j||| | | |S )z#Create the header, body, or footer.zcard-zsd-card-zclass-)r   rB   rC   rJ   nested_parseadd_card_child_classes)rQ   r;   r^   r8   rW   rG   	componentr   r   r   rI      s    
(

zCardDirective._create_componentc             C   s:   x4t | tjD ]"}d|kr g n|d dg |d< qW dS )z$Add classes to specific child nodes.r?   zsd-card-textN)r   r   Z	paragraph)nodeparar   r   r   r`      s    z$CardDirective.add_card_child_classesN)#r$   r%   r&   r'   has_contentrequired_argumentsoptional_argumentsZfinal_argument_whitespacer   r   r   r   r>   Z	unchangedclass_optionoption_specr   r   Noder:   classmethodr   r   listdictr6   staticmethodr   r(   r    rF   strrR   rI   r`   r   r   r   r   r   0   sJ   

x
r   c               @   s:   e Zd ZdZdZdZdZdejiZ	e
ej dddZd	S )
r   zGA component, which is a container for cards in a single scrollable row.Tr   r   class)r   c          
   C   s   |    y*tdd tddD | jd  }W n2 tk
rd } z| d| W dd}~X Y nX tdd	d
d| g| j	dg  }| 
| | j| j| j| x4|jD ]*}t|dstjdt d|tdd P qW |gS )zRun the directive.c             S   s   g | ]}t |qS r   )rn   ).0ir   r   r   
<listcomp>  s    z-CardCarouselDirective.run.<locals>.<listcomp>r      r   zInvalid directive argument: Nzcard-carouselzsd-sphinx-overridezsd-cards-carouselzsd-card-cols-ro   r   z4All children of a 'card-carousel' should be 'card' [z.card])locationtypesubtype)Zassert_has_contentr   ranger7   strip
ValueErrorerrorr   r8   rB   rC   rJ   r_   rG   rH   childrenr   LOGGERwarningr   )r9   colsexcrR   itemr   r   r   r:     s*    "


zCardCarouselDirective.runN)r$   r%   r&   r'   rd   re   rf   r   rg   rh   r   r   ri   r:   r   r   r   r   r     s   
r   ))retypingr   r   r   r   Zdocutilsr   Zdocutils.parsers.rstr   Zdocutils.statemachiner   Zsphinxr	   Zsphinx.applicationr
   Zsphinx.util.docutilsr   Zsphinx.util.loggingr   _compatr   Zsharedr   r   r   r   r   r   r   r$   r|   r   r   compilerY   r[   r   r    r   r   r   r   r   r   <module>   s(   $


 S