如何在Sphinx中展开侧边栏的所有子章节?
我在想有没有办法让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
比如说,当我点击第一节的时候,它会显示下面的内容,但如果我点击第二节,第一节的内容就会隐藏,只显示第二节。我希望每次在索引页面的时候,所有的节都能展开。我试过添加toctree
和maxdepth
,但都没有效果。
4 个回答
3
很不幸,这里有一个未解决的问题:https://github.com/readthedocs/sphinx_rtd_theme/issues/455
3
如果你在使用sphinx_rtd_theme这个主题,你可以通过修改文件layout.html中的'toctree maxdepth'值,来改变网页侧边栏菜单的最大深度。这个文件通常在source/_themes/sphinx_rtd_theme
这个文件夹里。这里有几种解决办法:
最简单、最快的方法是:在侧边栏显示更深层的目录树
如果你使用的是旧版本的主题,你可以在一行代码中设置一个新的'maxdepth'值(比如3),这行代码是:
{% set toctree = toctree(maxdepth=3, collapse=False, includehidden=True) %}
如果你使用的是最新版本的主题,文件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) %}
在这种情况下,你可以在theme.conf中定义'theme_navigation_depth':
[options] theme_navigation_depth = 3
修改完成后记得重新编译... 还有,别忘了享受阳光哦!
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']}
- 重新生成文档。