我正在尝试制作一个新的Sphinx模板,它将为侧边栏创建一个自定义的toctree。
使用Jinja模板语言,似乎只有一个函数可用:toctree()
,它一次显示所有toctree,但我需要遍历单个toctree项。
这看起来像这样:
{% for element in toctree_elements %}
{{ ... display the stuff as wanted }}
{% endfor %}
有可能吗?
发布于 2015-07-31 15:50:28
我终于找到了一个窍门,但它不是很令人满意。
这将从函数toctree()
中获取html toctree,并删除所有不需要的html标记。只保留URL和标题,并创建一个数组。
{% set theTocTree = toctree()
| replace("</a>", "")
| replace(" href=\"", "></a>")
| replace("</li>", "</li>;")
| striptags
| replace("\">", "%") %}
{% set theTocTree = theTocTree.split(";") %}
然后,下面的循环遍历新的toctree数组,以执行所需的任何操作。
{% for element in theTocTree %}
{% set el = element.split("%") %}
{% set url = el[0] | trim | safe %}
{% set entry = el[1] | trim | safe %}
... here, you can use variables url and entry ...
{% endfor %}
这个解决方案是不干净的,因为它依赖于toctree html呈现,这可能会在未来的Sphinx版本中发生变化。此外,它不接受URL或目录树条目中的字符%
和;
。
发布于 2019-08-26 10:29:32
sphinx-contrib/fulltoc中的fulltoc.py通过在html-page-context
事件运行时运行html_page_context
来修改html-page-context
。
Sphinx的
扩展,使侧边栏显示完整的目录,而不仅仅是局部标题
https://stackoverflow.com/questions/31748047
复制相似问题