
    i                        d Z ddlm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mZmZ erddlmZ 	 	 	 	 	 	 d.dd	d
ededee   dz  dee   dz  dee   dz  dedeeed	f      fdZdee   deeed	f      ddfdZd/dd	d
edeed	f   fdZd/dee   dededdfdZdd	dedee   ddfdZdd	dee   dee   ddfdZ dd	ded
edee   dz  dee   dz  dedee   ddfdZ!dee   dd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$	 	 	 	 	 	 	 	 	 	 	 	 d0d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)1z5Documentation generation functions for cyclopts apps.    )TYPE_CHECKING)extract_text)DEFAULT_FORMAT)adjust_filters_for_subcommandbuild_command_chainextract_descriptionextract_usageformat_usage_linegenerate_anchorget_app_infois_all_builtin_flagsiterate_commandsnormalize_command_filtersshould_include_commandshould_show_commands_listshould_show_usage)AppNappr   include_hiddenprefixcommands_filterexclude_commandsparent_pathskip_filtered_commandreturnc           
      6   g }|g }t        ||      \  }}	t        | |      D ]t  \  }
}t        |
|||	|      s|xr |j                  }|s|r| |
 n|
}|j	                  ||f       ||
gz   }| |
 d}t        ||||||d      }|j                  |       v |S )zoRecursively collect all commands for table of contents.

    Returns a list of (display_name, app) tuples.
     F)r   r   r   r   r   r   )r   r   r   	_commandsappend_collect_commands_for_tocextend)r   r   r   r   r   r   r   commandsnormalized_commands_filternormalized_exclude_commandsnamesubappskip_this_commanddisplay_namenested_pathnested_prefixnesteds                    |/home/jay/workspace/.worktrees/task-2116-dev1/scripts/.codegraph-venv/lib/python3.12/site-packages/cyclopts/docs/markdown.pyr    r       s     H>W)?; ; )n=  f%+9;VX^
  2Ff6F6F 06fXdV,DLOO\623 "TF*!(4&**) +-#"'
 	7 : O    linesr"   c           	          i }|D ]w  \  }}|j                  d      dz
  }d|z  }|j                         d   }t        |      }||v r||xx   dz  cc<   | d||    }nd||<   | j                  | d| d| d	       y y
)zGenerate TOC entries with proper indentation.

    Parameters
    ----------
    lines : list[str]
        List to append TOC entries to.
    commands : list[tuple[str, "App"]]
        List of (display_name, app) tuples.
    r      z    _r   z- [``](#)N)countsplitr   r   )	r.   r"   anchor_countsr(   _appdepthindentcmd_nameanchors	            r,   _generate_toc_entriesr=   Q   s     %'M& =d""3'!+%%%'+ .]"&!Q&!xqv!6 78F$%M&!xtH:T&;<=r-   c                 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.
    )r   r   )r   r   command_mapr%   r&   s        r,   _build_command_mapr@   m   s:     K
}},S.A 	'LD& &K	'r-   content	add_blankc                 R    |r| j                  |       |r| j                  d       yy)a  Append content to lines if present, optionally adding blank line.

    Parameters
    ----------
    lines : list[str]
        List to append to.
    content : str
        Content to append (only if non-empty).
    add_blank : bool
        Whether to add a blank line after content.
     N)r   )r.   rA   rB   s      r,   _append_if_presentrE      s'     WR r-   help_formatc                     t        | |      }|rF|dv }t        |d|      }|r1|j                  |j                                |j                  d       yyy)a  Extract and render app description.

    Parameters
    ----------
    app : App
        The app to extract description from.
    help_format : str
        Help format (e.g., "markdown", "rich").
    lines : list[str]
        List to append description to.
    markdownmdNpreserve_markuprD   )r   r   r   strip)r   rF   r.   descriptionpreserve	desc_texts         r,   _render_description_sectionrQ      sX     &c;7K"44 dHM	LL*+LL 	 r-   command_chainc                    t        |       rt        |       }|rr|j                  d       t        |t              r|}nt        |dd      }t        ||      }|j                  |       |j                  d       |j                  d       yyy)zRender usage console block.

    Parameters
    ----------
    app : App
        The app to extract usage from.
    command_chain : list[str]
        Command chain for usage line.
    lines : list[str]
        List to append usage to.
    z
