o
    ia(                     @   sn   d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 er.ddl
mZmZ ddlmZmZ G dd	 d	ZdS )
z)reStructuredText documentation formatter.    N)TYPE_CHECKINGAnyOptional)extract_text)make_rst_section_header)ConsoleConsoleOptions)	HelpEntry	HelpPanelc                   @   s   e Zd ZdZ		d"dedefddZd#d
dZdefddZ	de
d de
d dddd	fddZded de
d dd	fddZded de
d dd	fddZde
d de
d dedd	fddZde
d de
d dedd	fd d!Zd	S )$RstFormattera  reStructuredText documentation formatter.

    Parameters
    ----------
    heading_level : int
        Starting heading level for panels (default: 2).
    include_hidden : bool
        Include hidden commands/parameters in documentation (default: False).
       Fheading_levelinclude_hiddenc                 C   s   || _ || _t | _d S N)r   r   ioStringIO_output)selfr   r    r   b/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/help/formatters/rst.py__init__   s   zRstFormatter.__init__returnNc                 C   s   t  | _dS )z!Reset the internal output buffer.N)r   r   r   r   r   r   r   reset#   s   zRstFormatter.resetc                 C   s
   | j  S )zGet the accumulated RST output.

        Returns
        -------
        str
            The RST documentation string.
        )r   getvaluer   r   r   r   
get_output'   s   
zRstFormatter.get_outputconsoler   optionsr   panelr
   c                 C   s   |j sdS |jr t|j|}dt|| j}| j| d |jr4t|j|}|r4| j| d |j	dkrA| 
|j | n|j	dkrM| |j | | jd dS )a]  Format and render a help panel as RST.

        Parameters
        ----------
        console : Optional[Console]
            Console for rendering (used for extracting plain text).
        options : Optional[ConsoleOptions]
            Console rendering options (unused for RST).
        panel : HelpPanel
            Help panel to render.
        N


command	parameter)entriestitler   joinr   r   r   writedescriptionformat_format_command_panel_format_parameter_panel)r   r   r   r   
title_textheader	desc_textr   r   r   __call__1   s   

zRstFormatter.__call__r#   r	   c           	      C   s   |D ]\}g }|j r||j  |jr||j |r^|d }| jd| d t|jdo<t|jjdo<d|jjjj	v }t
|j||d}|r^dd	d
 |dD }| jd| d qdS )zFormat command entries as RST.

        Parameters
        ----------
        entries : list[HelpEntry]
            Command entries to format.
        console : Optional[Console]
            Console for text extraction.
        r   ````
primary_renderable	__class__RestructuredTextpreserve_markup c                 s        | ]}|  r|  V  qd S r   strip.0liner   r   r   	<genexpr>|       z5RstFormatter._format_command_panel.<locals>.<genexpr>r       r    N)namesextendshortsr   r&   hasattrr'   r1   r2   __name__r   r%   split)	r   r#   r   entryr@   primary_namepreserve_rst_markupdescr-   r   r   r   r)   Y   s(   
z"RstFormatter._format_command_panelc                 C   s  |D ]k}g }|j r||j  |jr||j |rn|jo-|jdu o-tdd |D  }|rFdd |D }|r?|d  n|d  }nd|}| j	d| d	 g }t
|jd
okt
|jjdokd|jjjjv }	t|j||	d}
|
r{||
 g }|r|jr|d n
|jr|s|d |jrddd |jD }|d|  |jdurt|j|dd}|jrdt|jv rd}d}|D ]}|dr|}q|dr|s|}q| dkr|r|d| d n'| dkr|r|d| d n|d| d n	|d| d |jr,ddd |jD }|d|  |r^ddd |d dD }| j	d|  |rW| j	dd| d | j	d  q|rn| j	dd| d  qdS )!zFormat parameter entries as RST.

        Parameters
        ----------
        entries : list[HelpEntry]
            Parameter entries to format.
        console : Optional[Console]
            Console for text extraction.
        Nc                 s   s    | ]}| d V  qdS )-N
startswithr;   nr   r   r   r=      s    z7RstFormatter._format_parameter_panel.<locals>.<genexpr>c                 S   s   g | ]	}| d s|qS )rJ   rK   rM   r   r   r   
<listcomp>   s    z8RstFormatter._format_parameter_panel.<locals>.<listcomp>r   z, r/   r0   r1   r2   r3   r4   z**Required**c                 s       | ]	}d | d V  qdS r/   Nr   )r;   cr   r   r   r=          z	Choices: Fboolz--no-z--truezDefault: ``falsec                 s   rP   rQ   r   )r;   er   r   r   r=      rS   zEnvironment variable: r6   c                 s   r7   r   r8   r:   r   r   r   r=      r>   r   r?   z []r    )r@   rA   rB   requireddefaultanyupperr%   r   r&   rC   r'   r1   r2   rD   r   appendchoicestypestrrL   lowerenv_varrE   )r   r#   r   rF   r@   is_positionalpositional_namesname_str
desc_partsrH   rI   metadatachoices_strdefault_strpositive_flagnegative_flagnameenv_strr-   r   r   r   r*      sz   

$






z$RstFormatter._format_parameter_panelusagec                 C   sZ   |r)t ||}|r+| jd |dD ]}| jd| d q| jd dS dS dS )a-  Render the usage line as RST.

        Parameters
        ----------
        console : Optional[Console]
            Console for text extraction.
        options : Optional[ConsoleOptions]
            Console rendering options (unused).
        usage : Any
            The usage line content.
        z::

r   r?   N)r   r   r&   rE   )r   r   r   rn   
usage_textr<   r   r   r   render_usage   s   
zRstFormatter.render_usager'   c                 C   s0   |rt ||}|r| j| d dS dS dS )a5  Render the description as RST.

        Parameters
        ----------
        console : Optional[Console]
            Console for text extraction.
        options : Optional[ConsoleOptions]
            Console rendering options (unused).
        description : Any
            The description content.
        r    N)r   r   r&   )r   r   r   r'   r-   r   r   r   render_description   s   
zRstFormatter.render_description)r   F)r   N)rD   
__module____qualname____doc__intrT   r   r   r`   r   r   r.   listr)   r*   r   rp   rq   r   r   r   r   r      sP    

	

(&c
r   )rt   r   typingr   r   r   cyclopts._markupr    cyclopts.help.formatters._sharedr   rich.consoler   r   cyclopts.helpr	   r
   r   r   r   r   r   <module>   s    