o
    iA                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlmZ er"ddl	m
Z
 		d-dee dB d	ee dB d
eee dB ee dB f fddZdedee dee dB dee dB ddd
efddZdedee dB dee dB d
eee dB ee dB f fddZ					d.dddededee dB d	ee dB dee dB d
eeeedf  fddZ	d/dee deeeedf  d edB d
dfd!d"Z	#		$		#				d0ddd%eded&ed'ee dB d(ed)edee dB d	ee dB d*ed
efd+d,ZdS )1z9RST documentation generation functions for cyclopts apps.    )TYPE_CHECKING)extract_text)BaseDocGenerator)make_rst_section_header)AppNcommands_filterexclude_commandsreturnc                 C   s8   d}| durdd | D }d}|rdd |D }||fS )a  Normalize command filter lists by converting underscores to dashes.

    Parameters
    ----------
    commands_filter : Optional[List[str]]
        List of commands to include.
    exclude_commands : Optional[List[str]]
        List of commands to exclude.

    Returns
    -------
    Tuple[Optional[Set[str]], Optional[Set[str]]]
        Normalized include and exclude sets for O(1) lookup.
    Nc                 S      h | ]}| d dqS _-replace.0cmd r   W/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/docs/rst.py	<setcomp>!       z-_normalize_command_filters.<locals>.<setcomp>c                 S   r
   r   r   r   r   r   r   r   %   r   r   )r   r   normalized_includenormalized_excluder   r   r   _normalize_command_filters   s   r   nameparent_pathnormalized_commands_filternormalized_exclude_commandssubappr   c           	      C   s   |r
d || g n| }|r3| |v s||v rdS tt|D ]}d |d|d  }||v r2 dS q|dur}| |v s?||v rAdS tt|D ]}d |d|d  }||v r[ dS qG|sd| |v rddS t|dr{|jr{|D ]}||d rz dS qndS dS )a9  Determine if a command should be included based on filters.

    Parameters
    ----------
    name : str
        The command name.
    parent_path : List[str]
        Path to parent commands.
    normalized_commands_filter : Optional[Set[str]]
        Set of commands to include (already normalized).
    normalized_exclude_commands : Optional[Set[str]]
        Set of commands to exclude (already normalized).
    subapp : App
        The subcommand App instance.

    Returns
    -------
    bool
        True if the command should be included, False otherwise.
    .FN   T	_commands)joinrangelenhasattrr!   
startswith)	r   r   r   r   r   	full_pathiparent_segment
filter_cmdr   r   r   _should_include_command*   s4   r+   c           	      C   s   d}|dur6g }|D ]%}| | d r'|t| d d }||dd q
|| kr/d} nq
|g kr6g }d}|reg }|D ]&}| | d r[|t| d d }||dd q>||dd q>||fS )a  Adjust filter lists for subcommand context.

    Parameters
    ----------
    name : str
        The current command name.
    normalized_commands_filter : Optional[Set[str]]
        Set of commands to include (already normalized).
    normalized_exclude_commands : Optional[Set[str]]
        Set of commands to exclude (already normalized).

    Returns
    -------
    Tuple[Optional[List[str]], Optional[List[str]]]
        Adjusted commands_filter and exclude_commands lists (denormalized).
    Nr   r    r   r   )r&   r$   appendr   )	r   r   r   sub_commands_filterr*   
sub_filtersub_exclude_commandsexclude_cmdsub_excluder   r   r   _adjust_filters_for_subcommande   s,   r2   F appinclude_hiddenprefixc              	   C   s   g }| j s|S |du rg }t||\}}t| |D ]>\}	}
t|	||||
s'q|r/| |	 n|	}|dd }||||
f ||	g }t|
|| d|||d}|	| q|S )zwRecursively collect all commands for table of contents.

    Returns a list of (display_name, anchor, app) tuples.
    N r   )r5   r6   r   r   r   )
r!   r   r   iterate_commandsr+   r   lowerr,   _collect_commands_for_tocextend)r4   r5   r6   r   r   r   commandsr   r   r   r   display_nameanchornested_pathnestedr   r   r   r:      s6   