```consoleNFrK   z```rD   )r   r	   r   
isinstancestrr   r
   )r   rR   r.   usage
usage_text
usage_lines         r,   _render_usage_sectionrY      s|     c"LL&%%"
)%uM
*:}EJLL$LLLL  r-   app_namec                     t        | ||r| dnd|||      }|r@|j                  d       |j                  d       t        ||       |j                  d       yy)a  Generate and render table of contents.

    Parameters
    ----------
    app : App
        The app to generate TOC for.
    app_name : str
        Application name for TOC prefixes.
    include_hidden : bool
        Whether to include hidden commands.
    commands_filter : list[str] | None
        Commands to include.
    exclude_commands : list[str] | None
        Commands to exclude.
    skip_filtered_command : bool
        Whether to skip the single filtered command in TOC.
    lines : list[str]
        List to append TOC to.
    r   rD   )r   r   r   r   r   z## Table of ContentsN)r    r   r=   )r   rZ   r   r   r   r   r.   toc_commandss           r,   _render_tocr]      sc    : -%!)(1~r')3L +,Re\2R	 r-   c                 8   |j                          | j                  d      } |dd|       |j                         j                         }|rN| j                  r|j                  d| j                   d       |j                  |       |j                  d       yy)a  Render a parameter panel as-is.

    Parameters
    ----------
    panel : HelpPanel
        The parameter panel to render.
    formatter : MarkdownFormatter
        Formatter to use for rendering.
    lines : list[str]
        List to append rendered content to.
    rD   )titleN****:
)resetcopy
get_outputrM   r_   r   )panel	formatterr.   
panel_copyoutputs        r,   _render_parameter_panelri      s     OO"%JdD*%!!#))+F ;;LL2ekk]%01VR	 r-   entriesr?   normalized_filter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getr   r   )	rj   r?   r   rk   rl   filtered_entriesentryr;   r&   s	            r,   _filter_command_entriesrr     s    6  3;;{{1~H __X.F~$,1C1K$++E2 *(KARTfhno$++E23 r-   	recursiveheading_levelmax_heading_levelgenerate_tocflatten_commandsno_root_titlecode_block_titleskip_preamblec                    ddl m} g }|g }d}nd}|xr |duxr t        |      dk(  }t        | |      \  }}}|r|rd| dn|}n|}|s:|
r|s6t	        ||      }|j                  d|z   d	|        |j                  d
       | j                  j                  dt              }|s|st        | ||       |s|st        | ||       |r|s| j                  rt        | ||||	||       |s| j                  | g      5  | j                  g |      }ddd       t        ||	      \  }}g }t        | d      } ||dz   |d      }D ]d  \  }}|s|r|j                   s|j"                  dk(  r|j$                  D cg c]'  }|j&                  rt)        | |j&                        r&|) } }| sgt+        | ||||      }!|!sy|j,                  r|j                  d|j,                   d       |!D ]  }"|"j&                  s|"j&                  d   }#| d	|# }$t/        |$      }%|"j0                  rt3        |"j0                  dd      nd
}&|&r|j                  d|# d|% d|&        t|j                  d|# d|% d        |j                  d
       G|j"                  dk(  sXt5        |||       g nt        ||	      \  }}g }| j                  rt7        | |      D ]  \  }'}(t9        |'||||(      st;        ||'|      })|r|}*n|
