如何在Sphinx中展开侧边栏TOC树中的所有子部分?

40
我在想是否有一种方法可以展开在index.rst文件中包含的标题下的所有子部分?
例如,这是它的显示方式:
Section 1
Section 2
Section 3

以下是我希望它成为的样子:

Section 1
  Subsection 1.1
  Subsection 1.2
  Subsection 1.3
Section 2
  Subsection 2.1
  Subsection 2.2
  Subsection 2.3
Section 3
  Subsection 3.1
  Subsection 3.2
  Subsection 3.3
如果我点击"Section 1",会显示它下面的内容,但是如果我点击"Section 2",那么"Section 1"的内容就会消失,只有"Section 2"的内容会出现。 我希望每次在索引页面时都能展开这两个部分。 我已经尝试过添加toctreemaxdepth,但没有用。

5
你能把你的 index.rst 文件的内容添加到问题中吗? - ddbeck
如果您正在使用ReadTheDocs主题,您可能想查看https://dev59.com/XF4c5IYBdhLWcg3wqb0C - erb
4个回答

13

好吧,我尝试阅读sphinx源代码时失去了约340万个神经元(它是由一群疯狂的浣熊编写的吗?!有太多抽象层次)。

所以:

  • 制作您自己的sphinx主题(使用第三方主题作为基础非常容易。我使用'readable'主题)
  • 在拥有theme.conf的目录中,添加一个“fulltoc.html”模板,其中包含一行:

fulltoc.html:

{{ toctree(collapse=False) }}

(嘿,注意“collapse”参数了吗?)

  • 在Sphinx的conf.py中,修改html_sidebars选项以添加您的模板;并声明您的主题

conf.py:

html_theme_path = [customized_readable_theme.get_html_theme_path()]
html_theme = 'customized_readable'
html_sidebars = {'**': ['fulltoc.html', 'relations.html', 'sourcelink.html', 'searchbox.html']}
  • 重建文档

3
如果你正在使用sphinx_rtd_theme主题,你可以通过修改layout.html文件中定义的'toctree maxdepth'值来改变html页面中侧边栏菜单的最大深度。该文件通常位于目录source/_themes/sphinx_rtd_theme中。有几种解决方案:
  • The simplest, fastest solution: Show deeper toctree in sidebar

  • You are using an old version of the theme. Then you can set a new 'maxdepth' value (e.g., 3) in the line that says:

    {% set toctree = toctree(maxdepth=3, collapse=False, includehidden=True) %}
    
  • You are using a newest version of the theme. Then you may have these lines in the file layout.html:

    {% set global_toc = toctree(maxdepth=theme_navigation_depth|int,
                                collapse=theme_collapse_navigation|tobool,
                                includehidden=theme_includehidden|tobool,
                                titles_only=theme_titles_only|tobool) %}
    

    In this case, you may define 'theme_navigation_depth' in theme.conf:

    [options]
    theme_navigation_depth = 3
    
重新编译修改后的代码... 别忘了享受阳光!最初的回答。

3

0

以下是如何为移动端准备的"sphinx_rtd_theme"主题完成此操作。

将以下[Vanilla] JS添加到您的自定义JavaScript中:

function toctreeExpand ()
{var toctree=document.querySelector('button.toctree-expand');

 toctree.focus ();
 toctree.click ();
} // function toctreeExpand ()                                                                

function deferToctreeExpand ()
{// flush any pending housekeeping events first...                                            
 setTimeout (toctreeExpand);
} // function deferToctreeExpand ()                                                           

function init ()
{var anchors=document.querySelectorAll('a.reference.internal');

 toctreeExpand ();
 for (var anchor of anchors)
     anchor.addEventListener ('click', deferToctreeExpand);
} // function init ()                                                                         

window.addEventListener ('load', init);

它只展开顶级条目 - 但这比默认情况下完全折叠树形结构作为单个条目要好得多!这可以通过展开您刚刚单击的嵌套分支(可能向下三个级别...)来改进。但是我的收件箱已经满了;所以对我来说,上面的修复方法现在是可接受的...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接