JSON 使用哪种 Sphinx 代码块语言
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我正在使用 Sphinx 1.4.2,其中包含一个名为“json”的 Pygments 词法分析器。默认情况下,这是开箱即用的。使用方法:
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:
我不喜欢使用 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.
即使使用 Sphinx 1.2b1 和 Pygments 1.6,我也需要调用 add_lexer 来获取
.. code-block:: json
来执行任何操作。我最终将以下代码片段放入扩展中(docs/_ext/jsonlexer.py
):我的 Sphinx docs/conf.py 具有以下内容来启用扩展:
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
):My
docs/conf.py
for Sphinx has the following to enable the extension:JSON 是 JavaScript,简单明了。 JSON 实际上代表“JavaScript 对象表示法”。
JSON is JavaScript, plain and simple. JSON in fact stands for "JavaScript Object Notation".
Sphinx 默认提供了几个专用于 JSON 突出显示的 pygment 词法分析器,因此您可以在此处选择一个:
http://pygments.org/docs/lexers/#lexers-for-data-file-format
pygments.lexers.data.JsonLexer (pygment v1.5+)
对于 JSON 数据结构。
pygments.lexers.data.JsonLdLexer(pygment v2.0+)
对于 JSON-LD 链接数据。
pygments.lexers.data.JsonBareObjectLexer(pygment v2.2+)
对于 JSON 数据结构(缺少对象大括号)。
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.
pygments.lexers.data.JsonLdLexer (pygment v2.0+)
For JSON-LD linked data.
pygments.lexers.data.JsonBareObjectLexer (pygment v2.2+)
For JSON data structures (with missing object curly braces).