更一致的 Sphinx 主题
More consistent Sphinx themes
考虑以下函数
# 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 从我的文档字符串生成代码,它看起来像这样(这次我使用默认主题 haiku
,rst
或 conf.py
文件):
是否有任何主题或任何其他解决方法可以解决我用颜色指出的问题?
我尝试了不同的方法,none 成功了,但我无法得到 1.-4 中的任何一点。上班。
EDIT 我的想法是 不想 编辑 HTML,这太麻烦了。我只想更改我的 Sphinx docs
目录中的 conf.py
和 index.rst
文件,以便进行上述更改。
CSS 单靠定制无法解决这个问题。即使您可以使用 ::before
和 ::after
选择器之后,我认为 JavaScript 更适合处理这个问题。
您可以使用 conf.py 中的 html_js_files 选项添加自定义 Javascript 文件.您的要求 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
我的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 之后
关于可选要求4
Sphinx 使用 pygments for synatx highlighting. If you are not satisfied with this, you can use Highlightjs, Prismjs, Google code prettify 等,通过 html_js_files
下载并包含它们。但这些也是 syntax 荧光笔。突出显示变量的语义荧光笔可用于 python,但我还没有听说任何可以与 Sphinx 一起使用的东西。
注意:
- 我正在使用 Alabaster theme。但是这个想法应该适用于大多数主题。但是,您必须根据您的主题编写 custom.js 文件。
- 我的custom.js文件非常简单,没有考虑到所有的可能性。
考虑以下函数
# 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 从我的文档字符串生成代码,它看起来像这样(这次我使用默认主题 haiku
,rst
或 conf.py
文件):
是否有任何主题或任何其他解决方法可以解决我用颜色指出的问题?
我尝试了不同的方法,none 成功了,但我无法得到 1.-4 中的任何一点。上班。
EDIT 我的想法是 不想 编辑 HTML,这太麻烦了。我只想更改我的 Sphinx docs
目录中的 conf.py
和 index.rst
文件,以便进行上述更改。
CSS 单靠定制无法解决这个问题。即使您可以使用 ::before
和 ::after
选择器之后,我认为 JavaScript 更适合处理这个问题。
您可以使用 conf.py 中的 html_js_files 选项添加自定义 Javascript 文件.您的要求 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
我的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 之后
关于可选要求4
Sphinx 使用 pygments for synatx highlighting. If you are not satisfied with this, you can use Highlightjs, Prismjs, Google code prettify 等,通过 html_js_files
下载并包含它们。但这些也是 syntax 荧光笔。突出显示变量的语义荧光笔可用于 python,但我还没有听说任何可以与 Sphinx 一起使用的东西。
注意:
- 我正在使用 Alabaster theme。但是这个想法应该适用于大多数主题。但是,您必须根据您的主题编写 custom.js 文件。
- 我的custom.js文件非常简单,没有考虑到所有的可能性。