模仿狮身人面像的侧边栏toctree

时间:2015-07-31 13:59:18

标签: jinja2 python-sphinx

我正在尝试制作一个新的Sphinx模板,它会为侧边栏创建一个自定义的toctree。

使用Jinja模板语言,似乎只有一个函数可用:toctree()一次显示所有toctree但我需要遍历各个toctree项。

这看起来像那样:

{% for element in toctree_elements %}
    {{ ... display the stuff as wanted }}
{% endfor %}

有可能吗?

2 个答案:

答案 0 :(得分:1)

我终于找到了一个技巧,但这并不令人满意。

这将从函数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版本中发生变化。此外,它不接受网址或toctree条目中的字符%;

答案 1 :(得分:0)

fulltoc.py中的

sphinx-contrib/fulltoc通过在运行context['toc']事件时运行html_page_context来修改html-page-context

  

Sphinx的扩展名,使边栏显示完整的目录表,而不仅仅是本地标题

相关问题