U
    Sh,K                     @   s  d Z ddlZddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZ dejedd	d
dZG dd deddZdejedd	ddZ dejeddddZ!dejeddddZ"dejedd	ddZ#e$e$dddZ%dejedd	ddZ&e' G dd dZ(e(ee$ef eee$gdf e(dd d!Z)G d"d# d#e*Z+ee$e
e$ f eee$ef  dd$d%Z,dS )&z&The configuration for the myst parser.    N)import_module)AnyCallableDictIterableIteratorListOptionalSequenceSetTuple	TypedDictUnion)MystWarnings   )any_deep_iterabledeep_mappingin_instance_ofoptionalvalidate_fieldvalidate_fieldsMdParserConfig)instfieldvaluereturnc                 C   s~   t |ts td|j d| t|ddddddd	d
ddddddddg}|rhtd|j d| t| |jt| dS )z5Check that the extensions are a list of known strings'z' not iterable: ZamsmathZattrs_imageZattrs_inlineZattrs_blockZcolon_fenceZdeflist
dollarmathZ	fieldlistZhtml_admonitionZ
html_imagelinkifyZreplacementsZsmartquotesZstrikethroughZsubstitutiontasklistz' items not recognised: N)
isinstancer   	TypeErrornameset
difference
ValueErrorsetattr)r   r   r   diff r*   J/root/rtd-docs/venv/lib/python3.8/site-packages/myst_parser/config/main.pycheck_extensions"   s0    
r,   c                   @   s.   e Zd ZU dZeed< eed< ee ed< dS )UrlSchemeTypez(Type of the external schemes dictionary.urltitleclassesN)__name__
__module____qualname____doc__str__annotations__r   r*   r*   r*   r+   r-   ?   s   
r-   F)totalc              	   C   s   t |ttfrDtdd |D s6td|j d|dd |D }t |tsdtd|j d|i }| D ]z\}}t |tstd|j d||d	kr|||< qpt |trd
|i||< qpt |trtdd |D s td|j d| d|d
|kr:t |d
 ts:td|j d| d|d
 d|krtt |d tsttd|j d| d|d d|krt |d tstdd |d D std|j d| d|d |||< qptd|j d| d|qpt	| |j| d	S )z8Check that the external schemes are of the right format.c                 s   s   | ]}t |tV  qd S Nr"   r5   .0vr*   r*   r+   	<genexpr>J   s     z$check_url_schemes.<locals>.<genexpr>r   z' is not a list of strings: c                 S   s   i | ]
}|d qS r8   r*   r:   r*   r*   r+   
<dictcomp>L   s      z%check_url_schemes.<locals>.<dictcomp>' is not a dictionary: ' key is not a string: Nr.   c                 s   s   | ]}t |tV  qd S r8   r9   r;   kr*   r*   r+   r=   Z   s     [z]' keys are not strings: z][url]' is not a string: r/   z][title]' is not a string: r0   c                 s   s   | ]}t |tV  qd S r8   r9   )r;   cr*   r*   r+   r=   g   s     z"][classes]' is not a list of str: z"]' value is not a string or dict: )
r"   listtupleallr#   r$   dictitemsr5   r(   )r   r   r   Znew_dictkeyvalr*   r*   r+   check_url_schemesG   sN    




rL   )_r   r   r   c                 C   sj   t |ttfrt|dkr0td|j d| |D ]0}t |trNt|dkr4td|j d| q4dS )zLCheck that the sub_delimiters are a tuple of length 2 of strings of length 1   r   z' is not a tuple of length 2: r   z(' does not contain strings of length 1: N)r"   rF   rE   lenr#   r$   r5   )rM   r   r   delimr*   r*   r+   check_sub_delimitersu   s    rQ   c              	   C   s   t |ts td|j d|| D ]\}}t |tsPtd|j d|t |ttfrjt|dkrtd|j d| d|t |d tstd|j d| d|d  |d	 d
ks(t |d	 ts(td|j d| d|d	  q(d
S )zDCheck that the inventories are a dict of {str: (str, Optional[str])}r   r?   r@   rN   rC   z]' value is not a 2-item list: r   z][0]' is not a string: r   Nz][1]' is not a null/string: )	r"   rH   r#   r$   rI   r5   rF   rE   rO   )rM   r   r   rJ   rK   r*   r*   r+   check_inventories   s    

 rR   c              
   C   s   |dkrdS t |trz&|dd\}}t|}t||}W n: tk