r:   linesr<   app_namec                 C   s4   |sdS |  d |  d |  d |  d dS )z5Generate TOC entries with proper indentation for RST.Nz.. contents:: Commandsz
   :local:z   :depth: 2r3   )r,   )rA   r<   rB   r   r   r   _generate_toc_entries   s   


rC   Tr    	recursiveheading_levelcommand_chaingenerate_tocflatten_commandsno_root_titlec
           .      C   sv  ddl m}
 g }|du rg }t| |\}}}|r-t|dkr(d|dd n|d }n|}dg}|r:|| n|| d|ddd	d	 }|d
| d |d |	rc|sc|}n|rj|rj|}n|rt|t| d n|}|	rz|rt
||}|| |d | jjddd}t| |}|r|dv }t|d|d}|r||  |d |	r|r|r|d |d |	r|rt| }d}|rt|tr|}nt|ddd}|rtj||dd}d|v r|dd }d| }|r|d |d |dD ]}|d|  q|d | g |}|
|d |d}t||}|d }|d }|d } |rx|	rF|rx|d |d |D ]%\}!}"|  d|"_|dd|" |  }#|#rv||# |d qR|s~| r|	r|r|d |d |D ]%\}!}"|  d|"_|dd|" |  }#|#r||# |d q| D ]"\}!}"|  |dd|" |  }#|#r||# |d q|r6| jr6t||\}$}%g }&t| |D ]D\}'}(t|'|&|$|%|(s q|d |r||'g n||'g})|r|}*n|}*t|'|$|%\}+},t|(|||*|)d||+|,dd 
}-||- qd|S )!a  Generate reStructuredText documentation for a CLI application.

    Parameters
    ----------
    app : App
        The cyclopts App instance to document.
    recursive : bool
        If True, generate documentation for all subcommands recursively.
        Default is True.
    include_hidden : bool
        If True, include hidden commands/parameters in documentation.
        Default is False.
    heading_level : int
        Starting heading level for the main application title.
        Default is 1 (uses '=' markers).
    command_chain : list[str]
        Internal parameter to track command hierarchy.
        Default is None.
    generate_toc : bool
        If True, generate a table of contents for multi-command apps.
        Default is True.
    flatten_commands : bool
        If True, generate all commands at the same heading level instead of nested.
        Default is False.
    commands_filter : list[str], optional
        If specified, only include commands in this list.
        Supports nested command paths like "db.migrate".
        Default is None (include all commands).
    exclude_commands : list[str], optional
        If specified, exclude commands in this list.
        Supports nested command paths like "db.migrate".
        Default is None (no exclusions).
    no_root_title : bool
        If True, skip generating the root application title.
        Useful when embedding in existing documentation with its own title.
        Default is False.

    Returns
    -------
    str
        The generated RST documentation.
    r   )RstFormatterNr    r7   cycloptsr   /z.. _:r3   help_formatrestructuredtext)fallback)rP   rst)preserve_markupz
**Usage:**F)r6   zUsage:zUsage: z::
z    )rE   r5   	argumentsoptionsgroupedz**Arguments:**z**Options:**)	rD   r5   rE   rF   rG   rH   r   r   rI   ) cyclopts.help.formatters.rstrJ   r   get_app_infor$   r"   r;   r,   r   r9   r   	app_stackresolveextract_descriptionr   stripextract_usage
isinstancestrformat_usage_linesplit_assemble_help_panelscategorize_panelsresettitle
get_outputr!   r   r8   r+   r2   generate_rst_docs).r4   rD   r5   rE   rF   rG   rH   r   r   rI   rJ   rA   rB   full_command
base_titlerf   anchor_partsanchor_nameeffective_heading_levelheader_linesrO   descriptionpreserve	desc_textusage
usage_textlinehelp_panels_with_groups	formattercategorizedargument_panelsoption_panelsgrouped_panelsr   paneloutputr   r   r   r   r   subcommand_chainnext_heading_levelr-   r/   subdocsr   r   r   rh      s   6(



























rh   )NN)Fr3   NNN)N)	TFr    NTFNNF)__doc__typingr   cyclopts._markupr   cyclopts.docs.baser    cyclopts.help.formatters._sharedr   cyclopts.corer   listr`   tuplesetr   boolr+   r2   r:   rC   intrh   r   r   r   r   <module>   s    





;


4



4



	
