
    k7io8                        % S r SSKrSSKJr  S\\   S\S\S\4S jrS	\S\4S
 jr	S\\   S\S\S\
\\   \4   4S jrS\S\S\\   S\S\S\
\\4   4S jrS\S\S\\   S\S\S\
\\4   4S jrS\S\S\\   S\S\S\
\\4   4S jr\\\\\   \\/\
\\4   4   r\\\\\\S.r\\\4   \S'   S\S-  S\4S jrS\\   S\\   SS4S jrS\\   S\\   S\SS4S jrg)a  Preprocessing utilities for reStructuredText content.

This module provides workarounds for limitations in the rich_rst library when rendering
Sphinx directives. While rich_rst handles standard reStructuredText well, it doesn't
support Sphinx-specific directives (versionadded, deprecated, note, warning, etc.).

Since Cyclopts docstrings may contain these directives, we preprocess RST content to
convert Sphinx directives into plain text annotations before passing to rich_rst. This
ensures users see meaningful information in CLI help rather than raw directive syntax.

This is a pragmatic workaround; ideally this functionality would be in rich_rst itself.
    N)Callablelinesstart_indexbase_indentreturnc                     UnU[        U 5      :  aQ  X   nUR                  5       n[        U5      [        U5      -
  nU(       a  Xb:  a  US-  nO U$ U[        U 5      :  a  MQ  U$ )aR  Skip over lines indented more than base_indent.

Parameters
----------
lines : list[str]
    All lines in the text.
start_index : int
    Index to start from.
base_indent : int
    Base indentation level; lines must be indented more than this.

Returns
-------
int
    Index of the first non-matching line (or len(lines) if reached end).
   )lenlstrip)r   r   r   ilinestrippedindents          X/home/james-whalen/.local/lib/python3.13/site-packages/cyclopts/help/rst_preprocessor.py_skip_indented_blockr      se    " 	A
c%j.x;;=TS]*6/FAH c%j. H    r   c                     U (       d  gU S   S;   a  [        U 5      S:H  =(       d    U S   S;   $ [        R                  " SU 5      nUSL$ )zCheck if a line is a list item.

Parameters
----------
line : str
    The stripped line to check.

Returns
-------
bool
    True if the line starts with a list marker.
Fr   )-*+r	   ) 	z^\d+[\.\)](\s|$)N)r
   rematch)r   r   s     r   _is_list_itemr   1   sN     Aw/!4yA~7aK!77HH($/Er   c                    / nUn[        S5      nU[        U 5      :  at  X   nUR                  5       n[        U5      [        U5      -
  nX:  d  U(       d+  U(       a  [        XX5      nUR	                  XhU45        US-  nOOU[        U 5      :  a  Mt  U[        S5      :X  a  US-   n/ n	/ n
SnU GH!  u  pnU(       d-  U
(       a"  U	R	                  SR                  U
5      5        / n
SnM;  [        U5      nX:  nU(       a>  U
(       a"  U	R	                  SR                  U
5      5        / n
U	R	                  U5        SnM  U(       aH  U
(       a"  U	R	                  SR                  U
5      5        / n
X-
  nU	R	                  SU-  U-   5        SnM  U(       a)  U
(       a"  U	R	                  SR                  U
5      5        / n
U
R	                  U5        SnGM$     U
(       a   U	R	                  SR                  U
5      5        X4$ )aj  Gather lines indented more than base_indent, preserving structure.

This function preserves paragraph breaks (blank lines), list structure,
and code block indentation while gathering indented content. List items
are kept on separate lines. Lines that are indented beyond the minimum
content indentation (like code blocks) preserve their relative indentation.

Parameters
----------
lines : list[str]
    All lines in the text.
start_index : int
    Index to start from.
base_indent : int
    Base indentation level; lines must be indented more than this.

Returns
-------
content_lines : list[str]
    Content lines preserving paragraph structure. Each element represents
    either a paragraph (multiple lines joined with spaces), a single
    list item line, or indented code lines.
end_index : int
    Index of the first non-matching line (or len(lines) if reached end).
infr	   Fr   T)floatr
   r   minappendjoinr   )r   r   r   collected_linesr   
min_indentr   r   r   
paragraphscurrent_paragraphlast_was_empty_is_listis_code_blockrelative_indents                   r   _gather_indented_blockr+   F   s   6 OAuJ
c%j.x;;=TS]*x 4
""D(#;<FA c%j. U5\! 1_
 JN.8 !!#((+<"=>$&!!N#H-G"/M$%%chh/@&AB(*%!!(+!&$%%chh/@&AB(*%"("5!!#"7("BC!&!&7%%chh/@&AB(*%!((2!&=  /@ #((#456=r   directive_namedirective_argcurrent_indentc                 4    U S:X  a  SOSnSU SU S3nXcS-   4$ )a  Handle versionadded and versionchanged directives.

Parameters
----------
directive_name : str
    Name of the directive (e.g., "versionadded").
directive_arg : str
    Version number argument.
lines : list[str]
    All lines in the text.
start_index : int
    Current line index.
current_indent : int
    Current indentation level.

Returns
-------
tag : str
    Formatted tag text.
next_index : int
    Next line index to process.
versionaddedAddedChanged[z in v]r	    )r,   r-   r   r   r.   prefixtags          r   _handle_version_directiver8      s3    2 '.8WiFfXU=/
+Car   c                     [        X#S-   U5      u  pVSR                  U5      R                  5       nSU S3nU(       a	  U SU 3U4$ UU4$ )a  Handle deprecated directive with optional content.

Parameters
----------
directive_name : str
    Name of the directive ("deprecated").
directive_arg : str
    Version number argument.
lines : list[str]
    All lines in the text.
start_index : int
    Current line index.
current_indent : int
    Current indentation level.

Returns
-------
tag : str
    Formatted tag text with optional content.
next_index : int
    Next line index to process.
r	   

u   [⚠ Deprecated in vr4   r   )r+   r!   strip)	r,   r-   r   r   r.   r$   next_icontentr7   s	            r   _handle_deprecated_directiver>      s^    2 0QWJkk*%++-G q
1C!(cU!G9699c699r   c                    U(       a  U/O/ n[        X#S-   U5      u  pgUR                  U5        / n/ n	/ n
U GH   n[        U5      nUR                  S5      =(       a    UR	                  5       nU(       a<  U
(       a"  UR                  SR                  U
5      5        / n
U	R                  U5        Mz  U(       a<  U	(       a"  UR                  SR                  U	5      5        / n	U
R                  U5        M  U	(       a"  UR                  SR                  U	5      5        / n	U
(       a"  UR                  SR                  U
5      5        / n
UR                  U5        GM#     U	(       a   UR                  SR                  U	5      5        U
(       a   UR                  SR                  U
5      5        SR                  U5      R	                  5       nSSSS.nX   nU(       a	  SU SU S3OSU S3nUU4$ )	a  Handle note, warning, and seealso directives.

Parameters
----------
directive_name : str
    Name of the directive (e.g., "note", "warning", "seealso").
directive_arg : str
    Inline content on the directive line.
lines : list[str]
    All lines in the text.
start_index : int
    Current line index.
current_indent : int
    Current indentation level.

Returns
-------
tag : str
    Formatted tag text with content.
next_index : int
    Next line index to process.
r	   r   
r:   zNote:u   ⚠ Warning:z	See also:)notewarningseealso)r+   extendr   
startswithr;   r    r!   )r,   r-   r   r   r.   r$   more_paragraphsr<   blockscurrent_listcurrent_code_blockparar(   is_coder=   
prefix_mapr6   	formatteds                     r   _handle_admonition_directiverN      s   2 %2-rJ4U!O^\Oo&FL%//#&74::<!dii(:;<%'"%dii56!%%d+ dii56!!dii(:;<%'"MM$1 6 dii-.dii 234kk&!'')G !J
 'F18$vhay-VHD>QIfr   )r0   versionchanged
deprecatedrA   rB   rC   DIRECTIVE_HANDLERStextc           	        ^ U (       d  gU R                  S5      n/ m/ nSnSn1 SknU[        U5      :  GaL  X   nUR                  5       n[        U5      [        U5      -
  nUR                  S5      (       a  SU;   a  [        R
                  " SU5      n	U	(       a  U	R                  S	5      n
U	R                  S
5      R                  5       n[        R                  U
5      nU(       aS  U" XXU5      u  pX;   a.  Uc  [        T5      nTR                  UR                  5       5        OUR                  U5        UnO6[        XS	-   U5      nO&[        XS	-   U5      nOTR                  U5        US	-  nU[        U5      :  a  GML  U(       a[  UbL  [        U4S j[        US	-   [        T5      5       5       5      nU(       d  [        TX#5        O[        TU5        O[        TU5        SR!                  T5      R                  5       nU$ )u  Process Sphinx directives in reStructuredText content for CLI help display.

Converts Sphinx directives to readable format:
- .. versionadded:: X -> [Added in vX]
- .. versionchanged:: X -> [Changed in vX]
- .. deprecated:: X -> [⚠ Deprecated in vX]
- .. note:: content -> Note: content
- .. warning:: content -> ⚠ Warning: content
- .. seealso:: content -> See also: content

Unknown directives are silently removed but logged as debug messages.

Parameters
----------
text : str | None
    The reStructuredText content to process.

Returns
-------
str
    Processed text with directives converted to inline annotations.
    Returns empty string if input is None or empty.
 r@   Nr   >   rA   rC   rB   z..z::z\.\.\s+(\w+)::\s*(.*)r	      c              3   J   >#    U  H  nTU   R                  5       v   M     g 7f)N)r;   ).0r   result_partss     r   	<genexpr>,process_sphinx_directives.<locals>.<genexpr>  s%      !1iAQ%%''1is    #)splitr
   r   rE   r   r   groupr;   rQ   getr    r   anyrange_insert_version_tags_at_index_insert_version_tagsr!   )rR   r   version_tagsfirst_inline_directive_idxr   admonition_directivesr   r   r.   r   r,   r-   handlerr7   r<   has_text_afterresultrX   s                    @r   process_sphinx_directivesrh   6  s   0 JJtELL!%	A ;
c%j.x;;=TS]2t$$)9HH5x@E!&Q %A 4 4 6,00@").Sa"bKC%>5=9<\9J6$++CIIK8 %++C0A,UE>JA(A~F%FA= c%j.B %1  !167QTU7UWZ[gWh1i! N "-lLe %\<@ !|<YY|$**,FMr   rX   rb   c                    SR                  U5      nU (       aV  [        [        U 5      S-
  SS5       H'  nX   R                  5       (       d  M  X    SU 3X'     g   U R	                  U5        gU R	                  U5        g)z:Insert version tags at the end of the last non-empty line.r   r	   N)r!   r_   r
   r;   r    )rX   rb   	tags_textidxs       r   ra   ra     sz    &I\*Q.B7C &&(('3'8&99+$F! 8
 	I&I&r   before_indexc                     SR                  U5      n[        US-
  SS5       H'  nX   R                  5       (       d  M  X    SU 3X'     g   U R                  X#5        g)zgInsert version tags before the specified index, appending to the last non-empty line before that index.r   r	   rj   N)r!   r_   r;   insert)rX   rb   rm   rk   rl   s        r   r`   r`     sd    &I\A%r2.""$$#/#4"5Qyk BL /
 0r   )__doc__r   collections.abcr   liststrintr   boolr   tupler+   r8   r>   rN   DirectiveHandlerrQ   dict__annotations__rh   ra   r`   r5   r   r   <module>rz      s   
 $S	  # RU >  *W$s) W# WC WTYZ^_bZcehZhTi Wt  (+ 48I LO ad 
38_ <::(+:48I:LO:ad:
38_:>JJ(+J48IJLOJadJ
38_JZ S#tCy#s;U38_LM  ./.(++3 D../ WC$J W3 Wt'tCy 'S	 'd '	1S	 	1c 	1be 	1jn 	1r   