sphinx-rtd-theme 中自定义页脚的问题

Trouble with custom footer in sphinx-rtd-theme

我在向我的 Sphinx .html 文件添加自定义页脚时遇到了一些问题。我正在使用 sphinx_rtd_theme。我检查了这个 post 并尝试了它(以及评论中的一些建议)但无济于事。我不确定我错过了什么。抱歉,如果我没有在此处发布足够的内容来实际指出导致问题的原因。感谢任何帮助或建议!

我的 css 主题文件已被我自己修改(很差)(我不是 HTML/CSS 人!)但我认为这无关紧要?我唯一能想到的另一件事是,当我重新编译输出文件时,我可能必须做一些特别的事情。我只是使用:

make clean html && make html

我的 conf.py 位于:root/source/conf.py。这是我的 conf.py 文件的一些摘录:

import sphinx_rtd_theme

project = 'Project Name'
copyright = '2021, My Company'
author = 'My Name, Coworker Name'
master_doc = 'Home'
extensions = ["sphinx_rtd_theme", "sphinx.ext.todo"]
todo_include_todos = True
templates_path = ['_templates']
source_suffix = ['.rst']
html4_writer = True
html_theme = 'sphinx_rtd_theme'
# html_theme_path = ['_static']
html_static_path = ['_static']
# html_extra_path = []
html_show_sphinx = True
html_show_copyright = True
html_style = 'css/my_theme.css' 

这是我已覆盖的 layout.html 文件。它位于评论中显示的路径中。

 <!-- layout.html
 * Place this file in root/source/_templates
 * -->
{% extends "!layout.html" %}
{% block extrahead %}
    {{super}}
    <link href="{{ pathto("_static/my_theme.css", True) }}" rel="stylesheet" type="text/css">
{% endblock %}

{% block extrafooter %}
    {{super}}
    <div class="footer">
        My custom footer just needs to add a single sentance to the existing footer.
    </div>
{% endblock %}

所以我找到了解决方法。

1。复制现有 RTD html 个文件

我从我的虚拟环境文件夹中复制了现有的 RTD .html 文件。在我的系统上,它位于通常的位置:

.../Miniconda3/envs/my_env_name/Lib/site-packages/sphinx_rtd_theme/

我找到了以下文件:

  • breadcrumbs.html
  • footer.html
  • Layout.html
  • search.html
  • searchbox.html
  • theme.conf
  • versions.html

我将这些复制到我的项目工作目录中:

.../Documentation-repo/Sphinx/root/source/_templates/

2。编辑工作目录

中的conf.py文件

我打开了 conf.py 文件并更改了以下内容:

# Add any paths that contain templates here, relative to this directory.
#   Uncomment the line below to enable customized template #
#
# templates_path = ['_templates']

对此:

# Add any paths that contain templates here, relative to this directory.
#   Uncomment the line below to enable customized template #
#
templates_path = ['_templates']

3。将新内容添加到 footer.html 文件

我打开了footer.html并编辑了它,在底部添加了我想要的内容。在我的例子中,它就像在 {%- block extrafooter %} {% endblock %} 行下面添加我的单个更改语句一样简单。简单。可能不是完美的解决方案,但它可以满足我的需要。

您要添加自定义页脚还是替换默认页脚?在我的例子中,我只需要覆盖 footer.html 文件而不是 layout.html.

这是我 100% 为我的 Sphinx 文档所做的工作:

  1. 在您的 Sphinx 项目的 _template 文件夹中创建一个 footer.html
  2. 然后添加:
{% extends "!footer.html" %}
{%- block contentinfo %}
{{ super }}
<!-- your custom footer here-->
{% endblock %}

注意您的页脚实际包含在哪个块中。在我的例子中,它在 contentinfo

里面