U
    Sh?                  
   @   s  d 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mZ ddlZddlmZmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZm 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-m.Z. dOe/dddZ0dPee1 dddZ2e/edee1df f dddZ3G dd dZ4e4 Z5edddZ6dQddZ7eeeee1ef e1f dd d!Z8dReee1ee1ee1 ee1ef f d#d$d%Z9e"d"fe1d&d'd(Z:e"d"fej;e1d)d*d+Z<G d,d- d-eZG d.d/ d/eZ=G d0d1 d1e Z>e1e1e
ee1  d2d3d4Z?dSe
ee1  dd5d6d7Z@dTe
ee1  d8d9d:ZAdUe
ee1  d8d;d<ZBe1e1d=d>d?ZCdVe
ee1  d8d@dAZDdWe
ee1  d8dBdCZEdXe
ee1  d8dDdEZFdFdG ZGdHdI ZHejIdJdKdLZJejIdJdMdNZKdS )Yz"MyST Markdown parser for docutils.    )Field)AnyCallableDictIterableListLiteralOptionalSequenceSetTupleUnionget_args
get_originN)frontendnodes)default_descriptionpublish_cmdlinepublish_string)filter_settings_spec)Parser)HTMLTranslatorWriter)MdParserConfigTopmatterReadErrormerge_file_levelread_topmatter)DocutilsRenderer)ResolveAnchorIds)create_md_parser)MystWarningscreate_warning)returnc                 C   s   t |S zValidate an integer setting.)intsettingvalueoption_parserconfig_parserconfig_section r+   P/root/rtd-docs/venv/lib/python3.8/site-packages/myst_parser/parsers/docutils_.py_validate_int'   s    r-   c                 C   s   t | ||||}t|S r#   )r   validate_comma_separated_listsetr%   r+   r+   r,   _validate_comma_separated_set.   s        r0   .)lengthr"   c                    s   d fdd	}|S )z2Create a validator for a tuple of length `length`.Nc                    sF   t | ||||}t| kr>td  d|  dt| dt|S )Nz
Expecting z
 items in z, got .)r   r.   len
ValueErrortuple)r&   r'   r(   r)   r*   Zstring_listr1   r+   r,   	_validate;   s        z)_create_validate_tuple.<locals>._validate)NNr+   )r1   r7   r+   r6   r,   _create_validate_tuple8   s       r8   c                   @   s    e Zd ZdZdd Zdd ZdS )Unsetz$A sentinel class for unset settings.c                 C   s   dS )NZUNSETr+   selfr+   r+   r,   __repr__M   s    zUnset.__repr__c                 C   s   dS )NFr+   r:   r+   r+   r,   __bool__P   s    zUnset.__bool__N)__name__
__module____qualname____doc__r<   r=   r+   r+   r+   r,   r9   J   s   r9   )fieldc                 C   s   ddd}|S )z3Create a deserializer/validator for a json setting.Nc              
   S   sR   zt |}W n, tk
r: } ztd|W 5 d}~X Y nX t|tsNtd|S )zyCheck/normalize a key-value pair setting.

        Items delimited by `,`, and key-value pairs delimited by `=`.
        Invalid YAML stringNzExpecting a YAML dictionary)yaml	safe_load	Exceptionr4   
isinstancedictr&   r'   r(   r)   r*   outputerrr+   r+   r,   _validate_yaml\   s    
z-_create_validate_yaml.<locals>._validate_yaml)NNr+   )rB   rL   r+   r+   r,   _create_validate_yamlY   s       
rM   c              
   C   sp   zt |}W n, tk
r: } ztd|W 5 d}~X Y nX t|trZdd |dD }t|tsltd|S )zValidate a url_schemes setting.

    This is a tricky one, because it can be either a comma-separated list or a YAML dictionary.
    rC   Nc                 S   s   i | ]
}|d qS Nr+   ).0kr+   r+   r,   
<dictcomp>z   s      z)_validate_url_schemes.<locals>.<dictcomp>,z2Expecting a comma-delimited str or YAML dictionary)rD   rE   rF   r4   rG   strsplitrH   rI   r+   r+   r,   _validate_url_schemesn   s    

rU   )atdefaultr"   c                 C   s  | j dkrdtdd|fS | jtkr:dtdt|fS | jtkrXdtj	dt|fS | jtksl| j dkrdd	id
| dfS t
| jtkrtdd t| jD rt| j}dddd |D  dd|dt|fS | jtt tt fkr
dtjdd|fS | jtt kr.dtdd|fS | jtttf krZdtddd|fS | jtttdf krdtdt|fS | jttt tdf krdtjd|rd|ndfS t
| jtkrdt| d|rt|ndfS td| j  ddS )zaConvert a field into a Docutils optparse options dict.

    :returns: (option_dict, default)
    Zurl_schemesz<comma-delimited>|<yaml-dict>)metavar	validatorrR   z<int>z	<boolean>Zheading_slug_funcrX   z<str>z(default: 'z')c                 s   s   | ]}t |tV  qd S rN   )rG   rS   rO   ar+   r+   r,   	<genexpr>   s    z+_attr_to_optparse_option.<locals>.<genexpr><|c                 s   s   | ]}t |V  qd S rN   )reprrZ   r+   r+   r,   r\      s     >choice)rX   typechoicesz<comma-delimited>z	<str,str>   Nz
