summaryrefslogtreecommitdiff
path: root/sphinx/writers/html5.py
diff options
context:
space:
mode:
authorJeremy Maitin-Shepard <jbms@google.com>2022-03-10 19:33:56 -0800
committerJeremy Maitin-Shepard <jbms@google.com>2022-03-19 20:37:49 -0700
commit099b54cb87db3ca210f6edd67dfdbde3ec83c9a4 (patch)
tree9dad6cce1d15dab01960a2780bfa5b3068c76383 /sphinx/writers/html5.py
parentb3812f72a98b01bae4b1158761082edc46cfa87f (diff)
downloadsphinx-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/html5.py')
-rw-r--r--sphinx/writers/html5.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py
index b9d0f648c..4104bf81f 100644
--- a/sphinx/writers/html5.py
+++ b/sphinx/writers/html5.py
@@ -470,10 +470,25 @@ class HTML5Translator(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']: