
    KiW$                     t    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	      Zy)
z)reStructuredText documentation formatter.    N)TYPE_CHECKINGAnyOptional)extract_text)make_rst_section_header)ConsoleConsoleOptions)	HelpEntry	HelpPanelc                       e Zd ZdZ	 	 ddedefdZddZdefdZ	d	e
d
   de
d   ddddfdZded   d	e
d
   ddfdZded   d	e
d
   ddfdZd	e
d
   de
d   deddfdZd	e
d
   de
d   deddfdZy)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).
    heading_levelinclude_hiddenc                 R    || _         || _        t        j                         | _        y N)r   r   ioStringIO_output)selfr   r   s      h/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/cyclopts/help/formatters/rst.py__init__zRstFormatter.__init__   s!    
 +,{{}    returnNc                 6    t        j                         | _        y)z!Reset the internal output buffer.N)r   r   r   r   s    r   resetzRstFormatter.reset#   s    {{}r   c                 6    | j                   j                         S )zGet the accumulated RST output.

        Returns
        -------
        str
            The RST documentation string.
        )r   getvaluer   s    r   
get_outputzRstFormatter.get_output'   s     ||$$&&r   consoler   optionsr	   panelr   c                 P   |j                   sy|j                  rYt        |j                  |      }dj                  t	        || j
                              }| j                  j                  | d       |j                  r6t        |j                  |      }|r| j                  j                  | d       |j                  dk(  r| j                  |j                   |       n+|j                  dk(  r| j                  |j                   |       | j                  j                  d       y)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_texts          r   __call__zRstFormatter.__call__1   s    " }} ;;%ekk7;JYY6z4CUCUVWFLL&/ $U%6%6@I""i[#56 <<9$&&u}}g>\\[(((@4 r   r(   r
   c                 L   |D ]  }|j                   x}s|d   |dd }}|r| ddj                  |       d}n|}| j                  j                  d| d       t	        |j
                  d	      xrN t	        |j
                  j                  d
      xr, d|j
                  j                  j                  j                  v }t        |j
                  ||      }	|	sdj                  d |	j                  d      D              }
| j                  j                  d|
 d       ! y)zFormat command entries as RST.

        Parameters
        ----------
        entries : list[HelpEntry]
            Command entries to format.
        console : Optional[Console]
            Console for text extraction.
        r      Nz (, )````
primary_renderable	__class__RestructuredTextpreserve_markup c              3   ^   K   | ]%  }|j                         s|j                          ' y wr   strip.0lines     r   	<genexpr>z5RstFormatter._format_command_panel.<locals>.<genexpr>y   s"     (c$VZV`V`Vb(c   --r$       r%   )all_optionsr*   r   r+   hasattrr,   r:   r;   __name__r   split)r   r(   r    entrynamesprimary_namealiasesname_displaypreserve_rst_markupdescr2   s              r   r.   z"RstFormatter._format_command_panelY   s&     	?E)))u)(-a%)g&2^2dii6H5I#KL#/L ""R~T#:; E--/CD f 1 1 D DkRf*e.?.?.R.R.\.\.e.ee $
 $E$5$5wPcd !$(c$**TBR(c cILL&&i['=>/	?r   c                 L   |D ]  }|j                   x}s|j                  xr# |j                  du xr t        d |D               }|rJ|D cg c]  }|j	                  d      r| }}|r|d   j                         n|d   j                         }ndj                  |      }| j                  j                  d| d       g }	t        |j                  d      xrN t        |j                  j                  d	      xr, d
|j                  j                  j                  j                  v }
t        |j                  ||
      }|r|	j                  |       g }|r|j                  r|j                  d       n|j                  r|s|j                  d       |j                   r6dj                  d |j                   D              }|j                  d|        |j                  -t        |j                  |d      }|j                  d| d       |j"                  r6dj                  d |j"                  D              }|j                  d|        |	rdj                  d |	d   j%                  d      D              }| j                  j                  d|        |r.| j                  j                  ddj                  |       d       | j                  j                  d       |s| j                  j                  ddj                  |       d        yc c}w )zFormat parameter entries as RST.

        Parameters
        ----------
        entries : list[HelpEntry]
            Parameter entries to format.
        console : Optional[Console]
            Console for text extraction.
        Nc              3   >   K   | ]  }|j                  d         yw)-N)
startswith)rD   ns     r   rF   z7RstFormatter._format_parameter_panel.<locals>.<genexpr>   s     TvklUVUaUabeUfTvs   rV   r   r6   r8   r9   r:   r;   r<   r=   z**Required**c              3   (   K   | ]
  }d | d   ywr8   N )rD   cs     r   rF   z7RstFormatter._format_parameter_panel.<locals>.<genexpr>   s     +N1b2J+N   z	Choices: FzDefault: ``c              3   (   K   | ]
  }d | d   ywrZ   r[   )rD   es     r   rF   z7RstFormatter._format_parameter_panel.<locals>.<genexpr>   s     'Jq"QCr
'Jr]   zEnvironment variable: r?   c              3   ^   K   | ]%  }|j                         s|j                          ' y wr   rA   rC   s     r   rF   z7RstFormatter._format_parameter_panel.<locals>.<genexpr>   s"     (l$_c_i_i_k(lrG   r$   rH   z []r%   )rI   requireddefaultanyrW   upperr*   r   r+   rJ   r,   r:   r;   rK   r   appendchoicesenv_varrL   )r   r(   r    rM   rN   is_positionalrX   positional_namesname_str
desc_partsrR   rS   metadatachoices_strdefault_strenv_strr2   s                    r   r/   z$RstFormatter._format_parameter_panel|   s     =	IE)))u) % v5==D3H vQTTvpuTvQvMv 38'RaS@Q'R$'R>N/288:TYZ[T\TbTbTdH  $yy/H ""Rz#67  

 E--/CD f 1 1 D DkRf*e.?.?.R.R.\.\.e.ee $
 $E$5$5wPcd%%d+  U^^OON3^^MOON3=="&))+N+N"NKOOi}$=>==,".u}}gW\"]KOOk+b$AB=="ii'JEMM'JJGOO&<WI$FG  !$(l*Q-BUBUVZB[(l lILL&&i['9:**R		(0C/DA+FGLL&&v.LL&&dii.A-B$'GH{=	I (Ss   L!$L!usagec                 P   |rt        ||      }|r| j                  j                  d       |j                         j	                  d      s | j                  j                  d| d       n| j                  j                  d| d       | j                  j                  d       yyy)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::

zUsage:z    Usage: r$   rH   N)r   r   r+   rB   rW   )r   r    r!   rq   
usage_texts        r   render_usagezRstFormatter.render_usage   s    " %eW5J""8,!'')44X>LL&&ZL'CDLL&&j\'<=""4(  r   r,   c                 d    |r.t        ||      }|r| j                  j                  | d       yyy)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,   r2   s        r   render_descriptionzRstFormatter.render_description   s9    " $[':I""i[#56  r   )   F)r   N)rK   
__module____qualname____doc__intboolr   r   strr   r   r3   listr.   r/   r   rt   rv   r[   r   r   r   r      s0    $%% %%'C '&!)$&! *+&! 	&!
 
&!P!?T+-> !?R[I\ !?ae !?FGItK/@ GI8T]K^ GIcg GIR))$) *+) 	)
 
):7)$7 *+7 	7
 
7r   r   )rz   r   typingr   r   r   cyclopts._markupr   cyclopts.docs.rstr   rich.consoler   r	   cyclopts.helpr
   r   r   r[   r   r   <module>r      s,    / 	 / / ) 542g7 g7r   