r|s|}*n|dz   }*|duxr t        |      dk(  }+|+xr |duxr |'|d   k(  },|+xr |duxr |d   j=                  |'dz         }-|xr |,}.|xr |xr |-}/|xr |,}0|.sS|/sQd	j?                  |)      }1|rd|1 dn|1}2t	        |*|      }3|j                  d|3z   d	|2        |j                  d
       |0xs |/}4|(j                  | |(g      5  |(j                  j                  d|      }5|5dv }6|4st        |(|)|       t        |(|5|       |r&|(j                  g |5      }7tA        |'||      \  }8}9t        |8|9      \  }:};t        |(d      }<g }=|r|dz   }>n|dz   }> ||>|d      }?|xr |(j                  }@|7D ]  \  }}|s|r|j                   s|j"                  dk(  rstC        |(      rg|j$                  D cg c]'  }|j&                  rt)        |(|j&                        r&|) }A}|Ass@rg } AD ]  }"|"j&                  s|"j&                  d   }#|<jE                  |#      }B|Brt9        |#|=|:|;B      sB|"j0                  rt3        |"j0                  d|6      nd
}&d	j?                  |)|#gz         }$t/        |$      }%|&r| j                  d|# d|% d|&        | j                  d|# d|% d        | s=|j,                  r|j                  d|j,                   d       |jG                  |        |j                  d
       g }!AD ]R  }"|"j&                  s|"j&                  d   }#|<jE                  |#      }B|Brt9        |#|=|:|;B      sB|!j                  |"       T |!s|j,                  r|j                  d|j,                   d       |?jI                          |jK                  d
|!|j"                  |j0                        }C |?dd|C       |?jM                         jO                         }D|Dr|j                  D       |j                  d
       |j"                  dk(  st5        ||?|        |r0|(j                  r#tA        |'||      \  }E}Ft        |E|F      \  }G}Hg }It7        |(|      D ]  \  }J}Kt9        |JIGH|K      st;        |)J|      }L|r|}Mn
