Skip to content

Conversation

kdeldycke
Copy link

@kdeldycke kdeldycke commented Sep 10, 2025

In rST, a directive's block_text contains the entire directive, including options and full, unparsed content.

This aligns MyST parser to rST.

Context

I stumbled upon this issue while implementing my own click:example and click:run directives within my Click Extra project: https://kdeldycke.github.io/click-extra/sphinx.html

My implementation is trying to report, within these directives, of the absolute line in the document where a problem occured. The line number and offset calculation works perfectly in rST, but requires some hacks to report the right one in MyST.

But even with these hacks, there is no way to retro-actively compute the absolute line number, because the directive object do not carry its unparsed content.

Example

Compare a given rST directive defined as:

 .. click:run::
    :linenos:

    # This should fail due to variable conflict.
    isolated_filesystem = "Do not overwrite me!"

Its properties are:

>>> print("\n".join(directive.content))
# This should fail due to variable conflict.
isolated_filesystem = "Do not overwrite me!"

>>> print(directive.block_text)
.. click:run::
    :linenos:

    # This should fail due to variable conflict.
    isolated_filesystem = "Do not overwrite me!"

And for its MyST equivalent:

```{click:run}
:linenos:
# This should fail due to variable conflict.
isolated_filesystem = "Do not overwrite me!"
```
>>> print("\n".join(directive.content))
# This should fail due to variable conflict.
isolated_filesystem = "Do not overwrite me!"

>>> print(directive.block_text)
# This should fail due to variable conflict.
isolated_filesystem = "Do not overwrite me!"

Limitations

Note that it is not perfect, as it is supposed to also contain:

  • the complete directive header (i.e the ```{name} {arguments} first line), and
  • the directive closing line (i.e. the ``` line).

But I doubt we can find a way to do it unless we introduce heavier refactor.

kdeldycke added a commit to kdeldycke/click-extra that referenced this pull request Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant