
    -Phd                     T   d 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mZmZ ddlmZ ddl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 ddlmZ ddl m!Z! dede"fdZ#dede"de$de"fdZ%e G d d                      Z&dede"de"ddfdZ'deddfdZ(dede"de"fd Z)dS )!z4Methods to build the toctree used in the html pages.    )	dataclass)cachecount)dedent)IteratorListTupleUnion)urlparseN)BeautifulSoup)nodes)Node)toctree)Sphinx)TocTree)_   )traverse_or_findallnodereturnc                 2    d|                                   dS )a^  Render a node with HTML tags that activate MathJax processing.

    This is meant for use with rendering section titles with math in them, because
    math outputs are ignored by pydata-sphinx-theme's header.

    related to the behaviour of a normal math node from:
    https://github.com/sphinx-doc/sphinx/blob/master/sphinx/ext/mathjax.py#L28
    z-<span class="math notranslate nohighlight">\(z	\)</span>)astext)r   s    [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pydata_sphinx_theme/toctree.pyadd_inline_mathr      s      	XDKKMMWWW    apppagename
startdepthc                    t          | j                  }t          j        dd         dk    rddlm} g  || j        j        |          }n|                    |          }	 ||          }n# t          $ r d}Y nw xY w||fS )zm
    Get the name of `pagename`'s ancestor that is rooted `startdepth` levels below the
    global root.
    N      r!   r   _get_toctree_ancestors)	r   envsphinxversion_info#sphinx.environment.adapters.toctreer%   toctree_includesget_toctree_ancestors
IndexError)r   r   r   r   r%   	ancestorsouts          r   _get_ancestor_pagenamer/   %   s    
 cgG2A2&((NNNNNNQ,,SW-ExPPQ		11(;;	$   
  <s   "	A, ,A;:A;c                   <    e Zd ZU dZeed<   eed<   eed<   eed<   dS )LinkInfoz#Dataclass to generate toctree data.
is_currenthreftitleis_externalN)__name__
__module____qualname____doc__bool__annotations__str r   r   r1   r1   <   sB         --
IIIJJJr   r1   templatenamec                    
 ddt           f fd}t          dt          dt          t                   fd            dt          ffdt          dt          t
                   f fd            t          dt          t          t          t                   f         ffd	            
	 ddt           dt          dt          f
fd}t          	 ddt          dt           dt           dt          t          t          f         f fd            }t          ddt          dt          ffd            }dt          t                   ffd}	d<   |d<   |d<   |d<   |d<   |	d<   dS ) z9Add functions so Jinja templates can add toctree objects.r   r   c                     t          |           \  }}|dS |                    dd          rdS t          ||fi |}|du S )al  Check if there's a sidebar TocTree that needs to be rendered.

        Parameters:
            startdepth : The level of the TocTree at which to start. 0 includes the
            entire TocTree for the site; 1 (default) gets the TocTree for the current
            top-level section.

            kwargs : passed to the Sphinx `toctree` template function.
        r   r   r   NTincludehiddenF)r/   getget_nonroot_toctree)r   kwargsancestornametoctree_objr   r   r   s        r   suppress_sidebar_toctreez7add_toctree_functions.<locals>.suppress_sidebar_toctreeK   s     %;h:%
 %
 %
!k 4::ou-- 	 5
 &<
 