<null|int>z<null|comma-delimited> z<yaml-dict>zConfiguration option z% not set up for use in docutils.conf.)namerU   joinrb   r$   r-   rS   boolr   Zvalidate_booleanr   r   allr   r_   r   r
   r.   r   r0   r   r8   r   rH   rM   AssertionError)rV   rW   argsr+   r+   r,   _attr_to_optparse_option   s    


 

rl   myst_)	attributerW   prefixr"   c           	      C   sx   | | j  }d|dd }|td}t| |\}}|| | jrT| jddnd}|rl|d| d7 }||g|fS )	zConvert an ``MdParserConfig`` attribute into a Docutils setting tuple.

    :returns: A tuple of ``(help string, option flags, optparse kwargs)``.
    z--_-)destrW   helpre   z (default: ))rf   replaceDOCUTILS_UNSETrl   updatemetadataget)	rn   rW   ro   rf   flagoptionsZ
at_optionsZdefault_strZhelp_strr+   r+   r,   attr_to_optparse_option   s    

r|   )ro   c                    s"   |   t  fdd|  D S )z@Return a list of Docutils setting for the docutils MyST section.c                 3   s4   | ],}d |j dg krt|t |jV  qdS )docutilsomitN)rx   ry   r|   getattrrf   )rO   rV   defaultsro   r+   r,   r\      s   z,create_myst_settings_spec.<locals>.<genexpr>)r5   
get_fields)
config_clsro   r+   r   r,   create_myst_settings_spec   s    r   )settingsro   c                 C   s\   i }|  D ]D}d|jdg kr$q| |j }t| |t}|tk	r|||j< q|f |S )z8Create a configuration instance from the given settings.r}   r~   )r   rx   ry   rf   r   rv   )r   r   ro   valuesrn   r&   valr+   r+   r,   create_myst_config   s    r   c                       sh   e Zd ZU dZdZeedf ed< dde fe	j
Z
dZdZdZ fd	d
ZeejddddZ  ZS )r   z4Docutils parser for Markedly Structured Text (MyST).)mdmarkdownZmyst.	supportedzMyST optionsNzmyst parser)parsersc                    s   t   tg S rN   )superget_transformsr   r:   	__class__r+   r,   r      s    zParser.get_transforms)inputstringdocumentr"   c              
      s  ddl m} t|_t|_t|_t|_	| 
|  t jdrt|dD ]J\}}t| jjkrJ jd|d  d jj d} |  d	S qJzt j}W nD tk
r } z& jd
| } | t }W 5 d	}~X Y nX d|jkrt dtj zt|}	W n tk
r&   Y n X |	rF fdd}
t||	|
}t|t} |j d< |!| t" jdds #t$j%D ] } j&d}
|j'(||
 q| )  d	S )zParse source text.

        :param inputstring: The source string to parse
        :param document: The root docutils node to add AST elements to
        r   )r   line_length_limit
zLine    z  exceeds the line-length-limit: r2   Nz#Global myst configuration invalid: Zattrs_imagezMThe `attrs_image` extension is deprecated, please use `attrs_inline` instead.c                    s   t  || d dS )Nr   )lineZ	append_to)r!   )Zwtypemsgr   r+   r,   <lambda>4  s       zParser.parse.<locals>.<lambda>r   Zraw_enabledTzRaw content disabled.)*Zdocutils.writers._html_baser   visit_rubric_htmlZvisit_rubricdepart_rubric_htmlZdepart_rubricvisit_container_htmlZvisit_containerdepart_container_htmlZdepart_containerZsetup_parsehasattrr   	enumeraterT   r3   r   reportererrorappendr   rF   r   Zenable_extensionsr!   r    Z
DEPRECATEDr   r   r   r   r   r{   renderr   traverser   rawwarningparentru   Zfinish_parse)r;   r   r   r   ir   r   configexcZ	topmatterr   parsernoder+   r   r,   parse  sR    