rv } ztd|j d||W 5 d}~X Y nX t| |j| t	|std|j d|dS )z/Check that the heading_slug_func is a callable.N.r   r   z#' could not be loaded from string: z' is not callable: )
r"   r5   rsplitr   getattrImportErrorr#   r$   r(   callable)r   r   r   module_pathZfunction_namemodexcr*   r*   r+   check_heading_slug_func   s     
r[   )textr   c                 C   s   | ddd S )z5Dummy slug function, this is imported during testing.Nr*   )r\   r*   r*   r+   _test_slug_func   s    r^   c                 C   s6   t ttttttf| || t| |jt| dS )z9Check that the extensions are a sequence of known stringsN)r   r   r5   rE   rF   r%   r(   r$   )r   r   r   r*   r*   r+   check_fence_as_directive   s     r_   c                   @   s6  e Zd ZU dZedddZejdee	dddZ
e	ed	< ejdee	d
ddZe	ed< ejeedddZee ed< ejeeeeeeefdddZee ed< ejdee	dddZe	ed< ejdee	dddZe	ed< ejdd eddd dddZeeee f ed< ejdeeeeeeefddgddZeee  ed < ejeed!ddZ ee ed"< ejeeeeeeefd#ddZ!e"e ed$< ejdee	d%ddZ#e	ed&< ejd'ee$d'd(d)d*d+d,d-d.gd/ddZ%e&ed0< ejde'd1d2d3d4dZ(ee)egef  ed5< eje*e+eeeeee*d2d6d7d d8dZ,eeef ed9< ejd2ee	d:ddZ-e	ed;< ejd<ee&d=ddZ.e&ed>< eje*e+eee/ee*d2d?d@dAd dBdZ0eee1f ed@< ejdCde2dDd@dgdEdFZ3e4eef edG< ejd2ee	dHdIdJdZ5e	edK< ejd2ee	dLdMdJdZ6e	edN< ejd2ee	dOdMdJdZ7e	edP< ejd2ee	dQdMdJdZ8e	edR< ejdee	dSdMdJdZ9e	edT< ejd2ee	dUdMd2dgdVdZ:e	edW< ejdXeedYdMd2dgdVdZ;eedZ< ejdee	d[d\dJdZ<e	ed]< ejeeeeeeefd^d_gd2d`dZ=e"e eda< ejd2ee	dbd_gddZ>e	edc< eje*de?ddd_gd2d`deZ@eee4eee f f edf< dgdh ZAe1d didjdkZBeCe4ejDdlf ddmdnZEe*fe*ddodpZFee4ee1ejDf  ddqdrZGdS )sr   zConfiguration options for the Markdown Parser.

    Note in the sphinx configuration these option names are prepended with ``myst_``
    )r   c                 C   s`   g }|   D ]8\}}}|jsq|jdt|}|| d|  q| jj dd| dS )z-Return a string representation of the config.	repr_func=(, ))	as_triplereprmetadatagetappend	__class__r1   join)self
attributesr$   rK   fZval_strr*   r*   r+   __repr__   s    zMdParserConfig.__repr__FzUse strict CommonMark parser)	validatorhelp)defaultrg   commonmark_onlyz+Use strict Github Flavoured Markdown parsergfm_onlyzEnable syntax extensions)default_factoryrg   enable_extensionsz"Disable Commonmark syntax elementsdisable_syntaxz$Parse all links as simple hyperlinksall_links_externalz$Open all external links in a new tablinks_external_new_tabc                   C   s   d d d d dS )N)httphttpsmailtoftpr*   r*   r*   r*   r+   <lambda>   s
    zMdParserConfig.<lambda>z0URI schemes that are converted to external linksc                 C   s   t t| S r8   )rf   rF   r<   r*   r*   r+   r~          z(list[str] | dict[str, None | str | dict])rp   rq   r`   doc_typeurl_schemesNz4Sphinx domain names to search in for link referencesZdocutils)rp   rq   omitref_domainszInterpret a code fence as a directive, for certain language names. This can be useful for fences like dot and mermaid, and interoperability with other Markdown renderers.fence_as_directivez4Add line numbers to code blocks with these languagesnumber_code_blocksz:Convert a `title` field in the front-matter to a H1 headertitle_to_headerr   r   rN                  z*Heading level depth to assign HTML anchorsheading_anchorszfFunction for creating heading anchors, or a python import path e.g. `my_package.my_module.my_function`Tz!None | Callable[[str], str] | str)rp   rq   global_onlyr   heading_slug_funczHTML meta tagsc                 C   s   dd dd | D  dS )N{rc   c                 s   s   | ]}| d V  qdS z: ...Nr*   rA   r*   r*   r+   r=   G  s     *MdParserConfig.<lambda>.<locals>.<genexpr>}rk   r   r*   r*   r+   r~   G  r   )rp   merge_topmatterrq   r`   	html_metaz'Place a transition before any footnotesfootnote_transition   zFor reading speed calculationswords_per_minutezSubstitutions mappingsubstitutionsc                 C   s   dd dd | D  dS )Nr   rc   c                 s   s   | ]}| d V  qdS r   r*   rA   r*   r*   r+   r=   d  s     r   r   r   r   r*   r*   r+   r~   d  r   )rp   r   rq   	extensionr`   )r   r   zSubstitution delimiters)rp   rq   r   r   )rr   rf   rg   sub_delimitersz&Recognise URLs without schema prefixesr    )rp   rq   r   linkify_fuzzy_linkszParse `$$...$$ (label)`r   dmath_allow_labelsz'Allow initial/final spaces in `$ ... $`dmath_allow_spacez%Allow initial/final digits `1$ ...$2`dmath_allow_digitszParse inline `$$ ... $$`dmath_double_inlinez@Update sphinx.ext.mathjax configuration to ignore `$` delimiters)rp   rq   r   r   r   update_mathjaxz0tex2jax_process|mathjax_process|math|output_areaz#MathJax classes to add to math HTMLmathjax_classeszEnable checkboxesr!   enable_checkboxesz4A list of warning types to suppress warning messagesZsphinx)rp   rq   r   r   suppress_warningsz*Syntax highlight code blocks with pygmentshighlight_code_blocksz@Mapping of key to (url, inv file), for intra-project referencing)ru   rf   rg   inventoriesc                 C   s   t |  d S r8   )r   )rl   r*   r*   r+   __post_init__  s    zMdParserConfig.__post_init__)kwargsr   c                 K   s   t j| f|S )zReturn a new object replacing specified fields with new values.

        Note: initiating the copy will also validate the new fields.
        )dcreplace)rl   r   r*   r*   r+   copy  s    zMdParserConfig.copy.c                 C   s
   t | S )z*Return all attribute fields in this class.)r   fields)clsr*   r*   r+   
