
    i@                        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 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fd#Zy)&z9RST documentation generation functions for cyclopts apps.    )TYPE_CHECKING)extract_text)
adjust_filters_for_subcommand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    w/home/jay/workspace/.worktrees/task-2116-dev1/scripts/.codegraph-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_mapr3   Q   s:     K
}},S.A 	'LD& &K	'r   entriesr0   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   )	r4   r0   r5   r6   r7   filtered_entriesentrycmd_namer2   s	            r   _filter_command_entriesr?   g   s    6  3;;{{1~H __X.F~$,1C1K$++E2 *(KARTfhno$++E23 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;   )r@   s    r   _generate_tocrB      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_preamblec                    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}|rnt!        |t"              r|}nt%        |dd      }|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t/        |       | j                  | g      5  | j1                  g |      }ddd       t3        ||	      \  } }!g }"t5        | d      }# ||dz   |      }$D ]  \  }%}&|s|%r|%j6                  s|
r|s|&j8                  dk(  r|&j:                  D 'cg c]'  }'|'j<                  rt?        | |'j<                        r&|') }(}'|(sltA        |(|#|"| |!      })|)s~|&jB                  r0|j	                  d|&jB                   d       |j	                  d       |)D ]q  }*|*j<                  r|*j<                  d   nd}+t%        |*jD                  d      },|j	                  d|+ d       |,r|j	                  d|,        |j	                  d       s 2|&j8                  dk(  sC|$jG                          |&jI                  d      }- |$dd|-       |$jK                         j+                         }.|.s|&jB                  r0|j	                  d|&jB                   d       |j	                  d       |j	                  |.       |j	                  d        |r&| j,                  rt3        ||	      \  } }!g }"tM        | |      D ]  \  }/}0tO        |/|"| |!|0      s|j	                  d       |r||/gz   n||/g}1|r|}2n|
r|s|dz
  }2n|}2tQ        |/| |!      \  }3}4| xr" |xr |duxr t        |      dk(  xr |/|d   k(  }5| xr1 |xr- |duxr' t        |      dk(  xr |d   jS                  |/dz         }6|0j                  | |0g      5  tU        |0|||2||1d||3|4|6||5xs |6       }7ddd       |j	                  7        ddl+}8dj                  |      }9|8jY                  d!d"|9      }9|9S # 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.

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

)-cyclopts.help.formatters.rstrO   r	   extendr;   r   joinreplacer'   minr   r+   	app_stackresolver   r   
isinstancestrr   splitr   stripr/   rB   _assemble_help_panelsr   r3   showformatr4   r9   r
   r?   r   descriptionresetcopy
get_outputr   r   r   
startswithgenerate_rst_docsresub):r,   rC   r-   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rO   r@   app_namefull_command
base_titler   anchor_partsanchor_nameeffective_heading_levelheader_linesrU   usage
usage_textpartslinerl   preserve	desc_texthelp_panels_with_groupsnormalized_commands_filternormalized_exclude_commandsr5   r0   	formattergrouppanelecommand_entriesr<   r=   primary_namedesc
panel_copyoutputr1   r2   subcommand_chainnext_heading_levelsub_commands_filtersub_exclude_commandsis_single_targetis_intermediate_pathsubdocsrr   docs:                                                             r   rq   rq      s   D :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 !&,,T15E5zA~(+(?'@%(%L
%(XXm%<
T"R &,,T2 0DLL4v/0R  )#{; #&AAH$[$QIY__./R  Mcmme 
u	 M"%";";B"LM ?X)?; ;  K %S>K =1+<^\I 0 5!u%

  <<9$*/--pQL`adfgfmfmLnqpO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5!n S]]B[-C
?"$? ,S.A 7	"LD&)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  LL!o7	"t 
))E
C
&&FC
(CJYM M8 qp s$    X-#'X:X:X?-X7?Y	)T)TFr!   r&   NTFNNFFF)__doc__typingr   cyclopts._markupr   cyclopts.docs.baser   r   r   r   r	   r
   r   r   r   r   cyclopts.corer   rf   listr   intr+   booldictr3   setr?   rB   rq   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 	  &*"(,)-"y	yy y 	y
 y 9t#y y y #Y%y 3i$&y y y y 	yr   