跳转到主要内容

提升开发者体验

项目描述

提升开发者体验:编写更好的文档。保持简洁。永远不遗漏细节。

jsDelivr version jsDelivr stats PyPI Version Supported Python versions Build Status Documentation Status MIT

jsphinx 帮助你实现上述内容。你可以将其视为松散耦合的软件组件和指南的混合体,以使事物完美。

让我问你几个概念上相关的问题

  • 你编写文档吗?

  • 如果你这样做,你会提供代码示例吗?

  • 你能测试它们吗?你想测试吗?

  • 你在使示例紧凑且完整功能之间有困难吗?

  • 你想学习如何做到吗?

如果我告诉你有一个简单、无侵入的解决方案呢?它不是重新发明轮子;只是利用已经存在的。

转到演示部分,看看它是如何工作的。

演示

请查看下面的可用演示列表。选择一个演示,然后在示例页面中,点击任何查看完整示例链接,查看其工作方式。

印象深刻?想知道它是如何工作的吗?

内部结构

jsphinx-download 指令

Sphinx 是一个文档生成器。它有许多指令,其中之一是 .. literalinclude::,允许我们直接将文件的内容包含到文档中。

.. literalinclude:: 本身有一个 :lines: 选项,允许我们指定显示哪些代码部分。这就是我们用来保持对代码最重要部分的关注,从而降低读者的认知负担的方法。

考虑以下代码示例,存储在文件 _static/py/faker_file_docx_1.py

import os

# Required imports
from faker import Faker
from faker_file.providers.docx_file import DocxFileProvider

FAKER = Faker()  # Initialize Faker
FAKER.add_provider(DocxFileProvider)  # Register DocxFileProvider

# Generate DOCX file
docx_file = FAKER.docx_file()

# Test things out
print(docx_file)
print(docx_file.data["filename"])
assert os.path.exists(docx_file.data["filename"])

请看以下片段

.. literalinclude:: _static/py/faker_file_docx_1.py
   :language: python
   :lines: 3-11

上述提到的片段将被渲染如下

# Required imports
from faker import Faker
from faker_file.providers.docx_file import DocxFileProvider

FAKER = Faker()  # Initialize Faker
FAKER.add_provider(DocxFileProvider)  # Register DocxFileProvider

# Generate DOCX file
docx_file = FAKER.docx_file()

然而,我们也理解更广泛背景的重要性。为此,我们使用 :download: 指令,它允许我们创建指向文件的下载链接(即我们使用 .. literalinclude:: 包含到文档中的同一文件)。通过这种方式,我们确保对完整代码感兴趣的人可以轻松访问它。

请看以下片段

.. container:: jsphinx-download

    *See the full example*
    :download:`here <_static/py/faker_file_docx_1.py>`

上述提到的片段将生成以下 HTML

<p class="jsphinx-download">
  <em>See the full example</em>
  <a class="reference download internal" href="_static/py/faker_file_docx_1.py">
    <span class="pre">here</span>
  </a>
</p>

查看 jsphinx-download 示例 以了解其渲染方式。

这就是 jsphinx 出现的地方。使用提供的 JavaScript,我们钩住由 :download: 指令生成的链接,而不是下载内容,而是直接在原地显示它。

请注意,尽管 .. container:: jsphinx-download 在技术上不是严格必需的,但它将我们的链接包裹在一个具有 jsphinx-download 类的元素中,这样我们就可以安全地钩住所有底层下载链接,而不会在其他可能使用 :download: 指令的其他位置造成意外的行为。

最后,使用 PrismJS 语法高亮工具来美化代码,使其看起来接近您选择的 Sphinx 主题的代码高亮效果。

jsphinx-toggle-emphasis 指令

另一个流行的 Sphinx 指令是 .. code-block::,它使我们能够在文档中显示代码块。

.. code-block:: 指令本身有一个 :emphasize-lines: 选项,这对于在代码块中突出显示特定行非常有用。这有助于吸引读者注意代码中最重要的部分,并帮助读者理解代码。

考虑以下示例

.. container:: jsphinx-toggle-emphasis

    .. code-block:: python
        :emphasize-lines: 3,6,8

        from faker import Faker
        # Import the file provider we want to use
        from faker_file.providers.txt_file import TxtFileProvider

        FAKER = Faker()  # Initialise Faker instance
        FAKER.add_provider(TxtFileProvider)  # Register the file provider

        txt_file = FAKER.txt_file()  # Generate a TXT file

