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

Inline code is not syntax highlighted #5157

Closed
jessetan opened this issue Jul 10, 2018 · 6 comments · Fixed by #10251
Closed

Inline code is not syntax highlighted #5157

jessetan opened this issue Jul 10, 2018 · 6 comments · Fixed by #10251
Labels
Milestone

Comments

@jessetan
Copy link
Contributor

Problem

It is not possible to apply syntax highlighting to inline literals or code.

The docutils documentation provides a way to do this using the role directive to define a new role, based on the code role, but the resulting HTML contains classes like .keyword instead of classes like .highlight .k which are used in Pygments CSS.

Expected results

Sphinx to provide a code role that applies proper classes for Pygments to style.

There is an extension to add this, but it is from 2013 (so maybe out of date with recent Sphinx), and it does not contain code to generate highlighting in LaTeX.

Reproducible project / your project

.. role:: js(code)
    :language: js

This is inline code :js:`function a() { return 1; }`

Environment info

  • Sphinx version: 1.7.5
@tk0miya tk0miya added the markup label Jul 19, 2018
@tk0miya tk0miya added this to the 2.0 milestone Jul 19, 2018
@tk0miya tk0miya modified the milestones: 2.0.0, 2.1.0 Feb 4, 2019
@tk0miya tk0miya modified the milestones: 2.1.0, 2.2.0 May 30, 2019
@tk0miya tk0miya modified the milestones: 2.2.0, 2.3.0 Aug 18, 2019
@amerlyq
Copy link

amerlyq commented Dec 11, 2019

Does docutils.conf workaround from here works ?
https://stackoverflow.com/questions/21591107/sphinx-inline-code-highlight

@jessetan
Copy link
Contributor Author

Unfortunately it doesn't. That correctly applies the short class names in HTML and syntax highlighting works if youalso add the highlight class to the <code> element that contains the code.

It does not work for LaTeX output.
This:

.. role:: js(code)
    :language: js

:js:`window.addEventListener(function() { return false; });`

.. code-block:: js

    window.addEventListener(function() { return false; });

is converted to:

\sphinxcode{\sphinxupquote{\DUrole{nb}{window}\DUrole{p}{.}\DUrole{nx}{addEventListener}\DUrole{p}{(}\DUrole{kd}{function}\DUrole{p}{()} \DUrole{p}{\{} \DUrole{k}{return} \DUrole{kc}{false}\DUrole{p}{;} \DUrole{p}{\});}}}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n+nb}{window}\PYG{p}{.}\PYG{n+nx}{addEventListener}\PYG{p}{(}\PYG{k+kd}{function}\PYG{p}{(}\PYG{p}{)} \PYG{p}{\PYGZob{}} \PYG{k}{return} \PYG{k+kc}{false}\PYG{p}{;} \PYG{p}{\PYGZcb{}}\PYG{p}{)}\PYG{p}{;}
\end{sphinxVerbatim}

Only the code block (not the inline code) has highlighting

@tk0miya tk0miya modified the milestones: 2.3.0, 2.4.0 Dec 11, 2019
@tk0miya
Copy link
Member

tk0miya commented Dec 11, 2019

Yes, current version of Sphinx does not support inline highlighting on LaTeX output. So we need to improve LaTeX writer (or pygments bridge?) to support it.

cc: @jfbu

@jessetan
Copy link
Contributor Author

I've created a PR with a possible implementation. Please review or rewrite as necessary.

@tk0miya tk0miya modified the milestones: 2.4.0, 3.0.0 Feb 5, 2020
@tk0miya tk0miya modified the milestones: 3.0.0, 4.0.0 Mar 14, 2020
@tk0miya tk0miya modified the milestones: 4.0.0, 4.1.0 Mar 16, 2021
@tk0miya tk0miya modified the milestones: 4.1.0, 4.2.0 Jul 10, 2021
@tk0miya tk0miya modified the milestones: 4.2.0, 4.3.0 Sep 12, 2021
@tk0miya tk0miya modified the milestones: 4.3.0, 4.4.0 Nov 8, 2021
@gmilde
Copy link

gmilde commented Nov 8, 2021

Could adding 'syntax_highlight': 'short' to the default_settings in sphinx/environment/__init__.py solve this problem for HTML? (cf. #9701)

This would still need the "highlight" class to work with the current CSS styles.
Example from Stackexchange:

.. role:: py(code)
   :language: py
   :class: highlight

If this is too cumbersome, a change in the CSS selectors or a patch that adds "highlight" to Code nodes ("literal" nodes with "code" class in the Docutils document tree) may help.

For inline syntax highlight in LaTeX, definitions for "\DUrolenx", "\DUrolekd", ... are required.
See https://docutils.sourceforge.io/docs/user/latex.html#custom-interpreted-text-roles and the example https://docutils.sourceforge.io/sandbox/stylesheets/pygments-default.sty.

@tk0miya tk0miya modified the milestones: 4.4.0, 4.5.0 Jan 15, 2022
jbms added a commit to jbms/sphinx that referenced this issue Mar 11, 2022
@tk0miya tk0miya modified the milestones: 4.5.0, 5.0.0 Mar 27, 2022
@tk0miya
Copy link
Member

tk0miya commented May 5, 2022

Fixed by #10251. It will be released as v5.0.

@tk0miya tk0miya closed this as completed May 5, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
4 participants