Sphinx inline code highlight

前端 未结 6 1671
暖寄归人
暖寄归人 2021-02-07 11:03

I use Sphinx to make a website that contains code samples. I\'m successful using the .. code-block directive to get syntax highlighting. But I can\'t get inline syn

6条回答
  •  Happy的楠姐
    2021-02-07 11:38

    Here's a better than previous workaround:

    Change default --syntax-highlight option of docuitls. In

    docutils/parsers/rst/__init__.py
    

    Find

             ('Token name set for parsing code with Pygments: one of '
              '"long", "short", or "none (no parsing)". Default is "short".',
              ['--syntax-highlight'],
              {'choices': ['long', 'short', 'none'],
               'default': 'long', 'metavar': ''}),
    

    and chage the default to short:

               'default': 'short', 'metavar': ''}),
    

    The method to highlight inline code in sphnix is to register a new role:

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

    This construct is from docutils, not from sphinx. Then one can change the docutils's default, to get the desired output (short classes). A better solution would be to set the value during the init of the class in sphinx. But where that happens might not be easy to find.

    This solution is much better than the previous since it doesn't double the number of css rules to match to color the code.

提交回复
热议问题