zParser.parse)r>   r?   r@   rA   r   r   rS   __annotations__r   	RstParsersettings_specr*   Zconfig_section_dependenciesZtranslate_section_namer   r   r   r   __classcell__r+   r+   r   r,   r      s   
r   c                   @   s   e Zd Zdd ZdS )SimpleTranslatorc                 O   s   dS )Nre   r+   )r;   rk   kwargsr+   r+   r,   stylesheet_callJ  s    z SimpleTranslator.stylesheet_callN)r>   r?   r@   r   r+   r+   r+   r,   r   I  s   r   c                   @   s(   e Zd ZeejdZdd Zdd ZdS )SimpleWritertemplatec                 C   s   |   }djf |S )Nz{body}
)Zinterpolation_dictformat)r;   subsr+   r+   r,   apply_templateT  s    zSimpleWriter.apply_templatec                 C   s   i | _ t| _d S rN   )partsr   Ztranslator_classr:   r+   r+   r,   __init__X  s    zSimpleWriter.__init__N)r>   r?   r@   r   r   r   r   r   r+   r+   r+   r,   r   N  s   r   writer_nameZwriter_descriptionargvc                 C   s"   t t | d| dt |d dS )z7Run the command line interface for a particular writer.z
Generates z from standalone MyST sources.
)r   r   descriptionr   N)r   r   r   r   r+   r+   r,   _run_cli]  s    r   )r   r"   c                 C   s   t dd|  dS )z/Cmdline entrypoint for converting MyST to HTML.htmlz(X)HTML documentsNr   r   r+   r+   r,   cli_htmli  s    r   r   c                 C   s   t dd|  dS )z0Cmdline entrypoint for converting MyST to HTML5.html5zHTML5 documentsNr   r   r+   r+   r,   	cli_html5n  s    r   c                 C   s(   t t t dt dddd| d dS )zCmdline entrypoint for converting MyST to simple HTML5 demonstrations.

    This is a special case of the HTML5 writer,
    that only outputs the body of the document.
    z3Generates body HTML5 from standalone MyST sources.
Fr   )doctitle_xformsectsubtitle_xforminitial_header_level)r   writerr   settings_overridesr   N)r   r   r   r   r   r+   r+   r,   cli_html5_demos  s    r   )r   r"   c                 K   s,   ddddd}| | t| t t |dS )zConvert a MyST string to HTML5.Fr   unicode)r   r   r   Zoutput_encoding)r   r   r   )rw   r   r   r   )r   r   Z	overridesr+   r+   r,   to_html5_demo  s    
r   c                 C   s   t dd|  dS )z0Cmdline entrypoint for converting MyST to LaTeX.latexzLaTeX documentsNr   r   r+   r+   r,   	cli_latex  s    r   c                 C   s   t dd|  dS )z.Cmdline entrypoint for converting MyST to XML.xmlzDocutils-native XMLNr   r   r+   r+   r,   cli_xml  s    r   c                 C   s   t dd|  dS )z5Cmdline entrypoint for converting MyST to pseudo-XML.Z	pseudoxmlz
pseudo-XMLNr   r   r+   r+   r,   cli_pseudoxml  s    r   c                 C   sL   d|kr.| j | j|d|d  ddd n| j | j|dddd dS )a  Override the default HTML visit method for rubric nodes.

    docutils structures a document, based on the headings, into nested sections::

        # h1
        ## h2
        ### h3

        <section>
            <title>
                h1
            <section>
                <title>
                    h2
                <section>
                    <title>
                        h3

    This means that it is not possible to have "standard" headings nested inside
    other components, such as blockquotes, because it would break the structure::

        # h1
        > ## h2
        ### h3

        <section>
            <title>
                h1
            <blockquote>
                <section>
                    <title>
                        h2
            <section>
                <title>
                    h3

    we work around this shortcoming, in `DocutilsRenderer.render_heading`,
    by identifying if a heading is inside another component
    and instead outputting it as a "non-structural" rubric node, and capture the level::

        <section>
            <title>
                h1
            <blockquote>
                <rubric level=2>
                    h2
            <section>
                <title>
                    h3

    However, docutils natively just outputs rubrics as <p> tags,
    and does not "honor" the heading level.
    So here we override the visit/depart methods to output the correct <h> element
    levelhre   Zrubric)CLASSpN)bodyr   starttagr;   r   r+   r+   r,   r     s    7&r   c                 C   s2   d|kr"| j d|d  d n| j d dS )ziOverride the default HTML visit method for rubric nodes.

    See explanation in `visit_rubric_html`
    r   z</hz>
z</p>
Nr   r   r   r+   r+   r,   r     s    r   )r   c                 C   sP   d}i }| ddrd}d|kr,|d |d< | j| j|dfd|i| dS )	zOverride the default HTML visit method for container nodes.

    to remove the "container" class for divs
    this avoids CSS clashes with the bootstrap theme
    zdocutils containerZis_divFr}   styledivr   N)ry   r   r   r   )r;   r   classesattrsr+   r+   r,   r     s    r   c                 C   s   | j d dS )zpOverride the default HTML depart method for container nodes.

    See explanation in `visit_container_html`
    z</div>
Nr   r   r+   r+   r,   r      s    r   )NN)NN)NN)rm   )N)N)N)N)N)N)LrA   dataclassesr   typingr   r   r   r   r   r   r	   r
   r   r   r   r   r   rD   r}   r   r   Zdocutils.corer   r   r   Zdocutils.frontendr   Zdocutils.parsers.rstr   r   Zdocutils.writers.html5_polyglotr   r   Zmyst_parser.config.mainr   r   r   r   Z!myst_parser.mdit_to_docutils.baser   Z'myst_parser.mdit_to_docutils.transformsr   Zmyst_parser.parsers.mditr   Zmyst_parser.warnings_r    r!   r$   r-   rS   r0   r8   r9   rv   rM   rU   rl   r|   r   Valuesr   r   r   r   r   r   r   r   r   r   r   r   r   Noder   r   r+   r+   r+   r,   <module>   sx   <      
    
"B   [=