
    lKjlC                        d Z ddlm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 erddlmZ dedee   fdZded	edee   fd
Zd%dddedeedf   fdZdedeedf   dee   dee   dz  dee   dz  defdZdee   ddfdZ	 	 	 	 	 	 	 	 	 	 	 	 	 d&dddededededee   dz  dededee   dz  dee   dz  d ed!ed"ed#edz  defd$Zy)'z9RST documentation generation functions for cyclopts apps.    )TYPE_CHECKING)extract_text)adjust_filters_for_subcommandapply_usage_nameextract_descriptionextract_usagegenerate_anchorget_app_infois_all_builtin_flagsiterate_commandsnormalize_command_filtersshould_include_commandshould_show_usage)Apptitlereturnc                     ddd|  gS )zCreate an RST code block containing the title.

    Parameters
    ----------
    title : str
        Title text to display in code block.

    Returns
    -------
    list[str]
        RST formatted code block lines.
    z.. code-block:: text z    r   s    b/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/cyclopts/docs/rst.pymake_rst_code_block_titler      s     	

eW     levelc                 t    ddddddd}|dk  rd}n|d	kD  rd	}||   }|t        |       z  }|dk(  r|| |gS | |gS )
zCreate an RST section header.

    Parameters
    ----------
    title : str
        Section title.
    level : int
        Heading level (1-6).

    Returns
    -------
    list[str]
        RST formatted section header lines.
    =-^"'~)                  r"   r'   )len)r   r   markersmarker	underlines        r   make_rst_section_headerr,   ,   sm      G qy	U^FU#Iz5),,y!!r   appr   include_hiddenc                 T    i }| j                   rt        | |      D ]
  \  }}|||<    |S )a/  Build mapping of command names to App objects.

    Parameters
    ----------
    app : App
        The app to extract commands from.
    include_hidden : bool
        Whether to include hidden commands.

    Returns
    -------
    dict[str, App]
        Mapping of command names to App instances.
    )	_commandsr   )r-   r.   command_mapnamesubapps        r   _build_command_mapr4   R   s6     K
}},S.ALD& &K Br   entriesr1   parent_pathnormalized_filterNnormalized_excludec                     g }| D ]j  }|j                   s|j                   d   }|j                  |      }||5|8|j                  |       Jt        |||||      sZ|j                  |       l |S )a  Filter command entries based on inclusion/exclusion rules.

    Parameters
    ----------
    entries : list
        Command entries to filter.
    command_map : dict[str, App]
        Mapping of command names to App objects.
    parent_path : list[str]
        Parent command path.
    normalized_filter : set[str] | None
        Normalized filter set.
    normalized_exclude : set[str] | None
        Normalized exclude set.

    Returns
    -------
    list
        Filtered command entries.
    r   )namesgetappendr   )	r5   r1   r6   r7   r8   filtered_entriesentrycmd_namer3   s	            r   _filter_command_entriesr@   h   s    6 ;;{{1~H __X.F~$,1C1K$++E2 *(KARTfhno$++E2  r   linesc                     | j                  d       | j                  d       | j                  d       | j                  d       y)zGenerate table of contents using RST contents directive.

    The `.. contents::` directive automatically generates a TOC from
    section headings, which is the idiomatic approach for RST/Sphinx.
    z.. contents:: Table of Contentsz
   :local:z   :depth: 6r   N)r<   )rA   s    r   _generate_tocrC      s5     
LL23	LL	LL 	LLr   	recursiveheading_levelmax_heading_levelcommand_chaingenerate_tocflatten_commandscommands_filterexclude_commandsno_root_titlecode_block_titleskip_preamble
usage_namec                    ddl m} g }|g }t        | |      \  }}}|r|s|d   }n|r|}n|}dg}|r|j                  |       n|j	                  |       t        dj                  |            j                  dd      }|j	                  d	| d
       |j	                  d       |
r|s|}n|r|r|}n|r|t        |      z   dz
  n|}t        ||      }|
