Skip to content

{include} with :start-after: adds a character index to the line number #1216

Closed

Description

@chrisjsewell

Describe the bug

For {include} with :start-after:, the line numbers of the included content are wrong by a large amount. The
directive adds the marker's character index to startline, which is a line count:

# myst_parser/mocking.py, MockIncludeDirective.run (5.1.0)
if split_on_type == "start-after":
    startline += split_index + len(split_on)
    file_content = file_content[split_index + len(split_on) :]

startline is then passed on to nested_render_text(file_content, startline + 1, ...), so every line in the slice
(and every warning raised in it) is shifted by the number of characters that precede the end of the marker.

Reproduce

conf.py: extensions = ["myst_parser"], exclude_patterns = ["inc"]. There are three copies of the same file,
inc/plain.md, inc/startline.md and inc/startafter.md, each using its own role name:

line one
line two
START-HERE
line four
{plain}`x`

index.md:

# Title

```{include} inc/plain.md
```

```{include} inc/startline.md
:start-line: 3
```

```{include} inc/startafter.md
:start-after: START-HERE
```

With myst-parser 5.1.0, docutils 0.22.4 and Sphinx 9.1.0, sphinx-build -E, the warnings are:

index.md:2: WARNING: Unknown interpreted text role "plain"
index.md:5: WARNING: Unknown interpreted text role "startline"
index.md:31: WARNING: Unknown interpreted text role "startafter"

The paragraph holding the role starts on line 1 (plain), and on line 4 for the two clipped includes. plain and
startline are only off by the constant one of #1165. startafter reports line 31: the 28 characters up to the
end of START-HERE were added as lines.

Expected

:start-after: should add the number of lines consumed before the slice, for example
file_content[:split_index + len(split_on)].count("\n"), so that it lands on the same line as :start-line: would.
A regression test of the warning line for :start-after: (and for :start-line: together with :start-after:)
would pin it, alongside #1165's fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions