如何在Sphinx-doc中加载外部JavaScript [英] How to load external javascript in sphinx-doc

查看:51
本文介绍了如何在Sphinx-doc中加载外部JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 sphinx-doc ,我注意到基本主题layout.html script

I'm extending the basic theme from sphinx-doc and I notice the following code chunk inside basic theme layout.html script macro

{%- for scriptfile in script_files %}
<script type="text/javascript" src="{{ pathto(scriptfile, 1) }}"></script>
{%- endfor %}

这是否意味着我可以在theme.conf中添加如下所示的html_theme_options:

Does that mean I can add a html_theme_options like the following inside my theme.conf:

[options]
script_files = 

,然后在我的conf.py中添加:

html_theme_options = {'script_files': '_static'}

但是,使用此设置,构建完全被搞砸了,并产生了如下垃圾页面:

However, with this set, the build is totally messed up and produced junk pages like:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>...</head>
  <body>
     -->
   <!-- my js code but is automatically commented out-->
  </body>
</html>

哪个部分出了问题?我应该怎么做才能加载自己的自定义javascript?非常感谢!

Which part goes wrong? What should I do in order to load my own customized javascript? Thanks much!

推荐答案

script_files是模板内部变量.您无法通过html_theme_options进行设置(所有主题变量均以theme_作为前缀,请参见下文).

script_files is a template-internal variable. You cannot set it via html_theme_options (all theme variables have theme_as a prefix, see below).

Sphinx文档在此处中说明了如何添加其他脚本通过script_files变量直接在模板文件中.

The Sphinx docs explain here how to add additional scripts directly in the template files, via the script_files variable.

如果您认为在conf.py中定义其他脚本很重要,请按照以下步骤操作:

In case you regard it as important to define the additional scripts in your conf.py, proceed as follows:

  1. 将以下行添加到模板的layout.html中,例如,在DOCTYPE定义的endblock下面:

  1. Add the following line to your template's layout.html, for example below the endblock of the DOCTYPE definition:

{% set script_files = script_files + theme_extra_scripts %}

theme.conf中定义主题变量extra_scripts及其默认值:

Define the theme variable extra_scripts and its default value in theme.conf:

extra_scripts = []

覆盖conf.py中的变量:

html_theme_options = {
   'extra_scripts': ['_static/test.js']
}

这篇关于如何在Sphinx-doc中加载外部JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