r|r<|rt        |      }nt        ||      }|j                  |       |j	                  d       | j                  j                  dd      }|st        |       r|
r|rt        |       }d}|rzt!        |t"              r|}nt%        |dd      }t'        ||      }|rK|j)                  dd      }t        |      dkD  rdj                  |       d|d    }ndj                  |      }|r]|j	                  d       |j	                  d       |j)                  d      D ]  }|j	                  d|         |j	                  d       |sRt+        | |      }|rD|dv }t%        |d|      } | r0|j	                  | j-                                |j	                  d       |r|s| j.                  rt1        |       | j                  | g      5  | j3                  g |      }!ddd       t5        ||	      \  }"}#g }$t7        | d      }% ||dz   |      }&!D ]  \  }'}(|s|'r|'j8                  s|
r|s|(j:                  dk(  r|(j<                  D )cg c]'  })|)j>                  rtA        | |)j>                        r&|)) }*})|*sltC        |*|%|$|"|#      }+|+s~|(jD                  r0|j	                  d|(jD                   d       |j	                  d       |+D ]q  },|,j>                  r|,j>                  d   nd}-t%        |,jF                  d      }.|j	                  d|- d       |.r|j	                  d|.        |j	                  d       s 2|(j:                  dk(  sC|&jI                          |(jK                  d      }/ |&dd|/       |&jM                         j-                         }0|0s|(jD                  r0|j	                  d|(jD                   d       |j	                  d       |j	                  |0       |j	                  d        |r'| j.                  rt5        ||	      \  }"}#g }$tO        | |      D ]  \  }1}2tQ        |1|$|"|#|2      s|j	                  d       |r||1gz   n||1g}3|r|}4n|
r|s|dz
  }4n|}4tS        |1|"|#      \  }5}6| xr" |xr |duxr t        |      dk(  xr |1|d   k(  }7| xr1 |xr- |duxr' t        |      dk(  xr |d   jU                  |1dz         }8|2j                  | |2g      5  tW        |2|||4||3d||5|6|8||7xs |8|       }9ddd       |j	                  9        ddl,}:dj                  |      };|:j[                  d!d"|;      };|;S # 1 sw Y   }xY wc c})w # 1 sw Y   XxY w)#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).
    max_heading_level : int
        Maximum heading level to use. Headings deeper than this will be capped
        at this level. RST uses different underline characters for each level.
        Default is 6.
    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.
    skip_preamble : bool
        If True, skip the description and usage sections for the target command
        when filtering to a single command via ``commands_filter``.
        Useful when the user provides their own section introduction.
        Default is False.
    usage_name : str | None
        Optional replacement for the root app name used in ``Usage:`` lines
        only. Section headings, anchors, and TOC continue to use ``app.name[0]``.
        Default is None.

    Returns
    -------
    str
        The generated RST documentation.
    r   )RstFormatterNcyclopts /r   z.. _:r   r"   help_formatrestructuredtext)fallbackF)preserve_markupz::
z    )rX   rstT)r.   )rE   r.   commandz**z:**z``	parameterr   .)rD   r.   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   z\n{3,}z

).cyclopts.help.formatters.rstrQ   r
   extendr<   r	   joinreplacer(   minr   r,   	app_stackresolver   r   
isinstancestrr   r   splitr   stripr0   rC   _assemble_help_panelsr   r4   showformatr5   r:   r   r@   r   descriptionresetcopy
get_outputr   r   r   
startswithgenerate_rst_docsresub)<r-   rD   r.   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rQ   rA   app_namefull_command
base_titler   anchor_partsanchor_nameeffective_heading_levelheader_linesrW   usage
usage_textdisplay_chainpartslinern   preserve	desc_texthelp_panels_with_groupsnormalized_commands_filternormalized_exclude_commandsr6   r1   	formattergrouppanelecommand_entriesr=   r>   primary_namedesc
panel_copyoutputr2   r3   subcommand_chainnext_heading_levelsub_commands_filtersub_exclude_commandsis_single_targetis_intermediate_pathsubdocsrt   docs<                                                               r   rs   rs      s   N :E)5c=)I&HlJ
 -b!	  <LM*H%!#((<"89AA#sKK	LL4}A&'	LL ]"/	m"/ MZ-#m2D"Dq"H_l ""9;LM-4U;L25:QRL\"R--''@R'SK .s3
 m!#&EJeS)!&J!-eT5!QJ !1
 K !&,,T15E5zA~(+(?'@%(%L
%(XXm%<
T"R &,,T2DLL4v/ 3R  )#{; #&AAH$[$QIY__./R  Mcmme 
u	"%";";B"L 
 ?X)?; ;  K %S>K =1+<^\I 0u%

  <<9$*/--p-QL`adfgfmfmLnq-Op"  7k;UWr  $ {{r%++c23R  *16u{{1~"#E$5$5t<r,r23LL4v/R  * \\[(OO"-JdD*-))+113F ;;LL2ekk]#!67LL$V$R k 0n S]]B[-C
?"$? ,S.ALD&)k#=?Z\b LL9F}v5XW[L\%2"}%2Q%6"%2"8U02M95!5 "! /!/#4// (A-/ OA..  "! >!>#4/> (A-> $A&11$*= ! !!3-0+'#1"4&7"2!&%5$7%9"6%5"2"J6J) 1" LL!q Bv 
))E
C
&&FC
(CJ[ 
	8 qp 10s$   ,X:/'YYY:YY	)T)TFr"   r'   NTFNNFFFN)__doc__typingr   cyclopts._markupr   cyclopts.docs.baser   r   r   r   r	   r
   r   r   r   r   r   cyclopts.corer   rh   listr   intr,   booldictr4   setr@   rC   rs   r   r   r   <module>r      s   ?   )    !S T#Y (#"3 #"s #"tCy #"LE 4 4U
CS ,((c5j!( c( 3x$	(
 C4( 
(V	c 	t 	  &*"(,)-"!D	DD D 	D
 D 9t#D D D #Y%D 3i$&D D D D d
D 	Dr   