|.r|*}Mn|*dz   }MGrtA        JGH      \  }N}Ond}N|xr Eduxr t        E      dk(  xr JEd   k(  }P|xr- Eduxr' t        E      dk(  xr Ed   j=                  Jdz         }QKj                  |(|Kg      5  tQ        K||M|Ld|NFQ|Pxs Q      }Rddd       |j                  R       |j                  d
        ddd        dj?                  |      jS                         dz   }Sddl*}T|TjW                  dd |S      }S|SS # 1 sw Y   xY wc c}w c c}w # 1 sw Y   xY w# 1 sw Y   xY w)!a  Generate markdown 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 (single #).
    max_heading_level : int
        Maximum heading level to use. Headings deeper than this will be capped
        at this level. Standard Markdown supports levels 1-6.
        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] | None
        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] | None
        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 the root application title. Used for plugin contexts.
        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 markdown documentation.
    r   )MarkdownFormatterNTFr0   `#r   rD   rF   )fallback)r   list)rt   r   table_stylecommandr`   ra   rK   z* [`r3   z): r4   	parameter.rH      )r_   rj   formatrN   )rs   r   rt   ru   rR   rv   rw   r   r   rx   ry   rz   
z\n{3,}z

),!cyclopts.help.formatters.markdownr|   lenr   minr   	app_stackresolver   rY   rQ   r   r]   _assemble_help_panelsr   r@   showr   rj   rn   r   rr   r_   r   rN   r   ri   r   r   r   
startswithjoinr   r   ro   r!   rb   	__class__rd   rM   generate_markdown_docsrstripresub)Ur   rs   r   rt   ru   rR   rv   rw   r   r   rx   ry   rz   r|   r.   is_rootskip_current_levelrZ   full_command
base_titler_   effective_levelrF   help_panels_with_groupsr#   r$   r   r?   rf   groupre   ecommand_entriesrp   rq   r;   full_cmd_pathr<   rP   r%   r&   sub_command_chainsub_heading_levelis_single_filteris_exact_targetis_intermediate_pathskip_this_command_titleskip_intermediateskip_target_preambler(   display_fmteffective_sub_levelskip_this_preamblesub_help_formatpreserve_sub
sub_panelssub_commands_filter_for_panelsub_exclude_commands_for_panelnormalized_sub_filter_panelnormalized_sub_exclude_panelsub_command_mapnested_parent_path_for_panelpanel_heading_levelsub_formatterwill_recursecommand_entries_listsub_cmd_appfiltered_panelrh   sub_commands_filtersub_exclude_commandsnormalized_sub_filternormalized_sub_excludenested_parent_pathnested_name
nested_appnested_command_chainnested_heading_levelnested_commands_filterr2   nested_is_targetnested_is_intermediatenested_docsdocr   sU                                                                                        r,   r   r   6  s
   B D E !^_D%@^SEY]^E^ *6c=)I&HlJ '7!L>#\  }m->?o-.aw78R --'''OK mc=%8 m#Ce< McmmC>?DTVhjop ]]C5! 	Q&)&?&?K&P#	Q C\-C
?"$?  )TB &'!+)
	 4 )	ALE5!eEJJ||y( /4mm"tAGGPdehjkjqjqPr1"t"t& $;#[+?Y[v$ 
 ${{r%++e%<= "2 M ;;',{{1~H 0<nAhZ,HM%4]%CF_d_p_pU->->VZ [vx &  ) %tH:T&YK-X Y %tH:T&-K LM LL$,'y%@S)	AX C\-C
?"$?  }} -S.A @	)LD&)k#=?Z\b  !4M4 R  $1!}$1!$1A$5!  /d:Xs??SWX?X.m?$3NmSW[jkl[mSmO n_D%@n_UVEWEbEbcgjmcmEn ! '9&L_# - ]2D ]I]#0#D_  +3D"xx(9:5E,q1<&)*;=N&O#&9 9:!K=IJR  "6!J9J !!3-0 M)"("2"2":":=S^":"_.2DD))&2CUK/O !'!=!=b/!RJ Ur8:UUQ13Q Qj57UQM/1M
 '9PT&UO 460 (.;a.?+.;a.?+$5&9.fl%M
 $-#A1A1AL )3 TQu-%

$ <<949RSY9Z ,1==4&'MabhjkjqjqMr40 4 $8 (  ,24-A !cE',{{38;;q>6E6I6I(6S+6?U,4,H,G,H,7@* -5 05/@/@ -99J9JDbn,o13 )2 9<ARV^U_A_8`1@1O+4,;,B,BT(SWX^W__bclbmCn,o,;,B,BT(SWX^W__`Ca,b1!c4 $3',{{(-r%++e5L(M$)LL$A$)LL$4 46 0-A !GE',{{38;;q>6E6I6I(6S+6?U,4,H,G,H,7@* -5(8(?(?(F!G $4',{{(-r%++e5L(M$1$7$7$95:__.00@/4||494E4E	 6E 6&N %2$n$M-:-E-E-G-M-M-OF'-(-V(<$)LL$4"\\[83E=%PiTQn !1!1@]8:UA=')= E^+-AEA)+A *,&3CFN3[ =)/Z5');=RTjlv  % 0CCTVack/l,+3@043D03Dq3H0 18U +-BDZ952A 6:2
 * F 34 ?F #$7 8A =F !,/B1/E E	 ) * U 34 ?U #$7 8A =U !4A 6 A A+PSBS T	 / (116:2FG *@ **3/=.B2C.B-21A0F1E.D1A.>.XBX+K" [1R({=)aM) M)g@	)F ))E

!
!
#d
*C 
&&FC
(CJi
	Q 	Q8 #uf4P wM) M)sx   >b:='c%cC-c'c
/c
3cB1cA"c%Ac.Cc1C:c+c	,c:ccccc'	)FrD   NNNF)T)TFr0      NTFNNFFF)'__doc__typingr   cyclopts._markupr   cyclopts.corer   cyclopts.docs.baser   r   r   r	   r
   r   r   r   r   r   r   r   r   r   boolrU   r   tupler    r=   dictr@   rE   rQ   rY   r]   ri   setrr   intr    r-   r,   <module>r      s9   ;   ) (     !
 !(,)-$("'3	33 3 #Y%	3
 3i$&3 cT!3  3 
%U

3l=c =d5e;L6M =RV =8E 4 4U
CS ,d3i # $ RV $U  T#Y SW ,u T#Y tCy UY 4)	)) ) #Y%	)
 3i$&)  ) 9) 
)XT#Y 4 4((c5j!( c( 3x$	(
 C4( 
(Z  &*"(,)-"G	GG G 	G
 G 9t#G G G #Y%G 3i$&G G G G 	Gr-   