JSON 使用哪种 Sphinx 代码块语言

发布于 2024-10-04 04:37:29 字数 248 浏览 11 评论 0原文

我正在使用 Sphinx 来记录 Web 服务。我想使用代码块指令显示格式化的 JSON Web 响应,Spinx 通过 Pygments 执行此操作,但 JSON 在 Pygments 中没有语法突出显示。您建议我指定什么语言? HTML? JavaScript?

.. code-block:: javascript

    {
      "name": "roger",
      "score": 100
    }

I am using Sphinx to document a web service. I would like to show a formatted JSON web response using the code-block directive, which Spinx does via Pygments, but JSON doesn't have a syntax highlighter in Pygments. What language do you suggest I specify instead? HTML? JavaScript?

.. code-block:: javascript

    {
      "name": "roger",
      "score": 100
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

念三年u 2024-10-11 04:37:29

我正在使用 Sphinx 1.4.2,其中包含一个名为“json”的 Pygments 词法分析器。默认情况下,这是开箱即用的。使用方法:

.. code-block:: json

    {
        "key": "value",
        "key2": "value2",
        ...
    }

I am using Sphinx 1.4.2 which includes a Pygments lexer called "json". This is available out-of-the-box by default. To use:

.. code-block:: json

    {
        "key": "value",
        "key2": "value2",
        ...
    }
清泪尽 2024-10-11 04:37:29

我不喜欢使用 pygments javascript 来解析 JSON。是的,JSON 可以由 javascript 词法分析器解析,但 javascript 突出显示在应用于 JSON 值时并不是很有用。您通常会得到一大块未区分的文本。

由于我找不到好的解决方案,我创建了一个 用于 pygments 的 JSON 词法分析器。我现在使用它来突出显示 sphinx 创建的 PDF 文档中的 JSON。它并不完美,但结果比 javascript 词法分析器有用得多。我希望它有帮助。

I wasn't happy using pygments javascript for parsing JSON. Yes, JSON can be parsed by the javascript lexer, but javascript highlighting is not very useful when applied to a JSON value. You typically get a giant blob of undifferentiated text.

Since I couldn't find a good solution, I created a JSON lexer for pygments. I'm using it now for highlighting JSON in sphinx created PDF document. It's not perfect, but the results are much more useful than with the javascript lexer. I hope it helps.

葮薆情 2024-10-11 04:37:29

即使使用 Sphinx 1.2b1 和 Pygments 1.6,我也需要调用 add_lexer 来获取 .. code-block:: json 来执行任何操作。我最终将以下代码片段放入扩展中(docs/_ext/jsonlexer.py):

def setup(app):
    # enable Pygments json lexer
    try:
        import pygments
        if pygments.__version__ >= '1.5':
            # use JSON lexer included in recent versions of Pygments
            from pygments.lexers import JsonLexer
        else:
            # use JSON lexer from pygments-json if installed
            from pygson.json_lexer import JSONLexer as JsonLexer
    except ImportError:
        pass  # not fatal if we have old (or no) Pygments and no pygments-json
    else:
        app.add_lexer('json', JsonLexer())

我的 Sphinx docs/conf.py 具有以下内容来启用扩展:

import os
import sys

sys.path.insert(0, os.path.abspath('_ext'))

# Add any Sphinx extension module names here, as strings. They can be
# extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['jsonlexer']

Even with Sphinx 1.2b1 and Pygments 1.6, I needed to call add_lexer to get .. code-block:: json to do anything. I ended up putting the following code fragment into an extension (docs/_ext/jsonlexer.py):

def setup(app):
    # enable Pygments json lexer
    try:
        import pygments
        if pygments.__version__ >= '1.5':
            # use JSON lexer included in recent versions of Pygments
            from pygments.lexers import JsonLexer
        else:
            # use JSON lexer from pygments-json if installed
            from pygson.json_lexer import JSONLexer as JsonLexer
    except ImportError:
        pass  # not fatal if we have old (or no) Pygments and no pygments-json
    else:
        app.add_lexer('json', JsonLexer())

My docs/conf.py for Sphinx has the following to enable the extension:

import os
import sys

sys.path.insert(0, os.path.abspath('_ext'))

# Add any Sphinx extension module names here, as strings. They can be
# extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['jsonlexer']
落墨 2024-10-11 04:37:29

JSON 是 JavaScript,简单明了。 JSON 实际上代表“JavaScript 对象表示法”。

JSON is JavaScript, plain and simple. JSON in fact stands for "JavaScript Object Notation".

盛夏尉蓝 2024-10-11 04:37:29

Sphinx 默认提供了几个专用于 JSON 突出显示的 pygment 词法分析器,因此您可以在此处选择一个:

http://pygments.org/docs/lexers/#lexers-for-data-file-format

  • pygments.lexers.data.JsonLexer (pygment v1.5+)

    对于 JSON 数据结构。

    .. 代码块:: json
    
  • pygments.lexers.data.JsonLdLexer(pygment v2.0+)

    对于 JSON-LD 链接数据。

    .. 代码块:: json-ld
    
  • pygments.lexers.data.JsonBareObjectLexer(pygment v2.2+)

    对于 JSON 数据结构(缺少对象大括号)。

    .. 代码块:: json-object
    

There is several pygment lexers dedicated to JSON highlighting provided by default with Sphinx, so you can choose one here:

http://pygments.org/docs/lexers/#lexers-for-data-file-format

  • pygments.lexers.data.JsonLexer (pygment v1.5+)

    For JSON data structures.

    .. code-block:: json
    
  • pygments.lexers.data.JsonLdLexer (pygment v2.0+)

    For JSON-LD linked data.

    .. code-block:: json-ld
    
  • pygments.lexers.data.JsonBareObjectLexer (pygment v2.2+)

    For JSON data structures (with missing object curly braces).

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