Search code examples
python-3.xpython-sphinxrinohtype

sphinx, rinohtype: inline code background color


How can we change background color of inline code.

One way i use inline code in restructured text is:

.. role::sql(code)
    :language: sql

You can use the query :sql:`select * from table` to execute...

When using rst2pdf, it outputs beautiful syntax highlighting and same background color as code-blocks. How can we achieve the same using rinohtype?

Ideally somthing that looks like this in stackoverflow

This is something i tried myself

stylesheet:

[code-paragraph: Paragraph(has_class="coder")]
background_color = #fdffd6

and rst:

.. role::sql(code)
    :language: sql
    :class: coder

You can use the query :sql:`select * from table` to execute...

but this didn't work, because role might be Text not Paragraph.


Solution

  • At this point, rinohtype doesn't support setting a background color (or a border) for inline elements. While we wait for that functionality to materialize, I'll discuss styling of inline text in general.

    Here is the relevant part of the stylelog for your reStructuredText snippet:

    Paragraph('You can use the query select * f...')   inline_code.rst:6 <paragraph>
         > (0,0,0,0,2) body
      MixedStyledText('You can use the query select * f...')
        SingleStyledText('You can use the query ')
        MixedStyledText('select * from table', style='monospaced')   None:None <literal>
             > (0,0,1,0,1) monospaced
          MixedStyledText('select')   None:None <inline>
            SingleStyledText('select')
          ...
    

    The element you want to match with a selector is the MixedStyledText with the monospaced style. Because it's hard to predict whether you need to match SingleStyledText or MixedStyledText elements, you should always use the more general StyledText for selectors.

    For your example, the code-paragraph definition in the style sheet will thus look like this:

    [inline-code: StyledText('monospaced', has_class='coder')]
    base = monospaced
    background_color = #fdffd6
    

    Two remarks about this:

    • Since the style specified in a selector has a larger weight than the class, you need to specify it in addition to the has_class argument, or increase the selector's priority. Read Selectors for details.
    • Set the base of the style to monospaced to inherit the properties of the standard monospaced style. Otherwise you will likely need to set the typeface and other attributes as well.

    For now, rinohtype will abort with TypeError: background_color is not a supported attribute for TextStyle. See the documentation for TextStyle to see which style attributes are supported.

    For further reading, see Element Styling in the rinohtype manual.