How to add Sphinx-generated index to the sidebar when using Read the Docs theme?

≡放荡痞女 提交于 2019-12-21 07:04:08

问题


I'd like to have a link to the automatically generated index in the sidebar when using sphinx-rtd-theme. I've tried adding it to the toctree:

.. toctree::

    first
    second
    Index <:ref:`genindex`>

but this resulted in

WARNING: toctree contains reference to nonexisting document u':ref:`geinindex`'

from Sphinx and no other effect.

I think I could simply hardcode the index in the theme layout.html file, but perhaps there is some better way, not involving modifying the standard theme?

TIA for any hints!


回答1:


It's easy if you understand how Sphinx and Jinja work. Unfortunately the Sphinx docs on templating don't give you enough info if you don't. In short, you'll have to override the template:

  • Make sure you have a _templates folder under your sphinx docs folder.
  • Make sure it is listed in your conf.py, e.g. templates_path = ['_templates']
  • Create a file inside the folder called layout.html.
  • Put this snippet inside and save. The exclamation point/mark forces jinja to use the parent template. Don't forget it, or you'll get a recursion error. You only need to override the menu block.

    {% extends "!layout.html" %}
    
      {% block menu %}
        {{ super() }}
        <a href="genindex.html">Index</a>
      {% endblock %}
    



回答2:


How about:

.. toctree::

    first
    second

* :ref:`genindex`


来源:https://stackoverflow.com/questions/25243482/how-to-add-sphinx-generated-index-to-the-sidebar-when-using-read-the-docs-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!