reStructuredText 中的内联代码突出显示 [英] Inline code highlighting in reStructuredText

查看:64
本文介绍了reStructuredText 中的内联代码突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 reStructuredText 有这个指令:

I know reStructuredText has this directive:

.. code:: bash

    gedit pohl.m

呈现代码块.有没有办法让内联片段的语法高亮显示,如下所示:

which renders a code block. Is there some way to get syntax highlighting for inline snippets like this:

Do edit the file, type ``gedit pohl.m`` into a terminal.

反引号将其标记为代码,但我想用像块这样的 pygments 突出显示它.这可能吗?

The backticks mark it as code, but I'd like to highlight it with pygments like the block. Is this possible?

推荐答案

在深入研究之后,我偶然发现了文档 reStructuredText 解释文本角色.来自本文档:

Having looked into this some more I stumbled upon the document reStructuredText Interpreted Text Roles. From this document:

解释的文本在文本周围使用反引号 (`).一个明确的角色标记可以选择出现在文本之前或之后,用冒号分隔.例如:

Interpreted text uses backquotes (`) around the text. An explicit role marker may optionally appear before or after the text, delimited with colons. For example:

This is `interpreted text` using the default role.

This is :title:`interpreted text` using an explicit role.

似乎有一个code角色,所以你可以简单地输入

It seems that there is a code role, so you can simply type

:code:`a = b + c`

渲染内联代码块.要获得语法高亮,您可以定义自定义角色.例如

to render an inline code block. To get syntax highlighting you can define a custom role. For example

.. role:: bash(code)
   :language: bash

然后你可以像这样使用:

which you can then use like so:

Here is some awesome bash code :bash:`a = b + c`.

请注意,角色定义必须放在对角色的引用之前.

Note that the role definition must be placed before references to the role.

注意,我链接到的文档没有提到它所引用的 docutils 版本.代码角色在 docutils 0.8.1 中可用(这是我必须测试的唯一版本).

Note, the document I link to makes no mention of the version of docutils to which it refers. The code role is not available in docutils 0.8.1 (which is the only version I have to test against).

这篇关于reStructuredText 中的内联代码突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