Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add inline syntax highlighting using docutils :code: role #6916

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions sphinx/environment/__init__.py
Expand Up @@ -46,6 +46,7 @@
default_settings = {
'embed_stylesheet': False,
'cloak_email_addresses': True,
'syntax_highlight': 'short',
tk0miya marked this conversation as resolved.
Show resolved Hide resolved
'pep_base_url': 'https://www.python.org/dev/peps/',
'pep_references': None,
'rfc_base_url': 'https://tools.ietf.org/html/',
Expand Down
5 changes: 4 additions & 1 deletion sphinx/writers/html.py
Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion sphinx/writers/html5.py
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions sphinx/writers/latex.py
Expand Up @@ -464,6 +464,7 @@ def __init__(self, document, builder):
self.in_footnote = 0
self.in_caption = 0
self.in_term = 0
self.in_code = 0
self.needs_linetrimming = 0
self.in_minipage = 0
self.no_latex_floats = 0
Expand Down Expand Up @@ -2044,6 +2045,8 @@ def depart_citation_reference(self, node):
def visit_literal(self, node):
# type: (nodes.Element) -> None
self.no_contractions += 1
if('code' in node.get('classes', [])):
self.in_code = 1
if self.in_title:
self.body.append(r'\sphinxstyleliteralintitle{\sphinxupquote{')
else:
Expand All @@ -2052,6 +2055,7 @@ def visit_literal(self, node):
def depart_literal(self, node):
# type: (nodes.Element) -> None
self.no_contractions -= 1
self.in_code = 0
self.body.append('}}')

def visit_footnote_reference(self, node):
Expand Down Expand Up @@ -2294,6 +2298,9 @@ def visit_inline(self, node):
elif classes in [['accelerator']]:
self.body.append(r'\sphinxaccelerator{')
self.context.append('}')
elif classes and self.in_code:
tk0miya marked this conversation as resolved.
Show resolved Hide resolved
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('}')
Expand Down