U
    ShP6                     @  s  d Z ddlmZ ddlZ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 ddlmZ ddlm Z  e!e"Z#G dd deZ$G dd deZ%G dd deZ&G dd de&Z'G dd deZ(G dd deZ)dd Z*G dd  d eZ+G d!d" d"eZ,G d#d$ d$eZ-G d%d& d&eZ.dd'l/m0Z0 dd(l1m2Z2m3Z3m4Z4m5Z5 dd)l6m7Z7 G d*d+ d+e7Z8dS ),z*Code to use internally, for documentation.    )annotationsN)SequenceUnionget_args
get_originnodes)	Publisher)
directives)other)SphinxPostTransform)logging)SphinxDirective)to_html5_demo   )MdParserConfig)Parser)MystWarningsc                   @  s   e Zd ZdZdZdd ZdS )StripUnsupportedLatexz*Remove unsupported nodes from the doctree.i  c                 C  s   | j jjdkrd S ddlm} | j D ]R}|jdkrZ|d drZ|j	
||dd |jd	kr(|j	
||dd
 q(d S )Nlatexr   r   imageuriz.svg zRemoved SVG imagemermaidzRemoved Mermaid diagram)ZappZbuilderformatdocutilsr   documentfindalltagnameendswithparentreplaceinline)selfr   node r%   D/root/rtd-docs/venv/lib/python3.8/site-packages/myst_parser/_docs.pyrun   s    
zStripUnsupportedLatex.runN)__name__
__module____qualname____doc__default_priorityr'   r%   r%   r%   r&   r      s   r   c                   @  s    e Zd ZdZdZdZdd ZdS )NumberSectionszNumber sections (html only)i  )htmlc           
      C  s   d}d}g | j fg}|r| \}}t||kr|d }ddd ||d d  D d d }|dtjd	|d
d d|d< t||k rd}|jD ],}	t|	tj	r|d7 }|
||g |	f qqd S )N      r   .c                 s  s   | ]}t |V  qd S )N)str).0ir%   r%   r&   	<genexpr>:   s     z%NumberSections.run.<locals>.<genexpr>r   z&nbsp;&nbsp;r   r.   )r   auto)r   poplenjoininsertr   rawchildren
isinstancesectionappend)
r#   Zmin_heading_levelZmax_heading_levelstackpathr$   titletextr4   childr%   r%   r&   r'   1   s*    
zNumberSections.runN)r(   r)   r*   r+   r,   formatsr'   r%   r%   r%   r&   r-   +   s   r-   c                   @  s4   e Zd ZdZedd Zedd Zedd ZdS )	_ConfigBasez5Directive to automate rendering of the configuration.c                   C  s   dddddddgS )Nz```````{list-table}z:header-rows: 1z:widths: 15 10 20r   z* - Namez  - Typez  - Descriptionr%   r%   r%   r%   r&   table_headerL   s    z_ConfigBase.table_headerc                 C  s   d |  }|S )N )r9   
splitlines)valuedefaultr%   r%   r&   field_defaultX   s    z_ConfigBase.field_defaultc                 C  s|   t | jtkrt| jn| jg}ddd |D }d| }|dd}|dd}dD ]}|d	| d
|}q^|S )N | c                 s  s&   | ]}t |td krdn|V  qd S )NNone)r2   type)r3   Zftyper%   r%   r&   r5   c   s   z)_ConfigBase.field_type.<locals>.<genexpr>rH   ztyping.r   ztyping_extensions.)r2   intfloatboolz<class 'z'>)r   rO   r   r   r9   rI   r!   )fieldZftypesctypeZtnamer%   r%   r&   
field_type]   s    
z_ConfigBase.field_typeN)r(   r)   r*   r+   staticmethodrG   rL   rU   r%   r%   r%   r&   rF   I   s   

rF   c                   @  s(   e Zd Zejejdd dZdd ZdS )MystConfigDirectivec                 C  s   t | ddgS )Ngloballocal)r
   choice)xr%   r%   r&   <lambda>s       zMystConfigDirective.<lambda>)sphinx
extensionsscopec              
   C  sx  t  }|  }d}| D ](\}}}|jdr4qd| jkrRd|jdg krRqd| jkrl|jdszqn|jdrzq| jddkr|jd	rq| jdd
krd| }d|jdd }|jdr|jd d| }| |}|jdp| 	|}	|
d| dd|	 dd| d| dg |d7 }q|sPg S |d t }
| j|d|
 |
