diff options
author | Jeremy Maitin-Shepard <jbms@google.com> | 2022-03-10 19:33:56 -0800 |
---|---|---|
committer | Jeremy Maitin-Shepard <jbms@google.com> | 2022-03-19 20:37:49 -0700 |
commit | 099b54cb87db3ca210f6edd67dfdbde3ec83c9a4 (patch) | |
tree | 9dad6cce1d15dab01960a2780bfa5b3068c76383 /sphinx/writers/html.py | |
parent | b3812f72a98b01bae4b1158761082edc46cfa87f (diff) | |
download | sphinx-git-099b54cb87db3ca210f6edd67dfdbde3ec83c9a4.tar.gz |
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:
https://github.com/jbms/sphinx-immaterial/blob/1ef121a612d4f5afc2a9ca9c4e3f20fca89065e8/sphinx_immaterial/inlinesyntaxhighlight.py#L1
See also:
https://github.com/sphinx-doc/sphinx/pull/6916
Diffstat (limited to 'sphinx/writers/html.py')
-rw-r--r-- | sphinx/writers/html.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 34b73a0a5..43ad2a925 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -511,10 +511,25 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator): if 'kbd' in node['classes']: self.body.append(self.starttag(node, 'kbd', '', CLASS='docutils literal notranslate')) - else: + return + lang = node.get("language", None) + if 'code' not in node['classes'] or not lang: self.body.append(self.starttag(node, 'code', '', CLASS='docutils literal notranslate')) self.protect_literal_text += 1 + return + + opts = self.config.highlight_options.get(lang, {}) + highlighted = self.highlighter.highlight_block( + node.astext(), lang, opts=opts, location=node, nowrap=True) + starttag = self.starttag( + node, + "code", + suffix="", + CLASS="docutils literal highlight highlight-%s" % lang, + ) + self.body.append(starttag + highlighted.strip() + "</code>") + raise nodes.SkipNode def depart_literal(self, node: Element) -> None: if 'kbd' in node['classes']: |