diff --git a/CHANGELOG.md b/CHANGELOG.md index 1458ef18..d64bb2a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ## Unreleased: pdoc next +- Fix incorrect ordering of `start-after`/`end-before` in RST include directives. + ([#876](https://github.com/mitmproxy/pdoc/pull/876), @JohanKarlbergg) - Support Pydantic [`computed_field`](https://docs.pydantic.dev/2.0/usage/computed_fields/) descriptions ([#855](https://github.com/mitmproxy/pdoc/pull/855), @avhz) ## 2025-10-27: pdoc 16.0.0 diff --git a/pdoc/docstrings.py b/pdoc/docstrings.py index 9d45e209..4577c783 100644 --- a/pdoc/docstrings.py +++ b/pdoc/docstrings.py @@ -408,10 +408,10 @@ def _rst_include_trim(contents: str, options: dict[str, str]) -> str: if i := options.get("start-line"): lines = lines[int(i) :] contents = "\n".join(lines) - if x := options.get("end-before"): - contents = contents[: contents.index(x)] if x := options.get("start-after"): contents = contents[contents.index(x) + len(x) :] + if x := options.get("end-before"): + contents = contents[: contents.index(x)] return contents diff --git a/test/test_docstrings.py b/test/test_docstrings.py index 658d4d0d..c03ea860 100644 --- a/test/test_docstrings.py +++ b/test/test_docstrings.py @@ -81,6 +81,14 @@ def test_rst_include_trim_pattern(): assert trimmed == "\ncharlie\ndelta\n" +def test_rst_include_trim_start_after_applied_first(): + content = 'alpha\n"""\nbeta\ncharlie\ndelta\n"""\necho' + trimmed = docstrings._rst_include_trim( + content, {"start-after": '"""\n', "end-before": '"""'} + ) + assert trimmed == "beta\ncharlie\ndelta\n" + + def test_rst_include_trim_mixture(): content = "alpha\nbeta\ncharlie\ndelta\necho" trimmed = docstrings._rst_include_trim(