如何在sphinx doc中显示和隐藏代码?
我希望在 Sphinx、reStructuredText 显示/隐藏代码片段中提及相同的功能。但该帖子上没有共享代码。
I want the same functionality mention in Sphinx, reStructuredText show/hide code snippets. But there is no code shared on that post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SQLAlchemy 文档
SQLAlchemy 文档使用特殊扩展 (https://bitbucket.org/zzzeek/zzzeeksphinx/)。
文档源包含带有自定义选项和标记的标记,如本例所示:
这是由特殊的 Pygments lexer (
PythonWithSQLLexer
) 和 Pygments 格式化程序 (PopupSQLFormatter
),以便生成带有代码片段和显示/隐藏它的链接的“弹出”。源代码位于 sqlformatter.py 中。
显示/隐藏切换由 init.js。
Mako 模板系统用于生成 HTML 页面(Sphinx 中的默认模板系统是 Jinja)。这是通过在 mako 中子类化
TemplateBridge
来配置的。 py。在模板文件 layout.mako 中,指定 CSS 和 JavaScript 文件(包括 init.js)的路径。有关 Sphinx 模板的更多信息,请参阅 http://sphinx-doc.org/templatating.html 。
Python 文档
Python 文档中的许多代码示例都具有一个“按钮”,用于切换交互式提示的显示(
>>>
、...
)和 doctest 中的输出-风格的代码片段。有了提示和输出,就可以更轻松地复制和粘贴代码。以下是几个示例: http://docs.python.org/library/datetime .html#module-datetime。该功能在 copybutton.js 中实现。
SQLAlchemy docs
The SQLAlchemy documentation uses a special extension (https://bitbucket.org/zzzeek/zzzeeksphinx/).
The documentation source contains markup with custom options and tokens, like in this example:
This is processed by a special Pygments lexer (
PythonWithSQLLexer
) and Pygments formatter (PopupSQLFormatter
) in order to generate a "popup"<div>
with the code snippet and the link that shows/hides it. The source code is in sqlformatter.py.The show/hide toggling is handled by jQuery magic in init.js.
The Mako templating system is used to generate the HTML pages (the default templating system in Sphinx is Jinja). This is configured by subclassing
TemplateBridge
in mako.py.In the template file layout.mako, the paths to CSS and JavaScript files (including init.js) are specified. For more on Sphinx templating, see http://sphinx-doc.org/templating.html.
Python docs
Many code examples in the Python documentation feature a "button" that toggles display of interactive prompts (
>>>
,...
) and output in doctest-style code snippets. With prompts and output out of the way, the code can be more easily copied and pasted. Here are several examples: http://docs.python.org/library/datetime.html#module-datetime.The feature is implemented in copybutton.js.