jS )Run the directive.r   
deprecatedr^   Zomitr_   	extensionr`   rY   Zglobal_onlyrX   Zmyst_rH   helpr   z: Zdoc_typez* - ``z  - `z  - z (default: `z`)r   z```````)r   rG   Z	as_triplemetadatagetoptionsr9   rI   rL   rU   extendr?   r   Elementstatenested_parser<   )r#   configrC   countnamerJ   rS   descriptionrK   rT   r$   r%   r%   r&   r'   v   sJ    






zMystConfigDirective.runN)r(   r)   r*   r
   flagoption_specr'   r%   r%   r%   r&   rW   o   s
   rW   c                   @  s(   e Zd ZdZdZdZdZdZdd ZdS )DocutilsCliHelpDirectivez)Directive to print the docutils CLI help.Fr   c                 C  s~   t  }tt d}t|H z|jdgdd W n, tk
r` } z|jrPt	W 5 d}~X Y nX W 5 Q R X t
d| gS )ra   )parserz--helpz;myst-docutils-<writer> [options] [<source> [<destination>]])usageNr   )ioStringIOr	   DocutilsParser
contextlibredirect_stdoutZprocess_command_line
SystemExitcodeAssertionErrorr   Zliteral_blockgetvalue)r#   streamZpubexcr%   r%   r&   r'      s    
&zDocutilsCliHelpDirective.runN	r(   r)   r*   r+   has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer'   r%   r%   r%   r&   rs      s   rs   c                   @  s    e Zd ZdZdZdZdd ZdS )DirectiveDoczLoad and document a directive.r   Tc           	      C  s   | j d }t|| jjj| jj\}}|dkrJtjd| d| j	d g S d
| j}d| d| d	|j d
|j d|jr~dnd d}|jr|d7 }|j D ]$\}}|d| dt|| d7 }qt }| j| d| |jS )ra   r   Nz
Directive z not found.)linerH   z:Name: `z`
:Description: z
:Arguments: z required, z optional
:Content: yesnoz
:Options:
z  name | type
  -----|------
z  rM   
)	argumentsr
   	directiverk   memolanguager   LOGGERwarninglinenor9   contentr   r   r   rr   itemsconvert_optr   rj   rl   rI   r<   )	r#   ro   klass_r   rC   keyfuncr$   r%   r%   r&   r'      s8    
  zDirectiveDoc.runN)r(   r)   r*   r+   r   r   r'   r%   r%   r%   r&   r      s   r   c                 C  s   |t jkrdS |t jkrdS |t jkr*dS |t jkr8dS |t jkrFdS |t jkrTdS |tkr`dS |t jkrndS |t j	kr|dS |t j
krd	S |t jkrd
S |t jkrdS |t jkrdS |tjkrdS dS )z'Convert an option function to a string.rq   rC   zspace-delimited listURIrA   integerzinteger (positive)zinteger (non-negative)z1space/comma-delimited list of integers (positive)
percentagezlength or unitlesszlength, percentage or unitlessr   )r
   rq   	unchangedZunchanged_requiredZclass_optionr   rA   rP   Zpositive_intZnonnegative_intZpositive_int_listr   Zlength_or_unitlessZ length_or_percentage_or_unitlessr   Zint_or_nothing)ro   r   r%   r%   r&   r      s:    












r   c                   @  s(   e Zd ZdZdZdZdZdZdd ZdS )MystWarningsDirectivez&Directive to print all known warnings.Fr   c                   sf   ddl m} |tj tj    fddtD }dd |D }t }| j	
|d| |jS )ra   r   )ModuleAnalyzerc                   s"   g | ]}|j  j|jf fqS r%   )rJ   Z	attr_docsro   )r3   eZanalyzerZqnamer%   r&   
<listcomp>  s    z-MystWarningsDirective.run.<locals>.<listcomp>c                 S  s&   g | ]\}}d | dd | qS )z- `myst.z`: rH   )r9   )r3   ro   docr%   r%   r&   r     s     )Zsphinx.pycoder   Z
for_moduler   r)   r*   Zanalyzer   rj   rk   rl   r<   )r#   r   Zwarning_namesrC   r$   r%   r   r&   r'     s    zMystWarningsDirective.runNr   r%   r%   r%   r&   r     s   r   c                   @  s*   e Zd ZdZdZejejdZdd ZdS )MystExampleDirectivez>Directive to create an example, showing the source and output.T)
alt-output	highlightc                 C  s   d | j}| jd|}| jdd}d}||kr>|d7 }q,d| d| d| | d| d| d| d	| d
| d| d	| d}t }| j| | j	| |j
S )ra   r   r   r   mystz```re   z``{div} myst-example

z`{div} myst-example-source
z`
z`{div} myst-example-render

