python - 更一致的 Sphinx 主题

标签 python themes python-sphinx docstring

考虑以下函数

# in mymodule.py:

def myfunc(num,mystring):
    """
    replicates a string

    :param num: a positive integer
    :param mystring: a string
    :return: string concatenated with itself num times

    :Example:
        >>> num = 3
        >>> mystring = "lol"
        >>> myfunc(num, mystring)
        "lollollol"
    """
    return num*mystring

如果我使用 Sphinx 从我的 docstring 生成代码,它看起来像这样(这次我使用默认主题 haikurstconf.py 文件没有太多变化):

enter image description here

是否有任何主题 - 或任何其他解决方法 - 可以解决我用颜色指示的问题?

我尝试了不同的方法,但都没有奏效,但我无法获得 1.-4 分中的任何一点。上类。

编辑 这个想法是我不想编辑HTML,这很麻烦。我只想更改 conf.pyindex.rst我的 Sphinx 中的文件 docs目录以进行上述更改。

最佳答案

仅靠 CSS 定制无法解决这个问题。即使您可以使用 ::before之后 ::after选择器,我认为 JavaScript 更适合处理这个问题。

您可以使用 html_js_files 添加自定义 Javascript 文件conf.py 中的选项。使用它应该可以满足您的要求 1、2 和 3。

#  ...

html_js_files = ['custom.js']

# ...

如果您在 conf.py 中使用上面的代码,那么您的 custom.js 文件应位于如下位置
docs/
  _static/
    custom.css
    custom.js
  _templates/
  index.rst
  api.rst
  ...


示例

添加 custom.js 之前

enter image description here

我的 custom.js 文件
window.onload = function () {
    // Change 'Parameters' to 'Arguments' 
    first_dt_elements = document.querySelectorAll('dl.field-list > dt:first-child');
    for(let i = 0; i< first_dt_elements.length; i++){
        first_dt_elements[i].innerText="Arguments";
    }
    // arguments same font and typeface etc..
    dl_methods = document.querySelectorAll('dl.function, dl.method');
    parameter_texts = []
    for(let i = 0; i< dl_methods.length; i++){
        params = [];
        param_elems=dl_methods[i].querySelectorAll('.sig-param');
        for (let j = 0; j< param_elems.length; j++){
            params.push(param_elems[j].innerText);
        }
        for( let k=0; k<params.length; k++){
            str = dl_methods[i].innerHTML;
            dl_methods[i].innerHTML= str.replace(
                RegExp(params[k], "g"), 
                '<span style="color:red;font-family:monospace;font-style:normal;">'
                + params[k] +
                '</span>'
            );
        }
    }
}

再次构建 html 后

enter image description here

关于可选要求 4

狮身人面像用途 pygments用于语法高亮。如果对此不满意,可以使用Highlightjs , Prismjs , Google code prettify等等,通过 html_js_files 下载并包含它们.但这些也是语法高亮。突出变量的语义荧光笔可用于python,但我还没有听说过任何可以与Sphinx一起使用的东西。

备注 :
  • 我正在使用 Alabaster theme .但是这个想法应该适用于大多数主题。但是,您必须根据您的主题编写 custom.js 文件。
  • 我的 custom.js 文件非常简单,没有考虑到所有的可能性。
  • 关于python - 更一致的 Sphinx 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58182598/

    相关文章:

    在 Visual Studio 2008 Designer 中工作的 WPF 皮肤库

    javascript - WordPress 子主题排队自定义 javascript

    python - Sphinx 文档常量替换

    python-sphinx - Sphinx 是否支持 MathML(通过 mathjax 扩展)?

    python - 如何将 doctests 与 unittest 的测试发现相结合?

    python - 寻找ctypes unicode处理的 "Hello World"(包括Python和C代码)

    Python peek 定义在 vscode 中有效,但 pylint 提示无法导入

    python - 我的 scrapy 无法得到有效响应

    css - 在 node.js 应用程序中切换 CSS 主题

    python - 如何为 Sphinx 自定义模块名称