
    i%                        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            
       H   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	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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              y/home/jay/workspace/.worktrees/task-2116-dev1/scripts/.codegraph-venv/lib/python3.12/site-packages/cyclopts/ext/mkdocs.pyfrom_directive_blockz%DirectiveOptions.from_directive_block+   s5   : $$&,,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#   classmethodrF   r/       rE   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
 -1047M7M  #Tz	7M
 $':7M 
7M 7MrS   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.TFr/   )rX   startendcode_blockss      rE   is_in_code_blockz5process_cyclopts_directives.<locals>.is_in_code_block   s0    % 	JE3!c! "	 rS   matchc                 J    | 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$                        }|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#   z)Error processing ::: cyclopts directive: )rZ   groupr%   r&   r   rF   r	   r   r   r   r   r   r   r!   r    r   r   r"   r#   	Exceptionr   )
r^   r'   default_headingdefault_max_headingr@   appmarkdown_docsrD   r]   rU   s
           rE   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appendrZ   r[   r1   	enumeratelenr0   r2   rN   rQ   MatchrL   DIRECTIVE_PATTERNsub)rT   rU   fenced_patternr^   r>   in_indented_blockblock_startcurrent_posilineline_len
prev_blankis_indentedis_blankrh   	processedr\   r]   s    `              @@rE   process_cyclopts_directivesr   p   s     K ZZ 8",,:RSN((2 9EKKM599;789
 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N "%%&7BIrS   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)	rG   rH   rI   rJ   cTyperN   r%   r&   r/   rS   rE   r   r      s+    >"AFF32 &sA 6rS   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
    rT   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   )selfrT   r   r   r   kwargss         rE   on_page_markdownzCycloptsPlugin.on_page_markdown   s"     )O*8T[[AArS   N)rG   rH   rI   rJ   rL   r   r/   rS   rE   r   r      s7     	B 	Bv 	B~ 	B^e 	Btw 	BrS   r   )(rJ   ri   typingr   r   r4   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   rG   loggerr   rj   rk   rr   rL   r   Configr   r   r/   rS   rE   <module>r      s    = 	 %  + + 9 %3,+  - ) 8	8	$ IM IM IM` BJJ&LL j# jc jc jZ74;; 7BZ 45 BrS   