get_fields  s    zMdParserConfig.get_fieldsc                 C   s   t j| |dS )z+Return a dictionary of field name -> value.)dict_factory)r   asdict)rl   r   r*   r*   r+   as_dict  s    zMdParserConfig.as_dictc                 c   sB   dd t | jD }t |  D ]\}}|||| fV  q$dS )z&Yield triples of (name, value, field).c                 S   s   i | ]}|j |qS r*   )r$   )r;   rn   r*   r*   r+   r>     s      z,MdParserConfig.as_triple.<locals>.<dictcomp>N)r   r   rj   r   rI   )rl   r   r$   r   r*   r*   r+   re     s    zMdParserConfig.as_triple)Hr1   r2   r3   r4   r5   ro   r   r   r   boolrs   r6   rt   r%   r,   rv   r   rE   r   rF   rw   r   rx   ry   rL   r   r   r	   r-   r   r   r_   r   r   r
   r   r   r   intr[   r   r   rH   r   r   r   r   r   r   r   rQ   r   r   r   r   r   r   r   r   r   r   r   r   rR   r   r   r   classmethodFieldr   r   re   r*   r*   r*   r+   r      s  

  
		
	")config	topmatterwarningr   c                 C   s>  i }| di }t|ts2|tjdt|  n|}d|krV|tjd |d |d< d|krv|tjd |d |d< |  }dd |  D }| D ]\}}||kr|tjd	|  q|| \}	}
zt	||
| W n< t
k
r } z|tjt| W Y qW 5 d
}~X Y nX |
j dr,|	|}t||| q|S )zMerge the file-level topmatter with the global config.

    :param config: Global config.
    :param topmatter: Topmatter from the file.
    :param warning: Function to call with a warning (type, message).
    :returns: A new config object
    mystz'myst' key not a dict: r   zGtop-level 'html_meta' key is deprecated, place under 'myst' key insteadr   zKtop-level 'substitutions' key is deprecated, place under 'myst' key insteadc                 S   s   i | ]\}}}|||fqS r*   r*   )r;   r$   r   r   r*   r*   r+   r>     s      z$merge_file_level.<locals>.<dictcomp>zUnknown field: Nr   )rh   r"   rH   r   ZMD_TOPMATTERtyper   re   rI   r   	Exceptionr5   rg   r(   )r   r   r   Zupdatesr   newr   r$   r   	old_valuer   rZ   r*   r*   r+   merge_file_level  sB    
r   c                   @   s   e Zd ZdZdS )TopmatterReadErrorzTopmatter parsing error.N)r1   r2   r3   r4   r*   r*   r*   r+   r   3  s   r   c              
   C   s   ddl }t| tr2| ds dS dd |  D } zt| dsHW dS W n tk
r`   Y dS X g }| D ]$}|dr| q|| d  qjz|	d
|}W n8 |jj|jjfk
r } ztd	|W 5 d}~X Y nX t|tstd
t| |S )a  Read the (optional) YAML topmatter from a source string.

    This is identified by the first line starting with `---`,
    then read up to a terminating line of `---`, or `...`.

    :param source: The source string to read from
    :return: The topmatter
    r   N---c                 s   s   | ]
}|V  qd S r8   r*   )r;   liner*   r*   r+   r=   E  s     z!read_topmatter.<locals>.<genexpr>)r   z...
 zMalformed YAMLzYAML is not a dict: )yamlr"   r5   
startswith
splitlinesnextStopIterationri   rstripZ	safe_loadrk   parserZParserErrorscannerZScannerErrorr   rH   r   )r\   r   Z
top_matterr   rg   errr*   r*   r+   read_topmatter7  s,    	




r   )-r4   dataclassesr   	importlibr   typingr   r   r   r   r   r   r	   r
   r   r   r   r   Zmyst_parser.warnings_r   Zdc_validatorsr   r   r   r   r   r   r   r   r   r,   r-   rL   rQ   rR   r[   r5   r^   r_   	dataclassr   r   r   r   r   r*   r*   r*   r+   <module>   s@   8(.      D
=