z``
)r9   r   rh   rg   r   rj   rk   rl   rI   content_offsetr<   )r#   content_strZ
output_strr   Z	backticksr   node_r%   r%   r&   r'   '  s@    

zMystExampleDirective.runN)	r(   r)   r*   r+   r   r
   r   rr   r'   r%   r%   r%   r&   r     s   r   c                   @  s    e Zd ZdZdZdZdd ZdS )MystAdmonitionDirectivez5Directive to show a set of admonitions, in a tab set.r   Tc              	   C  sp   dd | j d dD }d}|D ] }|d| d| d| d	7 }q"|d
7 }t }| j| | j| |jS )ra   c                 S  s   g | ]}|  qS r%   )strip)r3   tr%   r%   r&   r   J  s     z/MystAdmonitionDirective.run.<locals>.<listcomp>r   ,z::::{tab-set}z
:::{tab-item} z
```{z}
This is a z	
```
:::
z::::)	r   splitr   rj   rk   rl   rI   r   r<   )r#   typesr   type_r   r%   r%   r&   r'   H  s    zMystAdmonitionDirective.runN)r(   r)   r*   r+   r   r   r'   r%   r%   r%   r&   r   B  s   r   c                   @  s.   e Zd ZdZdZdZdZdejiZ	dd Z
dS )MystToHTMLDirectivez"Directive to convert MyST to HTML.Tr   r_   c                 C  s   d | j}i }d}d| jkrJ| jd d}||d< |d| jd  7 }t|f|}d| d| d	| d
}t }| j|	 | j
| |jS )ra   r   r   r_   r   Zmyst_enable_extensionsz--myst-enable-extensions=z7::::myst-example
```bash
myst-docutils-demo example.md z
```
```myst
z
```
```html
z

```
::::
)r9   r   rh   r   r   r   rj   rk   rl   rI   r   r<   )r#   r   kwargsZcli_optextr.   r   r   r%   r%   r&   r'   d  s$    
	zMystToHTMLDirective.runN)r(   r)   r*   r+   r   r   r   r
   r   rr   r'   r%   r%   r%   r&   r   Z  s    r   )token)bygroupsinheritthisusing)MarkdownLexerc                   @  s   e Zd ZdZdZdgZdgZdgZdee	j
e	jje	j
e	jfdee	j
e	jfdee	j
e	jje	jfd	ee	j
e	jje	j
eed
de	jfegde	jfdee	j
e	jje	j
fdee	j
e	jje	jje	jje	j
fegdZdS )	MystLexerz!A custom lexer for MyST Markdown.ZMySTr   z*.mystztext/x-mystz^(\()([^\n]+)(\)=)(\n)z^([\:]{3,})(\n)z^([\:]{3,})([^\n]+)(\n)z^(\:)([^\n\:]+)(\:)([^\n]+)(\n)r"   )rk   z\\.z(\{)([a-zA-Z0-9+:-]+)(\})z7(<)(http|https|mailto|project|path|inv)(\:)([^\s>]+)(>))rootr"   N)r(   r)   r*   r+   ro   aliases	filenames	mimetypesr   r   PunctuationNameLabelTextTagGenericStrongr   r   r   OperatorWordStringOthertokensr%   r%   r%   r&   r     sX      

"
r   )9r+   
__future__r   ry   rv   typingr   r   r   r   r   r   Zdocutils.corer	   Zdocutils.parsers.rstr
   Zsphinx.directivesr   Z!sphinx.transforms.post_transformsr   Zsphinx.utilr   Zsphinx.util.docutilsr   Zmyst_parser.parsers.docutils_r   Zconfig.mainr   Zparsers.docutils_r   rx   Z	warnings_r   	getLoggerr(   r   r   r-   rF   rW   rs   r   r   r   r   r   r   pygmentsr   Zpygments.lexerr   r   r   r   Zpygments.lexers.markupr   r   r%   r%   r%   r&   <module>   s>   
&;!!%)