diff --git a/sphinx/builders/latex/transforms.py b/sphinx/builders/latex/transforms.py index 9fe44bc95ee..251d7ff900e 100644 --- a/sphinx/builders/latex/transforms.py +++ b/sphinx/builders/latex/transforms.py @@ -600,6 +600,17 @@ def apply(self): node.parent.insert(i + 1, index) +class InlineCodeTransform(SphinxTransform): + """Mark inline nodes just under literal node as "code" to handle them in writer""" + default_priority = 200 + + def apply(self): + for node in self.document.traverse(nodes.literal): + for subnode in node: + if isinstance(subnode, nodes.inline): + subnode['code'] = True + + def setup(app: Sphinx) -> Dict[str, Any]: app.add_transform(FootnoteDocnameUpdater) app.add_post_transform(BibliographyTransform) @@ -610,6 +621,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_post_transform(LiteralBlockTransform) app.add_post_transform(MathReferenceTransform) app.add_post_transform(ShowUrlsTransform) + app.add_post_transform(InlineCodeTransform) return { 'version': 'builtin', diff --git a/sphinx/environment/__init__.py b/sphinx/environment/__init__.py index bc9bf49a74e..4804c89c52e 100644 --- a/sphinx/environment/__init__.py +++ b/sphinx/environment/__init__.py @@ -46,6 +46,7 @@ default_settings = { 'embed_stylesheet': False, 'cloak_email_addresses': True, + 'syntax_highlight': 'short', 'pep_base_url': 'https://www.python.org/dev/peps/', 'pep_references': None, 'rfc_base_url': 'https://tools.ietf.org/html/', diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 9ac76901bc5..bb66278ae4a 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -537,8 +537,11 @@ def visit_literal(self, node): self.body.append(self.starttag(node, 'kbd', '', CLASS='docutils literal notranslate')) else: + classes = 'docutils literal notranslate' + if 'code' in node['classes']: + classes += ' highlight' self.body.append(self.starttag(node, 'code', '', - CLASS='docutils literal notranslate')) + CLASS=classes)) self.protect_literal_text += 1 def depart_literal(self, node): diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index fe57f42bff5..7fbfa0c11c6 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -484,8 +484,11 @@ def visit_literal(self, node): self.body.append(self.starttag(node, 'kbd', '', CLASS='docutils literal notranslate')) else: + classes = 'docutils literal notranslate' + if 'code' in node['classes']: + classes += ' highlight' self.body.append(self.starttag(node, 'code', '', - CLASS='docutils literal notranslate')) + CLASS=classes)) self.protect_literal_text += 1 def depart_literal(self, node): diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index d0bd38ce2dc..501fb946bf7 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -2294,6 +2294,9 @@ def visit_inline(self, node): elif classes in [['accelerator']]: self.body.append(r'\sphinxaccelerator{') self.context.append('}') + elif classes and node.get('code', False): + self.body.append(r'\PYG{%s}{' % ','.join(classes)) + self.context.append('}') elif classes and not self.in_title: self.body.append(r'\DUrole{%s}{' % ','.join(classes)) self.context.append('}')