
    lKj)&                        d Z ddlZddlmZmZ ddlZddlmZmZm	Z	 ddl
mZ ddlmZ erddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZmZ  ee      Z ed       G d d             Z ej@                  dejB                        Z"de#dede#fdZ$ G d dejJ                        Z& G d dee&         Z'y)z7MkDocs plugin for automatic Cyclopts CLI documentation.    N)TYPE_CHECKINGAny)definefield
validators)generate_markdown_docs)
import_app)MkDocsConfig)Files)Page)base)config_options)PluginError)
BasePluginget_plugin_loggerT)kw_onlyc            
          e Zd ZU dZ e ej                  e            Zee	d<    ed ej                  e
            Ze
e	d<    ed ej                  e
            Ze
e	d<    ed	 ej                   ej                  e                  Zee   d	z  e	d
<    ed	 ej                   ej                  e                  Zee   d	z  e	d<    ed ej                  e            Zee	d<    ed ej                  e            Zee	d<    ed ej                  e            Zee	d<    ed ej                  e            Zee	d<    ed ej                  e            Zee	d<    ed ej                  e            Zee	d<    ed	 ej                   ej                  e                  Zed	z  e	d<   ed	d	ddede
d	z  de
d	z  dd fd       Zy	)DirectiveOptionsz-Configuration for the ::: cyclopts directive.)	validatormodule   )defaultr   heading_level   max_heading_levelNcommandsexclude_commandsT	recursiveFinclude_hiddenflatten_commandsgenerate_toccode_block_titleskip_preamble
usage_namedefault_heading_leveldefault_max_heading_leveldirective_textr&   r'   returnc                N   |j                         j                  d      }|r'|d   j                         j                  d      r|dd }dj                  |      }t	        j
                  |      xs i }t        |t              st        d      d|vrt        d      ||j                  d	|       ||j                  d
|       |j                         D ci c]  \  }}|j                  dd      | }	}}	  | di |	S c c}}w # t        $ r}
t        d|
       |
d}
~
ww xY w)a  Parse options from a ::: cyclopts directive block.

        Expected format:
            ::: cyclopts
                module: myapp.cli:app
                heading_level: 2
                max_heading_level: 6
                recursive: true
                commands:
                  - cmd1
                  - cmd2

        Parameters
        ----------
        directive_text : str
            The directive text to parse.
        default_heading_level : int | None
            Default heading level from plugin config. Used if :heading-level: not specified.
        default_max_heading_level : int | None
            Default max heading level from plugin config. Used if :max-heading-level: not specified.
        
r   ::: cyclopts   Nz=Invalid YAML in ::: cyclopts directive: expected a dictionaryr   z:The "module" option is required for ::: cyclopts directiver   r   -_z!Error creating DirectiveOptions:  )stripsplit
startswithjoinyaml	safe_load
isinstancedict	TypeError
ValueError
setdefaultitemsreplace)clsr(   r&   r'   linesyaml_contentoptionskeyvaluenormalized_optionses              d/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/cyclopts/ext/mkdocs.pyfrom_directive_blockz%DirectiveOptions.from_directive_block,   s6   : $$&,,T2 U1X^^%00@!"IEyy'...4"'4([\\7"YZZ ,0EF$024MN NU]]_]_zsEckk#s3U:_]	M,+,, ^  	M@DE1L	Ms   D9D 	D$DD$)__name__
__module____qualname____doc__r   r   instance_ofstrr   __annotations__intr   r   optionallistr   r   boolr   r   r    r!   r"   r#   r$   classmethodrG   r0       rF   r   r      s   7"8*"8"8"=>FC>q4JJ4J4J34OPM3P"18N
8N8Ns8STsT!&t?Rz?R?RSiS]SiSijnSo?p!qHd3i$q). 3
 3 34JJ4J4J44P Q*d3i$&  D4JJ4J4J44PQItQ :P*:P:PQU:VWNDW"5<RJ<R<RSW<XYdYt7Mz7M7Md7STL$T"5<RJ<R<RSW<XYdY9O9O9OPT9UVM4V"4;N:;N;NOezOeOefiOj;klJd
l
 -1047M7M  #Tz	7M
 $':7M 