8>
 
 $r   base_idr   c              3   X   K   t          d          D ]}|dk    r| V  |  d| V  d S )Nr   )start-r   )rI   ns     r   get_or_create_id_generatorz9add_toctree_functions.<locals>.get_or_create_id_generatorg   sX      Q 	' 	'AAvv &&1&&&&&&		' 	'r   c                 4    t           |                     S )z
        Create an id that is unique from other ids created by this function at build
        time.

        The function works by sequentially returning "<base_id>", "<base_id>-2",
        "<base_id>-3", etc. each time it is called.
        )next)rI   rN   s    r   unique_html_idz-add_toctree_functions.<locals>.unique_html_ido   s     ..w77888r   c            
         t          j                  } t          j        dd         dk    rddlm} g  |j        j                  }n|                               }|r	|d         }nd}j        j        j	        j
                 }g }t          |t                    D ]}|j        d         D ]\  }}|dk    r|j        d	         n|}|`d
}j        j        |         j        D ]F}	t!          |	t"          j                  r|t'          |	          z  }/||	                                z  }Gt+          t-          |          j                  }
|
r|n d         |          }|                    t3          ||k    |||
                     ݌d         D ]4}|                    t3          d|d         |d         d                     5|S )zGenerate informations necessary to generate nav.

        Instead of messing with html later, having this as a util function
        should make it slightly easier to generate different html snippet for
        sidebar or navbar.
        Nr!   r"   r   r$   entriesselfparent pathto)r2   r3   r4   r5   theme_external_linksFurlnameT)r   r&   r'   r(   r)   r%   r*   r+   tocsconfigroot_docr   TocTreeNodeClass
attributestitleschildren
isinstancer   mathr   r   r:   r   netlocappendr1   )r   r%   header_pagesactive_header_pageroot_toc
links_datatocr4   pager   is_absolute	link_hrefexternal_linkr   contextr   s                r   _generate_nav_infoz1add_toctree_functions.<locals>._generate_nav_infoy   s?    #'"" rr"f,,RRRRRR
 Y33CG4LhWWXLL"88BBL 	&!-b!1!% 7<
 34
 'x1ABB !	 !	C  #~i8  t376>>s~h//t
 =E #t 4 = 3 3%dEJ77 3!_T%:%::EE!T[[]]2EE #8D>>#899$/LDD5FWX5Ft5L5L	!!$(,>$>&#$/	     /B %%;< 	 	M$&u-'/ $	      r   c                 B   	 t          |           } n # t          $ r t          d|            w xY w 	            }g }g }t          d          }d}d}d}|d|          D ]L}|                    |                    |j        rdnd|||j        rd	nd
|j        |j	                             M|| d         D ]R}|                    |                    |j        rdnd|dz   |z   d|j        rd	nd
|j        |j	                             Sd
                    |          |fS )a(  Return html for navbar and dropdown.

        Given the number of links before the dropdown, return the html for the navbar,
        as well as the list of links to put in a dropdown.

        Returns:
            - HTML str for the navbar
            - list of HTML str for the dropdown
        z'n_links_before_dropdown is not an int: z
            <li class="{nav_item} {active}">
              <a class="{nav_link} nav-{ext_int}" href="{href}">
                {title}
              </a>
            </li>
            nav-itemnav-linkzdropdown-itemNzcurrent activerW   externalinternal)activenav_linknav_itemext_intr3   r4    
)int	Exception
ValueErrorr   rf   formatr2   r5   r3   r4   join)
n_links_before_dropdownrj   
links_htmllinks_dropdownboilerplatery   rx   dropdown_itemlinkrq   s
            r   $_generate_header_nav_before_dropdownzCadd_toctree_functions.<locals>._generate_header_nav_before_dropdown   s   	&)*A&B&B## 	 	 	S:QSS  	 ('))


 
 '7 778 
	 
	D""/3F++B%%*.*:JJJ
* #  	 	 	 	 6778 
	 
	D!!""/3F++B%^m;*.*:JJJ
* #  	 	 	 	 yy$$n44s    0   Morer   dropdown_textc           
           |           \  }}|r? d          }d                     |          }|d| dt          |           d| d| d	z  }|S )a	  Generate top-level links that are meant for the header navigation.

        We use this function instead of the TocTree-based one used for the
        sidebar because this one is much faster for generating the links and
        we don't need the complexity of the full Sphinx TocTree.

        This includes two kinds of links:

        - Links to pages described listed in the root_doc TocTrees
        - External links defined in theme configuration

        Additionally it will create a dropdown list for several links after
        a cutoff.

        Parameters:
            n_links_before_dropdown:The number of links to show before nesting the
                remaining links in a Dropdown element.
            dropdown_text:Text of the dropdown element button.
        zpst-nav-more-linksr|   z
            <li class="nav-item dropdown">
                <button class="btn dropdown-toggle nav-item" type="button"
                data-bs-toggle="dropdown" aria-expanded="false"
                aria-controls="z">
                    z3
                </button>
                <ul id="z-" class="dropdown-menu">
                    z5
                </ul>
            </li>
            )r   r   )r   r   r.   r   dropdown_idlinks_dropdown_htmlr   rQ   s         r   generate_header_nav_htmlz7add_toctree_functions.<locals>.generate_header_nav_html  s    , CB#
 
^  	(.)=>>K"&))N";";  !,	 
 }%%  %  )   C 
r   kindshow_nav_levelc                    |dk    r d         di |}nVt          |          \  }}|t          d          t          ||fi |}j                            |          d         }t          |d          } |dd	d
i          D ]}	|	d	                             d           |                    d          D ]P}	|	                    d          r9|	                    d          d         }
d|
v r|
dk    r|		                                 Q| dk    r0 |dd          D ]+}g |j
                            d	g           dd|j
        d	<   ,|dk    r|                    dd	di          }t          |          rzt          dd          }|D ]e}|j        D ]}|j        dk    r|} n|                    dd	di          }	|	                    ||g           |j                            |	           f|}t'          |           t)          t+          |                    D ]#}|                    d| d          D ]}d|d<   $|S )a  Return the navigation link structure in HTML.

        This is similar to Sphinx's own default TocTree generation, but it is modified
        to generate TocTrees for *second*-level pages and below (not supported
        by default in Sphinx).
        This is used for our sidebar, which starts at the second-level page.

        It also modifies the generated TocTree slightly for Bootstrap classes
        and structure (via BeautifulSoup).

        Arguments are passed to Sphinx "toctree" function (context["toctree"] below).

        ref: https://www.sphinx-doc.org/en/master/templating.html#toctree

        Parameters:
            kind : "sidebar" or "raw". Whether to generate HTML meant for sidebar
                navigation ("sidebar") or to return the raw BeautifulSoup object
                ("raw").
            startdepth : The level of the toctree at which to start. By default,
                for the navbar uses the normal toctree (`startdepth=0`), and for the
                sidebar starts from the second level (`startdepth=1`).
            show_nav_level : The level of the navigation bar to toggle as visible on
                page load. By default, this level is 1, and only top-level pages are
                shown, with drop-boxes to reveal children. Increasing `show_nav_level`
                will show child levels as well.
            kwargs : passed to the Sphinx `toctree` template function.

        Returns:
            HTML string (if kind == "sidebar") OR BeautifulSoup object
                (if kind == "raw")
        r   r   rA   NzTemplate requested to generate a TocTree fragment but no suitable ancestor found to act as root node. Please report this to theme developers.fragmenthtml.parserliclasscurrentrw   ar3   #sidebarulF	recursivenavz
bd-sidenavpcaptionattrsz<ul class='list-caption'></ul>
toctree-l0zli.toctree-lz
 > detailsopenr=   )r/   RuntimeErrorrD   builderrender_partialr   rf   selectfind	decomposer   rC   find_alllennext_siblingsr[   new_tagextendr   add_collapse_checkboxesranger}   )r   r   r   rE   html_toctreerF   rG   toctree_elementsoupr   r3   r   partcaptionsnew_soupr   siblingtoclistiidetailsr   rp   r   s                      r   generate_toctree_htmlz4add_toctree_functions.<locals>.generate_toctree_html9  s   F ??-79-7777LL )?(z) ) )%L+ #""   2X|[ <B O ;55oFFzRL\=99 $tgy122 	) 	)BwKx(((( ++d## 	# 	#Bwws|| #wws||F+$;;43;;LLNNN9d45111 V V$Ubhll7B&?&?$U$U$U!!
 ""#}}S)8L}MM|$$ $,8-   H $0 	/ 	/ (/'< & &G&|t33*1 %  4 "\\$w6M\NN		7G"4555 **2....#D $D))) C//00 - -#{{+H"+H+H+HII - -G&,GFOO- r   htmlc                    d	vrdS t          	d         d          }	fd |                    d          d            |d          D ]#}g |                    dg           dd	d
|d<   $ |d          D ]h}g |                    dg           dd|d<   |                    d          r2|                    d          }g |                    dg           d|d<   i|                    d          }t	          |          dk    r6|d         }|                    d          sd}n|                    d          }n|}| dk    r|S |S )z)Return the within-page TOC links in HTML.rk   rW   r   c                    | d S |d         dz   k    rg |                      dg           d| d<    | dd          D ]F}g |                     dg           d| |d<    |                    d	d          |dz              Gd S )
Ntheme_show_toc_levelr   r   visibler   Fr   ztoc-hr   )rC   r   )r   levelr   add_header_level_recursiverp   s      r   r   zTadd_toctree_functions.<locals>.generate_toc_html.<locals>.add_header_level_recursive  s    z!781<==?w 3 3?Y?7b/// V VEw 3 3E_U__E7**277457+I+I5ST9UUUUV Vr   r   r   r   r   zsection-navzflex-columnr   rs   z	toc-entryr   rt   z.toc-h1r   z.toc-h2r   )r   r   rC   r   r   )
r   r   r   r   r   
h1_headersr4   r.   r   rp   s
           @r   generate_toc_htmlz0add_toctree_functions.<locals>.generate_toc_html  s    2WU^];;	V 	V 	V 	V 	V 	V 	#"499T??A666 $t** 	V 	VBUBFF7B//UUU}UBwKK$t** 	? 	?BIBFF7B//II[IBwKwws|| ?GGCLL>quuWb11>:>'
 [[++
z??aqME<<	** 'jj&& C 6>>JKr   c                                           dd          } dddd}| |vr't          d|                                 d|            ||          S )	z8Return the class that aligns the navbar based on config.theme_navbar_aligncontent)zcol-lg-3zcol-lg-9me-auto)rW   rW   r   )rW   rW   zms-auto)r   leftrightz(Theme option navbar_align must be one ofz, got: )rC   r   keys)alignalign_optionsrp   s     r   navbar_align_classz1add_toctree_functions.<locals>.navbar_align_class  s    0)<<:'(
 

 %%8 %%''8 8058 8   U##r   rQ   r   rH   r   r   r   N)r   )r   r   )r   r   )r   )	r}   r   r<   r   r	   r1   r
   r   r   )r   r   r>   rp   doctreerH   r   r   r   r   r   rq   rN   rQ   s   `` `      @@@@r   add_toctree_functionsr   F   s   
 S       8 'C 'HSM ' ' ' U'9 9 9 9 9 9 9 RX R R R R R R R URh :5	sDI~	:5 :5 :5 :5 :5 U:5z @F* *!$*9<*	* * * * * * *\ >?e ee"e8;e	}c!	"e e e e e e e UeN . . . . . . . . U.`$S	 $ $ $ $ $ $ !/G*BG&'*BG&''<G#$#4G $6G !!!r   r   c                 |   |                      dd          D ]}|                    dg           }d|v r1|                    dd          }|rd|                    d	          d
<   |                    d          sdg |d|d<   | j        u|                     d	          }|                    |j                   |                    |           |                    d          }|r|	                    d|           |                     d          }|                     dddd          }|                    |                     dddi                     |                    |           |                    d          }|r|	                    d|           |	                    d|           d|v rd
|d
<   dS )z1Add checkboxes to collapse children in a toctree.r   Tr   r   r   r   )class_Nr   r   r   zhas-childrenzdetails > a.referencer   summaryspanztoctree-togglepresentation)r   roler   izfa-solid fa-chevron-downzdetails > p.caption)
r   rC   find_parentr   r   r   contentsrf   
select_oneinsert)	r   elementclassesparentlir   toc_linkr   r   collapsible_section_headings	            r   r   r     s    ===66 d% d%++gr** **4*EEH 837i((0 ||D!! 	 6W5n5<Z ,,y))w'(((w %%&=>> 	(NN1h''' ,,y))||) '   	
 	
 	DLLW6P,QLRRSSSt '.&8&89N&O&O#& 	; NN19::: 	q'""" $GFOId% d%r   rF   c                    |                     dd           d|vs|d         sd|d<   t          |d                   |d<   |j        j        |                                         }g }t          |t                    D ]/} |j        d|| j        |d|}|r|	                    |           0|sdS |d         }	|dd         D ]}|	
                    |j                   |	S )	a\  Get the partial TocTree (rooted at `ancestorname`) that dominates `pagename`.

    Parameters:
    app : Sphinx app.
    pagename : Name of the current page (as Sphinx knows it; i.e., its relative path
    from the documentation root).
    ancestorname : Name of a page that dominates `pagename` and that will serve as the
    root of the TocTree fragment.
    toctree : A Sphinx TocTree object. Since this is always needed when finding the
    ancestorname (see _get_ancestor_pagename), it's more efficient to pass it here to
    re-use it.
    kwargs : passed to the Sphinx `toctree` template function.

    This is similar to `context["toctree"](**kwargs)` (AKA `toctree(**kwargs)` within a
    Jinja template), or `TocTree.get_toctree_for()`, which always uses the "root"
    doctree (i.e., `doctree = self.env.get_doctree(self.env.config.root_doc)`).
    collapseTmaxdepthr   )docnamer   r   Nr   r=   )
setdefaultr}   r&   r\   deepcopyr   r_   resolver   rf   r   rb   )
r   r   rF   r   rE   ancestor_doctreetoctreestoctree_noderesolved_toctreeresults
             r   rD   rD   T  s-   ( j$'''vj'9zVJ/00F:{'5>>@@H ,,<>NOO . .
 +7? 
K 
 
 	
 
  	.OO,--- ta[F$QRRL 1 1&/0000Mr   )*r9   dataclassesr   	functoolsr   	itertoolsr   textwrapr   typingr   r	   r
   r   urllib.parser   r'   bs4r   docutilsr   docutils.nodesr   sphinx.addnodesr   r_   sphinx.applicationr   r)   r   sphinx.localer   utilsr   r<   r   r}   r/   r1   r   r   rD   r=   r   r   <module>r      sD   : : ! ! ! ! ! !                   / / / / / / / / / / / / ! ! ! ! ! !                    7 7 7 7 7 7 % % % % % % 7 7 7 7 7 7       & & & & & &$ 3     # 3 3    .        `7	`7`7.1`7	`7 `7 `7 `7Fh%- h%D h% h% h% h%V1	11.11 1 1 1 1 1r   