diff --git a/myst_parser/mocking.py b/myst_parser/mocking.py index 7404899e..97e6dcaf 100644 --- a/myst_parser/mocking.py +++ b/myst_parser/mocking.py @@ -570,6 +570,10 @@ def run(self) -> list[nodes.Element]: # get required section of text startline = self.options.get("start-line", None) endline = self.options.get("end-line", None) + if startline is not None and startline < 0: + # resolve to the actual 0-based line, for source-line reporting + # (slicing semantics are unchanged by this) + startline = max(0, len(file_content.splitlines()) + startline) file_content = "\n".join(file_content.splitlines()[startline:endline]) startline = startline or 0 for split_on_type in ["start-after", "end-before"]: @@ -583,7 +587,9 @@ def run(self) -> list[nodes.Element]: f'Directive "{self.name}"; option "{split_on_type}": text not found "{split_on}".', ) if split_on_type == "start-after": - startline += split_index + len(split_on) + # advance by the number of *lines* removed, so that + # source-line reporting stays aligned + startline += file_content.count("\n", 0, split_index + len(split_on)) file_content = file_content[split_index + len(split_on) :] else: file_content = file_content[:split_index] @@ -655,9 +661,12 @@ def run(self) -> list[nodes.Element]: source_dir, path.parent, ) + # ``nested_render_text`` expects the 0-based source line of the + # first line of the text (the final 0-based to 1-based conversion + # happens in ``_render_tokens``), which is exactly ``startline`` self.renderer.nested_render_text( file_content, - startline + 1, + startline, heading_offset=self.options.get("heading-offset", 0), ) finally: diff --git a/tests/test_renderers/fixtures/mock_include_errors.md b/tests/test_renderers/fixtures/mock_include_errors.md index 6e443cb6..40d0d6db 100644 --- a/tests/test_renderers/fixtures/mock_include_errors.md +++ b/tests/test_renderers/fixtures/mock_include_errors.md @@ -19,5 +19,132 @@ Error in include file: ```{include} bad.md ``` . -tmpdir/bad.md:2: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +tmpdir/bad.md:1: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file: +. +```{include} bad_line3.md +``` +. +tmpdir/bad_line3.md:3: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file, after content in the parent: +. +para + +```{include} bad_line3.md +``` +. +tmpdir/bad_line3.md:3: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file with start-line: +. +```{include} bad_skipped.md +:start-line: 2 +``` +. +tmpdir/bad_skipped.md:4: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file with front matter: +. +```{include} bad_frontmatter.md +``` +. +tmpdir/bad_frontmatter.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in nested include file: +. +```{include} bad_outer.md +``` +. +tmpdir/bad_inner.md:3: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in include file with start-after: +. +```{include} bad_after.md +:start-after: START-HERE +``` +. +tmpdir/bad_after.md:4: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in include file with start-line and start-after: +. +```{include} bad_after.md +:start-line: 1 +:start-after: START-HERE +``` +. +tmpdir/bad_after.md:4: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in include file with a multi-line start-after marker: +. +```{include} bad_marker2.md +--- +start-after: "\n" +--- +``` +. +tmpdir/bad_marker2.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in include file with a negative start-line: +. +```{include} bad_negative.md +:start-line: -2 +``` +. +tmpdir/bad_negative.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in include file with a negative start-line beyond its length: +. +```{include} bad_negative.md +:start-line: -100 +``` +. +tmpdir/bad_negative.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in CRLF include file with start-after: +. +```{include} bad_crlf.md +:start-after: MARK +``` +. +tmpdir/bad_crlf.md:4: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in include file with start-after and end-before: +. +```{include} bad_between.md +:start-after: START +:end-before: END +``` +. +tmpdir/bad_between.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +No error when the start-after marker ends the file: +. +```{include} bad_between.md +:start-after: {c}`d` +``` +. +. + +Errors in a nested include file with start-line, and in the outer file after it: +. +```{include} bad_outer2.md +``` +. +tmpdir/bad_inner2.md:4: (WARNING/2) Unknown interpreted text role "c". [myst.role_unknown] +tmpdir/bad_outer2.md:7: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] . diff --git a/tests/test_renderers/test_include_directive.py b/tests/test_renderers/test_include_directive.py index 9f03d633..96ede2ba 100644 --- a/tests/test_renderers/test_include_directive.py +++ b/tests/test_renderers/test_include_directive.py @@ -41,6 +41,31 @@ def test_errors(file_params, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) tmp_path.joinpath("bad.md").write_text("{a}`b`") + # The role sits on line 3, so a warning reported against any other line is + # the include's own line attribution being off. + tmp_path.joinpath("bad_line3.md").write_text("line one\n\n{a}`b`\n") + tmp_path.joinpath("bad_skipped.md").write_text("skipped\nskipped2\n\n{a}`b`\n") + tmp_path.joinpath("bad_frontmatter.md").write_text("---\nx: 1\n---\n\n{a}`b`\n") + tmp_path.joinpath("bad_inner.md").write_text("inner one\n\n{a}`b`\n") + tmp_path.joinpath("bad_outer.md").write_text( + "outer line\n\n```{include} bad_inner.md\n```\n" + ) + tmp_path.joinpath("bad_after.md").write_text( + "line one\nline two\nSTART-HERE\nline four\n{a}`b`" + ) + tmp_path.joinpath("bad_marker2.md").write_text( + "x\n\n\n\n{a}`b`\n" + ) + tmp_path.joinpath("bad_negative.md").write_text("a\nb\nc\n\n{a}`b`\n") + # bytes, so the CRLF line endings are kept on every platform + tmp_path.joinpath("bad_crlf.md").write_bytes(b"a\r\nMARK\r\n\r\n{a}`b`\r\n") + tmp_path.joinpath("bad_between.md").write_text( + "a\nSTART\nmid\n\n{a}`b`\nEND\n{c}`d`\n" + ) + tmp_path.joinpath("bad_inner2.md").write_text("skip\ninner\n\n{c}`d`\n") + tmp_path.joinpath("bad_outer2.md").write_text( + "outer\n\n```{include} bad_inner2.md\n:start-line: 1\n```\n\n{a}`b`\n" + ) report_stream = StringIO() publish_doctree(