3

I'm trying to get inline code snippets highlighted while using Sphinx.

I've looked at solution provided at Inline code highlighting in reStructuredText but it does not work for me. I'm using Sphinx 1.4.8.

Is there another simple and elegant way of marking inline code to be highlighted?

In absence of a full solution, i.e. one that supports true language-based highlighting, I would be happy having ability to set a non-black colour for my inline code throughout the document. Is that possible?

Edit1: Here's my code:

.. role:: py(code)
   :language: python

here is some inline code :py:`def func():`

The text row here gets rendered all in black instead of keyword 'def' in another colour. Highlighting works perfectly for code-blocks but not inline.

Edit2:

Humbalan's suggestion helped me almost solve this. I added following to $SPHINX_PROJECT_DIR/_templates/layout.html:

{# layout.html #}
{# Import the theme's layout. #}
{% extends "!layout.html" %}
{% set css_files = css_files + ['_static/style.css'] %}

Then i created $SPHINX_PROJECT_DIR/_templates/style.css with following contents to override span.pre:

span.pre{
    color: red;
    border: 1px solid black;
    padding: 2px;
    background: #feeaea
}

I'm closer to the solution but not quite there yet. I get following output now:

enter image description here

Community
  • 1
  • 1
  • 1
    Please explain what means "it does not work" and post your code. – Humbalan Nov 15 '16 at 06:57
  • Your "Edit2" again tells us what you *don't* want. but please describe which result you would like to have. From my point of view you've got what you have programmed: red text, black border with 2px distance to the text and a backgroud of the defined colour. – Humbalan Nov 22 '16 at 09:19

1 Answers1

0

This is how I highlight code snippets in my doc:

.. sourcecode:: python

    a code line 
    another code line

Does that help you?


Edit:

I did some research as I am interested in an answer too. For me it seems that the grey code is default in some (or all?) themes, I tested sphinxdoc and bizstyle. If you check the html code of the snippet (e. g. with Firefox's debugger) you get this if you use sphinxdoc

<p>here is some inline code 
<code class="code py python docutils literal">
<span class="keyword">
    <span class="pre">def</span>
</span> 
<span class="name function">
    <span class="pre">func</span>
</span>
<span class="punctuation">
    <span class="pre">():</span>
</span>
</code>
</p>

Here you can see that you could define a style for "def", another one for "func" and a 3rd one for "():" in a layout.css of your sphinx project. Example:

code.py.python.docutils.literal  { background-color: Bisque; padding: 5px; border: 1px solid maroon; }
code.py.python.docutils.literal span.keyword { background-color: red; color:white; }
code.py.python.docutils.literal span.name.function { background-color: pink; color:green; }
code.py.python.docutils.literal span.punctuation { background-color: yellow; color:brown; }

Result:

syntax highlight with role

Humbalan
  • 677
  • 3
  • 14
  • Thanks for the reply. Unfortunately not. As I said, highlighting works as intended in code-blocks but I'd like to get coloured text in inline code that is usually marked with two leading and two trailing backticks. – Rocket Singh Nov 15 '16 at 11:03
  • OK, I see. If you highlight with :py:`myfunc` the highlighting works only if there is really a documented method (or function) with the name "myfunc". And if this method is in another module you must provide the path like :py:meth:`module.class.myfunc`. – Humbalan Nov 15 '16 at 11:34
  • Sorry, I didn't know how to show the charakter which includes "myfunc". So I you see text with a grey backgrounc it is the syntax like in your exampel above ":py:..." – Humbalan Nov 15 '16 at 11:37