o
    i.                     @   s   d Z ddlZddlmZmZmZmZmZmZ ddl	m
Z
 er.ddlmZmZ ddlmZmZ dedefd	d
ZG dd dZdS )z!Markdown documentation formatter.    N)TYPE_CHECKINGAnyOptionalUnionget_args
get_origin)extract_text)ConsoleConsoleOptions)	HelpEntry	HelpPaneltype_objreturnc                 C   sd  | du rdS t | tr3| }|dddd}|dd}d|v r1||dd |d	 }|S |S t| }|drB|d
d }t| }|tu r~t| }t	|dkrnt
d|v rn|d t
du rf|d n|d }t|S dd |D }dd| d	S |rt| }t|dt|}|rdd |D }| dd| d	S |S t| dr| jS |dd}|S )al  Format a type object into a readable string.

    Parameters
    ----------
    type_obj : Any
        Type object to format.

    Returns
    -------
    str
        Formatted type name.

    Examples
    --------
    >>> _format_type_name(str)
    'str'
    >>> _format_type_name(Optional[int])
    'int'
    >>> _format_type_name(list[str])
    'list[str]'
    N z<class 'z'>ztyping.z	Optional[[   ]      r   c                 S      g | ]}t |qS  _format_type_name.0argr   r   g/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/help/formatters/markdown.py
<listcomp>F       z%_format_type_name.<locals>.<listcomp>zUnion[, __name__c                 S   r   r   r   r   r   r   r   r   M   r   )
isinstancestrreplaceindexrindex
startswithr   r   r   lentyper   joingetattrhasattrr!   )r   type_strinneroriginargsnon_noneformatted_argsorigin_namer   r   r   r      s@   

 
r   c                   @   s   e Zd ZdZ			d$de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 )&MarkdownFormattera  Markdown documentation formatter.

    Parameters
    ----------
    heading_level : int
        Starting heading level for panels (default: 2).
        E.g., 2 produces "## Commands", 3 produces "### Commands".
    table_style : str
        Style for parameter/command tables: "table" or "list" (default: "table").
    include_hidden : bool
        Include hidden commands/parameters in documentation (default: False).
    r   tableFheading_leveltable_styleinclude_hiddenc                 C   s    || _ || _|| _t | _d S )N)r6   r7   r8   ioStringIO_output)selfr6   r7   r8   r   r   r   __init__i   s   zMarkdownFormatter.__init__r   Nc                 C   s   t  | _dS )z!Reset the internal output buffer.N)r9   r:   r;   r<   r   r   r   resett   s   zMarkdownFormatter.resetc                 C   s
   | j  S )zGet the accumulated markdown output.

        Returns
        -------
        str
            The markdown documentation string.
        )r;   getvaluer>   r   r   r   
get_outputx   s   
zMarkdownFormatter.get_outputconsoler	   optionsr
   panelr   c                 C   s   |j sdS |jrt|j|}d| j }| j| d| d |jr3t|j|}|r3| j| d |jdkr@| |j | n|jdkrL| 	|j | | jd dS )ag  Format and render a help panel as markdown.

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

command	parameter
)
entriestitler   r6   r;   writedescriptionformat_format_command_panel_format_parameter_panel)r<   rB   rC   rD   
title_textheading	desc_textr   r   r   __call__   s   


zMarkdownFormatter.__call__rK   r   c                 C   s   |D ]A}g }|j r||j  |jr||j |rC|d }t|j|}|r3| jd| d|  n
| jd| d | jd qdS )zFormat command entries as markdown.

        Parameters
        ----------
        entries : list[HelpEntry]
            Command entries to format.
        console : Optional[Console]
            Console for text extraction.
        r   * ``: z`:rJ   N)namesextendshortsr   rN   r;   rM   )r<   rK   rB   entryrX   primary_namedescr   r   r   rP      s   z'MarkdownFormatter._format_command_panelc                 C   sp  |D ]2}g }|j r||j  |jr||j |r5|jo#|jdu }|r<dd |D }|r5|d  n|d  }n/t|dkrgtdd |D radd |D }d	d |D }	d
||	 }n
d
|}n|d }| j	
d| d t|j|}
|
r| j	
|
 d}|jr|sd}n|r|jrd}g }|jrd
|j}|d|  |jrd
|j}|d|  |jdurt|j|}|jrt|jdkrd}d}|D ]}|dr|}q|dr|s|}q| dkr|r|d|  n| dkr|r|d|  n|d|  |r| j	
d |D ]}| j	
d| d q!| j	
d qdS )zFormat parameter entries as markdown in Typer style.

        Parameters
        ----------
        entries : list[HelpEntry]
            Parameter entries to format.
        console : Optional[Console]
            Console for text extraction.
        Nc                 S   s   g | ]	}| d s|qS )-r'   r   nr   r   r   r          z=MarkdownFormatter._format_parameter_panel.<locals>.<listcomp>r   r   c                 s   s&    | ]}| d o| d V  qdS )r^   --Nr_   r`   r   r   r   	<genexpr>   s   $ z<MarkdownFormatter._format_parameter_panel.<locals>.<genexpr>c                 S   s$   g | ]}| d r| ds|qS )r^   rc   r_   r`   r   r   r   r      s   $ c                 S   s   g | ]	}| d r|qS )rc   r_   r`   r   r   r   r      rb   r    rV   rW   FTz	choices: zenv: boolz--no-rc   truez	default: falsez  **[required]**z  *[z]*rJ   )rX   rY   rZ   requireddefaultupperr(   anyr*   r;   rM   r   rN   choicesappendenv_varr)   r   r'   lower)r<   rK   rB   r[   rX   is_positionalpositional_namesname_str
short_opts	long_optsr]   is_requiredmetadatachoices_strenv_strdefault_strpositive_flagnegative_flagnameitemr   r   r   rQ      sv   




z)MarkdownFormatter._format_parameter_panelusagec                 C   s2   |rt ||}|r| jd| d dS dS dS )a2  Render the usage line as markdown.

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

Nr   r;   rM   )r<   rB   rC   r~   
usage_textr   r   r   render_usage-  s   
zMarkdownFormatter.render_usagerN   c                 C   s0   |rt ||}|r| j| d dS dS dS )a:  Render the description as markdown.

        Parameters
        ----------
        console : Optional[Console]
            Console for text extraction.
        options : Optional[ConsoleOptions]
            Console rendering options (unused).
        description : Any
            The description content.
        rG   Nr   )r<   rB   rC   rN   rT   r   r   r   render_descriptionC  s   
z$MarkdownFormatter.render_description)r   r5   F)r   N)r!   
__module____qualname____doc__intr#   re   r=   r?   rA   r   rU   listrP   rQ   r   r   r   r   r   r   r   r4   [   sV    



(d
r4   )r   r9   typingr   r   r   r   r   r   cyclopts._markupr   rich.consoler	   r
   cyclopts.helpr   r   r#   r   r4   r   r   r   r   <module>   s     M