如何在sphinx doc中显示和隐藏代码?

发布于 2024-12-16 16:56:37 字数 170 浏览 5 评论 0原文

我希望在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离去的眼神 2024-12-23 16:56:37

SQLAlchemy 文档

SQLAlchemy 文档使用特殊扩展 (https://bitbucket.org/zzzeek/zzzeeksphinx/)。

文档源包含带有自定义选项和标记的标记,如本例所示:

.. sourcecode:: python+sql
 
    {sql}>>> engine.execute("select 1").scalar()
    select 1
    ()
    {stop}1

这是由特殊的 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:

.. sourcecode:: python+sql
 
    {sql}>>> engine.execute("select 1").scalar()
    select 1
    ()
    {stop}1

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文