7M 7MrT   r   z^::: cyclopts\n(?:[ \t]+.*\n?)*markdownplugin_configr)   c                    g t        j                  dt         j                  t         j                  z        }|j	                  |       D ]1  }j                  |j                         |j                         f       3 | j                  d      }d}d}d}t        |      D ]  \  }}	t        |	      dz   }
|s]|dk(  xs ||dz
     j                          }|	j                  d      xs |	j                  d      xr |	j                         }|rg|red}|}n`|	j                  d      xs |	j                  d      xr |	j                         }|	j                          }|s|sj                  ||f       d}||
z  } |rj                  ||f       d	t        d
t        ffddt         j                  d
t         ffd}t"        j%                  ||       }|S )a  Process all ::: cyclopts directives in markdown content.

    Parameters
    ----------
    markdown : str
        The markdown content containing ::: cyclopts directives.
    plugin_config : CycloptsPluginConfig
        The plugin configuration with default values. If None, uses DirectiveOptions defaults.

    Returns
    -------
    str
        The markdown content with directives replaced by generated documentation.
    z^[`~]{3,}.*?^[`~]{3,}r+   Fr   r-   z    	Tposr)   c                 8    D ]  \  }}|| cxk  r|k  s y  y)z+Check if a position is inside a code block.TFr0   )rY   startendcode_blockss      rF   is_in_code_blockz5process_cyclopts_directives.<locals>.is_in_code_block   s+    %JE3!c! " & rT   matchc                 `    | j                               r| j                  d      S | j                  d      }	 	r	j                  nd }	r	j                  nd }t        j                  |||      }t        |j                        }t        ||j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                   d|j"                  |j$                  |j&                        }|S # t(        $ r}t+        d|       |d }~ww xY w)Nr   r%   T)r   r   r   r   r!   r    commands_filterr   no_root_titler"   r#   r$   z)Error processing ::: cyclopts directive: )r[   groupr&   r'   r   rG   r	   r   r   r   r   r   r   r!   r    r   r   r"   r#   r$   	Exceptionr   )
r_   r(   default_headingdefault_max_headingrA   appmarkdown_docsrE   r^   rV   s
           rF   replace_directivez6process_cyclopts_directives.<locals>.replace_directive   s$   EKKM*;;q>!Q	VERmAAX\OMZ-"I"I`d&;;&5*= < G W^^,C2!++&55%33")";";$11!(!9!9 ' 0 0!(!9!9"!(!9!9%33"--M  !  	V I!MNTUU	Vs   CD 	D-D((D-)recompile	MULTILINEDOTALLfinditerappendr[   r\   r2   	enumeratelenr1   r3   rO   rR   MatchrM   DIRECTIVE_PATTERNsub)rU   rV   fenced_patternr_   r?   in_indented_blockblock_startcurrent_posilineline_len
prev_blankis_indentedis_blankri   	processedr]   r^   s    `              @@rF   process_cyclopts_directivesr   q   s     K ZZ 8",,:RSN((2EKKM599;78 3
 NN4 EKKU#4t9q= !a;uQU|'9'9';#;J??62Kdood6K]QUQ[Q[Q]Kk$(!)  ??62Kdood6K]QUQ[Q[Q]K::<'H x""K#=>$)!x/ $4 K56c d %V %Vc %VP "%%&7BIrT   c                   `    e Zd ZdZ ej
                  ed      Z ej
                  ed      Zy)CycloptsPluginConfigz4Configuration schema for the Cyclopts MkDocs plugin.r   )r   r   N)	rH   rI   rJ   rK   cTyperO   r&   r'   r0   rT   rF   r   r      s+    >"AFF32 &sA 6rT   r   c            
       ,    e Zd ZdZdeddddddd	ef
d
Zy)CycloptsPlugina{  MkDocs plugin to generate Cyclopts CLI documentation.

    Usage in mkdocs.yml:
        plugins:
          - cyclopts:
              default_heading_level: 2

    Usage in Markdown files:
        ::: cyclopts
            :module: myapp.cli:app
            :heading-level: 2
            :recursive: true
            :commands: init, build
            :exclude-commands: debug
    rU   pager   configr
   filesr   r)   c                :    d|vr|S t        || j                        S )zProcess ::: cyclopts directives in markdown content.

        This event is called after the page's markdown is loaded from file
        but before it's converted to HTML.
        r,   )r   r   )selfrU   r   r   r   kwargss         rF   on_page_markdownzCycloptsPlugin.on_page_markdown   s"     )O*8T[[AArT   N)rH   rI   rJ   rK   rM   r   r0   rT   rF   r   r      s7     	B 	Bv 	B~ 	B^e 	Btw 	BrT   r   )(rK   rj   typingr   r   r5   attrsr   r   r   cyclopts.docs.markdownr   cyclopts.utilsr	   mkdocs.config.defaultsr
   mkdocs.structure.filesr   mkdocs.structure.pagesr   mkdocs.configr   r   r   mkdocs.exceptionsr   mkdocs.pluginsr   r   rH   loggerr   rk   rl   rs   rM   r   Configr   r   r0   rT   rF   <module>r      s    = 	 %  + + 9 %3,+  - ) 8	8	$ JM JM JMb BJJ&LL k# kc kc k\74;; 7BZ 45 BrT   