我有一个桌面应用程序和一个用Sphinx编写的用户指南。通过使用与literalinclude
块一起导入的代码块来解释API
我希望在每个代码块内部或附近有一个按钮。该按钮用于在我们的应用程序中打开相应的示例
到目前为止,我所取得的(见下文)是代码块上方的一个按钮,每次使用literalinclude
语句时都必须手动包含该按钮
如何将按钮放在代码块内部而不是上面?
一个特别漂亮的结果是像Chris Holdgraf's Sphinx copybutton这样的透明按钮。但是现在我已经很高兴拥有这个按钮了,因为它位于代码块的右上角
目前情况的最简单示例
按钮呈现在代码块上方。但是我想把它放在里面(在右上角)
文件夹结构:
conf.py
index.rst
source_file.py
load_source.py
index.rst的内容:
:load_example:`source_file.py`
.. literalinclude:: source_file.py
source_file.py的内容:
import numpy as np
print "hello extension"
for i in range(3):
print np.sin(i)
load_souce.py扩展文件的内容:
from docutils import nodes
# HTML code to generate button
button_raw = """
<script>
function openScriptFunction() {
callBackHandler.openScript("%s");
}
</script>
<button onclick="openScriptFunction()">Load %s</button>
"""
def setup(app):
app.add_role('load_example', load_example_script)
def load_example_script(name, rawtext, text, lineno, inliner, options={}, content=[]):
node = nodes.raw(rawsource = button_raw%(text, text), text = button_raw%(text, text), format="html")
return [node], []
这个扩展被extensions = ['load_source']
包含在conf.py中
目前没有回答
相关问题 更多 >
编程相关推荐