l7l*_*ll7 6 python themes docstring python-sphinx
考虑以下功能
# 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
Run Code Online (Sandbox Code Playgroud)
如果我使用Sphinx从我的文档字符串生成代码,则看起来像这样(这次我使用的是默认主题haiku,在rstor conf.py文件中没有太多更改):
是否有任何主题或其他变通办法可以解决我用颜色指示的问题?
我尝试了不同的方法,但均无效果,但我无法获得1.-4的任何要点。去工作。
编辑的想法是,我不想要编辑的HTML,这是方式繁琐。我只想对Sphinx 目录中的conf.py和index.rst文件进行更改docs,以进行上述更改。
仅靠 CSS 定制无法解决这个问题。尽管您可以使用::before和 after::after选择器,但我认为 JavaScript 更适合处理这个问题。
您可以使用添加自定义JavaScript文件html_js_files在选项conf.py。使用它应该可以满足您的要求 1、2 和 3。
# ...
html_js_files = ['custom.js']
# ...
Run Code Online (Sandbox Code Playgroud)
如果您在conf.py中使用上面的代码,那么您的custom.js文件应位于如下位置
docs/
_static/
custom.css
custom.js
_templates/
index.rst
api.rst
...
Run Code Online (Sandbox Code Playgroud)
一个例子
添加custom.js之前
我的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>'
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
再次构建 html 后
关于可选要求 4
Sphinx 使用pygments来突出显示 Synatx。如果您对此不满意,您可以使用Highlightjs、Prismjs、Google 代码美化等html_js_files。但这些也是语法高亮。突出变量的语义荧光笔可用于python,但我还没有听说过任何可以与Sphinx一起使用的东西。
注意: