我一直在使用Sphinx和reStructuredText记录软件包的文档。
我的文档中有一些很长的代码片段。我希望它们默认情况下是隐藏的,而有一个小的“显示/隐藏”按钮可以展开它们(Example)。
有没有一种标准的方法来实现这个?
我一直在使用Sphinx和reStructuredText记录软件包的文档。
我的文档中有一些很长的代码片段。我希望它们默认情况下是隐藏的,而有一个小的“显示/隐藏”按钮可以展开它们(Example)。
有没有一种标准的方法来实现这个?
container
,它允许你为块添加自定义CSS类,并覆盖现有主题以添加一些JavaScript来添加显示/隐藏功能。_templates/page.html
:{% extends "!page.html" %}
{% block footer %}
<script type="text/javascript">
$(document).ready(function() {
$(".toggle > *").hide();
$(".toggle .header").show();
$(".toggle .header").click(function() {
$(this).parent().children().not(".header").toggle(400);
$(this).parent().children(".header").toggleClass("open");
})
});
</script>
{% endblock %}
这是 _static/custom.css
:
.toggle .header {
display: block;
clear: both;
}
.toggle .header:after {
content: " ▶";
}
.toggle .header.open:after {
content: " ▼";
}
将以下内容添加到conf.py
文件中:
This is added to conf.py
:
def setup(app):
app.add_css_file('custom.css')
现在您可以显示/隐藏代码块。
.. container:: toggle
.. container:: header
**Show/Hide Code**
.. code-block:: xml
:linenos:
from plone import api
...
我在这里使用非常类似的东西进行练习:https://training.plone.org/5/mastering-plone/about_mastering.html#exercises
您可以通过将代码包含在两个原始HTML指令中来使用内置的HTML可折叠details
标签。
.. raw:: html
<details>
<summary><a>big code</a></summary>
.. code-block:: python
lots_of_code = "this text block"
.. raw:: html
</details>
生成:
<details>
<summary><a>big code</a></summary>
<pre>lots_of_code = "this text block"</pre>
</details>
看起来自从这个问题被回答以来,一个新的sphinx扩展程序已经被制作出来了。
运行:pip install sphinx-togglebutton
添加到conf.py
extensions = [
...
'sphinx_togglebutton'
...
]
.. admonition:: Show/Hide
:class: dropdown
hidden message
既然上述方法都对我无效,下面是最终解决方案:
create a file substitutions.rst
in your source-directory with the following content:
.. |toggleStart| raw:: html
<details>
<summary><a> the title of the collapse-block </a></summary>
.. |toggleEnd| raw:: html
</details>
<br/>
add the following line at the beginning of every file you want to use add collapsible blocks
..include:: substitutions.rst
now, to make a part of the code collapsible simply use:
|toggleStart|
the text you want to collapse
..code-block:: python
x=1
|toggleEnd|
另一个选项是sphinx-design扩展中的下拉指令。从文档中可以看到:
pip install sphinx-design
conf.py
中的extensions
列表中extensions = ["sphinx_design"]
dropdown
指令:.. dropdown::
Dropdown content
.toggle .header {display: inline;}
。 - matusko.toggle .header:after
更改为.toggle .header p:after
,并将.toggle .header.open:after
更改为.toggle .header.open p:after
以避免此问题@dexteritas。 - Azrael3000