【问题标题】:Trouble with custom footer in sphinx-rtd-themesphinx-rtd-theme 中的自定义页脚问题
【发布时间】:2021-04-02 12:46:13
【问题描述】:

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

我的 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 %}

【问题讨论】:

  • @LexLi 我没有构建一个全新的模板...我正在尝试向现有模板的现有部分添加一行。
  • 即使不是完整的主题,您也需要具备相同水平的 HTML/JavaScript/CSS 专业知识才能自定义几行代码。
  • sphinx_rtd_theme 是一个独立于 Sphinx 及其默认主题的包,因此该技术需要修改。它的configuration options 开箱即用是有限的。您可以分叉包并修改主题模板。我不知道有什么其他方法可以扩展它的模板,但是有一个open issue

标签: html css themes python-sphinx


【解决方案1】:

您要添加自定义页脚还是替换默认页脚?就我而言,我只需要覆盖 footer.html 文件而不是 layout.html。

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

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

请注意您的页脚实际包含在哪个块中。就我而言,它在 contentinfo

【讨论】:

  • 是的,这也是我所做的。我的解决方案见下文。当我试图将我的内容添加到 footer.html 时,花括号最初让我失望,但如果我没记错的话,反复试验最终解决了它。已经有一段时间。您是否知道任何其他方法可以做到这一点,或者修改 footer.html 文件是添加自定义页脚内容的唯一方法?
  • 是的,我认为您也可以使用 (% extrafooter %} 块。如果您想添加新内容同时仍保留默认页脚中的原始内容,也许不要省略 {{超级 }} 语句?
【解决方案2】:

所以我找到了解决方法。

1。复制现有的 RTD html 文件

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

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

我找到了以下文件:

  • 面包屑.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 %} 行下方添加我的单句更改一样简单。简单。可能不是完美的解决方案,但它可以满足我的需要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2011-07-13
    • 2011-01-18
    • 2013-03-26
    相关资源
    最近更新 更多