U
    Sh*                  	   @  s  d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
 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mZ eG dd dZeG dd dZddddddddddddddZeG dd dZd%ddddddddd Zddd!d"d#d$ZdS )&a  Fenced code blocks are parsed as directives,
if the block starts with ``{directive_name}``,
followed by arguments on the same line.

Directive options are read from a YAML block,
if the first content line starts with ``---``, e.g.

::

    ```{directive_name} arguments
    ---
    option1: name
    option2: |
        Longer text block
    ---
    content...
    ```

Or the option block will be parsed if the first content line starts with ``:``,
as a YAML block consisting of every line that starts with a ``:``, e.g.

::

    ```{directive_name} arguments
    :option1: name
    :option2: other

    content...
    ```

If the first line of a directive's content is blank, this will be stripped
from the content.
This is to allow for separation between the option block and content.

    )annotationsN)	dataclass)dedent)AnyCallable)	Directive)flag)TestDirective)MarkupError)MystWarnings   )TokenizeErroroptions_to_itemsc                   @  s0   e Zd ZU ded< dZded< ejZded< dS )ParseWarningsstrmsgN
int | Nonelinenor   type)__name__
__module____qualname____annotations__r   r   ZDIRECTIVE_PARSINGr    r   r   Q/root/rtd-docs/venv/lib/python3.8/site-packages/myst_parser/parsers/directives.pyr   7   s   
r   c                   @  s6   e Zd ZU ded< ded< ded< ded< ded	< d
S )DirectiveParsingResult	list[str]	argumentsdictoptionsbodyintZbody_offsetlist[ParseWarnings]warningsNr   r   r   r   r   r   r   r   r   >   s   
r   T)linevalidate_optionsadditional_optionsztype[Directive]r   r   boolzdict[str, str] | None)directive_class
first_linecontentr%   r&   r'   returnc                C  s   | j rLt|| || |d}|j}|j}|j}	|j }
t| t|
 }ng }d}i }	| }
d}| js| j	s|
 r|rt|
r|td |
d| d}g }n
t| |}|
r|
d 
 s|
dd }
|d7 }|
r| js|td t||	|
||S )a  Parse (and validate) the full directive text.

    :param first_line: The text on the same line as the directive name.
        May be an argument or body text, dependent on the directive
    :param content: All text after the first line. Can include options.
    :param validate_options: Whether to validate the values of options
        This is actually only here to be used by myst-nb cells,
        which converts options directly to JSON metadata, using the full YAML spec.
    :param additional_options: Additional options to add to the directive,
        above those parsed from the content (content options take priority).

    :raises MarkupError: if there is a fatal parsing/validation error
    )r%   as_yamlr'   Fr   zbSplitting content across first line and body, when an options block is present, is not recommendedr   NzHas content, but none permitted)option_spec_parse_directive_optionsr#   has_optionsr   r+   
splitlineslenrequired_argumentsoptional_argumentsstripanyappendr   insertparse_directive_argumentsZhas_contentr   )r)   r*   r+   r%   r&   r'   resultZparse_warningshas_options_blockr   
body_linesZcontent_offsetr   r   r   r   parse_directive_textN   sT    


    r=   c                   @  s.   e Zd ZU ded< ded< ded< ded< d	S )
_DirectiveOptionsr   r+   zdict[str, Any]r   r"   r#   r(   r0   Nr$   r   r   r   r   r>      s   
r>   )r+   r)   r-   r%   r'   r,   c                 C  sL  d}|  dr|dkrdn|d }d|  dd } td| tj}|rr| d|  }| | d d } n| }d} t|}nd| 	  dr|  }g }|r|d 	  dsq|
|d	 dd  qd|}d|} |dk	}	|rg }
zt|pdpi }W n: tjjtjjfk
rN   i }|

td	|tj Y nX t|tsti }|

td
|tj t| ||
|	S g }i }|dk	rzt|\}}t|}W nJ tk
r } z*t| |td|j |tjg|	 W Y S d}~X Y nX |jr|
td|tj t|tr.t| |g |	S |r<||}|j}g }i }|  D ]\}}z|| }W n& t!k
r   |
| Y qRY nX |sd}|t"krd}z||}W nN t#t$fk
r  } z*|
td|d| d| |tj W 5 d}~X Y n
X |||< qR|r>|
tdt%| dt%| d|tj t| |||	S )zlParse (and validate) the directive option section.

    :returns: (content, options, validation_errors)
    Nz---r   
z^-{3,} :r   z!Invalid options format (bad YAML)z#Invalid options format (not a dict)zInvalid options format: zPDirective options has # comments, which may not be supported in future versions.zInvalid option value for z: zUnknown option keys: z (allowed: ))&
startswithjoinr1   research	MULTILINEstartendr   lstripr7   popyamlZ	safe_loadparserZParserErrorscannerZScannerErrorr   r   ZDIRECTIVE_OPTION
isinstancer   r>   r   r   problemZhas_commentsZDIRECTIVE_OPTION_COMMENTS
issubclassr	   r.   itemsKeyErrorr   
ValueError	TypeErrorsorted)r+   r)   r-   r%   r'   Zoptions_blockmatchZcontent_linesZ
yaml_linesr;   Zyaml_errorsZyaml_optionsZvalidation_errorsr   _optionsstateerrZoptions_specZunknown_optionsnew_optionsnamevalueZ	convertorZconverted_valueerrorr   r   r   r/      s    








	r/   r   )directive_clsarg_textr,   c                 C  s   | j }| j}| }t||k r:t| dt| dnJt||| kr| jrf|d|| d }ntd||  dt| d|S )z4Parse (and validate) the directive argument section.z argument(s) required, z	 suppliedNr   zmaximum z argument(s) allowed, )r3   r4   splitr2   r
   Zfinal_argument_whitespace)r_   r`   requiredoptionalr   r   r   r   r9   4  s    r9   )N)__doc__
__future__r   rE   dataclassesr   textwrapr   typingr   r   rL   Zdocutils.parsers.rstr   Zdocutils.parsers.rst.directivesr   Z$docutils.parsers.rst.directives.miscr	   Zdocutils.parsers.rst.statesr
   Zmyst_parser.warnings_r   r   r   r   r   r   r=   r>   r/   r9   r   r   r   r   <module>   s4   $Q  