首页 > 解决方案 > sphinx, rinohtype: 内联代码背景颜色

问题描述

我们如何更改内联代码的背景颜色。

我在重组文本中使用内联代码的一种方法是:

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

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

使用时rst2pdf,它会输出漂亮的语法突出显示和与代码块相同的背景颜色。我们怎样才能达到同样的效果rinohtype呢?

理想的东西that looks like this in stackoverflow

这是我自己尝试过的

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...

但这不起作用,因为role可能是文本而不是段落。

标签: python-3.xpython-sphinxrinohtype

解决方案


此时,rinohtype不支持为内联元素设置背景颜色(或边框)。在我们等待该功能实现的同时,我将讨论一般的内联文本样式。

这是您的 reStructuredText 片段的样式日志的相关部分:

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')
      ...

您要与选择器匹配的元素是具有等宽样式的MixedStyledText 。因为很难预测您是否需要匹配SingleStyledTextMixedStyledText元素,所以您应该始终使用更通用的StyledText作为选择器

对于您的示例,样式表中的代码段定义将如下所示:

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

对此有两点评论:

  • 由于选择器中指定的样式class的权重更大,因此您需要在has_class参数之外指定它,或者增加选择器的优先级。阅读选择器了解详情。
  • 将样式的基础设置为等宽以继承标准等宽样式的属性。否则,您可能还需要设置字体和其他属性。

目前,rinohtype 将中止TypeError: background_color is not a supported attribute for TextStyle. 请参阅TextStyle的文档以查看支持哪些样式属性。

如需进一步阅读,请参阅rinohtype 手册中的元素样式


推荐阅读