Make code role highlighting consistent with code-block directive

Fixes https://github.com/sphinx-doc/sphinx/issues/5157

This is factored out of the sphinx-immaterial theme:
1ef121a612/sphinx_immaterial/inlinesyntaxhighlight.py (L1)

See also:
https://github.com/sphinx-doc/sphinx/pull/6916
This commit is contained in:
Jeremy Maitin-Shepard
2022-03-10 19:33:56 -08:00
parent b3812f72a9
commit 099b54cb87
10 changed files with 200 additions and 5 deletions

View File

@@ -499,8 +499,10 @@ __ https://pygments.org/docs/lexers
The directive's alias name :rst:dir:`sourcecode` works as well. This
directive takes a language name as an argument. It can be `any lexer alias
supported by Pygments <https://pygments.org/docs/lexers/>`_. If it is not
given, the setting of :rst:dir:`highlight` directive will be used.
If not set, :confval:`highlight_language` will be used.
given, the setting of :rst:dir:`highlight` directive will be used. If not
set, :confval:`highlight_language` will be used. To display a code example
*inline* within other text, rather than as a separate block, you can use the
:rst:role:`code` role instead.
.. versionchanged:: 2.0
The ``language`` argument becomes optional.

View File

@@ -276,6 +276,34 @@ The following role creates a cross-reference to a term in a
If you use a term that's not explained in a glossary, you'll get a warning
during build.
Inline code highlighting
------------------------
.. rst:role:: code
An *inline* code example. When used directly, this role just displays the
text *without* syntax highlighting, as a literal.
.. code-block:: rst
By default, inline code such as :code:`1 + 2` just displays without
highlighting.
Unlike the :rst:dir:`code-block` directive, this role does not respect the
default language set by the :rst:dir:`highlight` directive.
To enable syntax highlighting, you must first use the ``role`` directive to
define a custom ``code`` role for a particular language:
.. code-block:: rst
.. role:: python(code)
:language: python
In Python, :python:`1 + 2` is equal to :python:`3`.
To display a multi-line code example, use the :rst:dir:`code-block` directive
instead.
Math
----