0

Surprisingly sphinx does not offer a role to markup other parameters. Using :obj:`...` creates a random, misleading link and ``...`` renders into a completely different format. Therefore I would like to define a custom role arg which I can use like this:

    def is_comment(self, ln: str) -> bool:
        '''
        Check if :arg:`ln` is a comment.

        :param ln: The current line
        :return: :const:`True` if :arg:`ln` is a comment
        '''

I have already figured out that I can define a custom role by setting rst_prolog in docs/source/conf.py (thanks to this answer)

rst_prolog = '''
.. role:: arg
'''

However, I am lost how to define how this role is supposed to be rendered.

It is easy to inherit from a different role like this:

rst_prolog = '''
.. role:: arg(obj)
'''

Which looks good but has the same problem like using obj directly: It creates wrong links.

My second attempt was to not inherit from another role but specify a class as shown here:

rst_prolog = '''
.. role:: arg
    :class: code
'''

but that gives several error messages <rst_prolog>:3: WARNING: Explicit markup ends without a blank line; unexpected unindent. Adding an empty line below does not help.

Is there an easy way to make arg(obj) not be a link?

What am I doing wrong in my second attempt?


EDIT: I am one step further. The problem in my second attempt was that I indented :class: code with a tab. Using spaces instead silences the error message but the code is not highlighted.

Right Click > Inspect in Firefox shows that it is correctly translated to Check if <span class="code">ln</span> is a comment. However, the code class is ignored.

If I inspect the True (which is rendered as expected) I see that it is using the code class and that the code class is defined in nature.css as

code {
    background-color: #ecf0f3;
    color: #222;
    /* padding: 1px 2px; */
    font-size: 1.1em;
    font-family: monospace;
}

How do I use the code class?

jakun
  • 624
  • 5
  • 13
  • Can you explain why you need this role? – mzjn Dec 05 '22 at 13:05
  • @mzjn well, I have tried explaining in the first paragraph ^^" I have not found any predefined role which is suitable. And if I don't mark it any way it is rendered as normal text. That is confusing because all other names referring to Python objects are formatted in a special way. Formatting only parameters not is inconsistent. Also the default font for text is not suitable for variable names because it is very difficult to tell certain character apart, like a lower case l and an upper case I in this case. – jakun Dec 05 '22 at 13:24
  • I'm just trying to understand what the point is. Are you perhaps looking for something like this? https://pypi.org/project/sphinx-paramlinks/ – mzjn Dec 05 '22 at 13:29

0 Answers0