o
    io8                     @   s  U d Z ddlZddlmZ dee dededefdd	Zd
edefddZ	dee dedede
ee ef fddZdededee dedede
eef fddZdededee dedede
eef fddZdededee dedede
eef fddZeeeee eege
eef f ZeeeeeedZeeef ed< dedB defddZdee dee ddfdd Zdee dee d!eddfd"d#ZdS )$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                 C   s\   |}|t | k r,| | }| }t |t | }|r||kr#|d7 }n	 |S |t | k s|S )a  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indent r   d/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/help/rst_preprocessor.py_skip_indented_block   s   

r   r   c                 C   s@   | sdS | d dv rt | dkp| d dv S td| }|duS )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   r   r   r   _is_list_item1   s   r   c                 C   s  g }|}t d}|t| k r?| | }| }t|t| }||ks$|s8|r+t||}||||f |d7 }nn|t| k s|t dkrI|d }g }	g }
d}|D ]g\}}}|sg|
rd|	d|
 g }
d}qQt|}||k}|r|
r}|	d|
 g }
|	| d}qQ|r|
r|	d|
 g }
|| }|	d| |  d}qQ|r|
r|	d|
 g }
|
| d}qQ|
r|	d|
 |	|fS )a  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_indentr   r   r   _gather_indented_blockF   s`   



r'   directive_namedirective_argcurrent_indentc                 C   s.   | dkrdnd}d| d| d}||d fS )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   r*   prefixtagr   r   r   _handle_version_directive   s   r2   c           	      C   sL   t ||d |\}}d| }d| d}|r"| d| |fS ||fS )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 vr/   r   )r'   r   strip)	r(   r)   r   r   r*   r    next_icontentr1   r   r   r   _handle_deprecated_directive   s   r7   c                 C   sZ  |r|gng }t ||d |\}}|| g }g }	g }
|D ]T}t|}|do,| }|rA|
r;|d|
 g }
|	| q|rU|	rO|d|	 g }	|
| q|	ra|d|	 g }	|
rm|d|
 g }
|| q|	r}|d|	 |
r|d|
 d| }dddd}||  }|rd| d| dnd| d}||fS )	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   
r3   zNote:u   ⚠ Warning:z	See also:)notewarningseealso)r'   extendr   
startswithr4   r   r   )r(   r)   r   r   r*   r    more_paragraphsr5   blockscurrent_listcurrent_code_blockparar$   is_coder6   
prefix_mapr0   	formattedr   r   r   _handle_admonition_directive   sL   
"rF   )r+   versionchanged
deprecatedr9   r:   r;   DIRECTIVE_HANDLERStextc                    s  | sdS |  d}g  g }d}d}h d}|t|k r|| }| }t|t| }|drd|v rtd|}	|	r~|	d	}
|	d
 }t	|
}|ru||
||||\}}|
|v rm|du ret } 
|  n|
| |}nt||d	 |}nt||d	 |}n	 
| |d	7 }|t|k s|r|durt fddt|d	 t D }|st || nt | nt | d  }|S )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.
     r8   Nr   >   r9   r;   r:   z..z::z\.\.\s+(\w+)::\s*(.*)r      c                 3   s    | ]	} |   V  qd S )N)r4   ).0r
   result_partsr   r   	<genexpr>  s    
z,process_sphinx_directives.<locals>.<genexpr>)splitr   r	   r=   r   r   groupr4   rI   getr   r   anyrange_insert_version_tags_at_index_insert_version_tagsr   )rJ   r   version_tagsfirst_inline_directive_idxr
   admonition_directivesr   r   r*   r   r(   r)   handlerr1   r5   has_text_afterresultr   rN   r   process_sphinx_directives6  sT   




!
r^   rO   rX   c                 C   sl   d |}| r/tt| d ddD ]}| |  r'| |  d| | |<  dS q| | dS | | dS )z:Insert version tags at the end of the last non-empty line.r   r   N)r   rU   r   r4   r   )rO   rX   	tags_textidxr   r   r   rW     s   
rW   before_indexc                 C   sX   d |}t|d ddD ]}| |  r#| |  d| | |<  dS q| || dS )zgInsert version tags before the specified index, appending to the last non-empty line before that index.r   r   r_   N)r   rU   r4   insert)rO   rX   rb   r`   ra   r   r   r   rV     s   
rV   )__doc__r   collections.abcr   liststrintr   boolr   tupler'   r2   r7   rF   DirectiveHandlerrI   dict__annotations__r^   rW   rV   r   r   r   r   <module>   sp    *Z





"M
Z&