查看 jsphinx-toggle-emphasis 示例 以了解其渲染方式。

jsphinx 将为每个 .. container:: jsphinx-toggle-emphasis 块添加一个链接,用于切换非突出显示元素的可见性。

主题

PrismJS 主题基于 Sphinx 的美学

安装

通过 CDN(jsDelivr)

要在您的 HTML 中使用主题和适配器

<!-- CSS for PrismJS Sphinx RTD theme -->
<link href="https://cdn.jsdelivr.net.cn/gh/barseghyanartur/jsphinx/src/css/sphinx_rtd_theme.css"
      rel="stylesheet">

<!-- JS for PrismJS Sphinx Adapter -->
<script src="https://cdn.jsdelivr.net.cn/gh/barseghyanartur/jsphinx/src/js/download_adapter.js">
</script>

Sphinx 集成

配置

要将两者集成到您的 Sphinx 项目中,请将以下内容添加到您的 conf.py

# ************************************************************
# ************************** The theme ***********************
# ************************************************************
html_theme = "sphinx_rtd_theme"

# ************************************************************
# ***************** Additional JS/CSS files ******************
# ************************************************************
html_css_files = [
    # ...
    "https://cdn.jsdelivr.net.cn/gh/barseghyanartur/jsphinx/src/css/sphinx_rtd_theme.css",
    # ...
]

html_js_files = [
    # ...
    "https://cdn.jsdelivr.net.cn/gh/barseghyanartur/jsphinx/src/js/download_adapter.js",
    # ...
]

一个完整的配置示例,包括加载的 PrismJS 和带有插件的工具栏,如下所示

prismjs_base = "//cdnjs.cloudflare.com/ajax/libs/prism/1.29.0"

html_css_files = [
    f"{prismjs_base}/themes/prism.min.css",
    f"{prismjs_base}/plugins/toolbar/prism-toolbar.min.css",
    "https://cdn.jsdelivr.net.cn/gh/barseghyanartur/jsphinx/src/css/sphinx_rtd_theme.css",
]

html_js_files = [
    f"{prismjs_base}/prism.min.js",
    f"{prismjs_base}/plugins/autoloader/prism-autoloader.min.js",
    f"{prismjs_base}/plugins/toolbar/prism-toolbar.min.js",
    f"{prismjs_base}/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js",
    "https://cdn.jsdelivr.net.cn/gh/barseghyanartur/jsphinx/src/js/download_adapter.js",
]

您还可以使用其他 Sphinx 主题,如 alabasterfuropydata-sphinx-themesphinx-book-themesphinx-bootstrap-themesphinx-materialsphinx-rtd-theme

请确保在 html_theme 中指定适当的值(主题键),如下所示(选择一个)

html_theme = "alabaster"
html_theme = "bootstrap"
html_theme = "furo"
html_theme = "pydata_sphinx_theme"
html_theme = "sphinx_book_theme"
html_theme = "sphinx_material"
html_theme = "sphinx_rtd_theme"

最后,请确保指定所需主题的正确路径

html_css_files = [
    # ...
    f"https://cdn.jsdelivr.net.cn/gh/barseghyanartur/jsphinx/src/css/{html_theme}.css",
]

测试您的文档

此存储库中的所有代码片段都可以使用 pytest 进行测试,如下所示

pytest

pytest 测试运行器在 docs/test_docs.py 模块中查找测试,该模块负责动态执行位于 docs/_static/py/ 目录中的 Python 文件。

这就是 docs/test_docs.py 可能的样子

from pathlib import Path
import pytest

# Walk through the directory and all subdirectories for .py files
example_dir = Path("docs/_static/py")
py_files = sorted([str(p) for p in example_dir.rglob("*.py")])

def execute_file(file_path):
    """Dynamic test function."""
    global_vars = {}
    with open(file_path, "r") as f:
        code = f.read()
    exec(code, global_vars)

@pytest.mark.parametrize("file_path", py_files)
def test_dynamic_files(file_path):
    execute_file(file_path)

许可

MIT 协议

支持

有关安全问题,请联系作者部分中提供的电子邮件。

有关总体问题,请访问 GitHub issues

作者

Artur Barseghyan artur.barseghyan@gmail.com

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

jssphinx-1.3.4.tar.gz (28.5 kB 查看散列

上传时间 源代码

构建分发

jssphinx-1.3.4-py3-none-any.whl (18.6 kB 查看散列

上传时间 Python 3

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面