From 8db940c4494d4d8fa139f0cea22856137216252e Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Thu, 5 Oct 2023 13:11:49 -0400 Subject: [PATCH 1/3] fix: ensure docstring section transform preserves starting text --- quartodoc/ast.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/quartodoc/ast.py b/quartodoc/ast.py index 4dc3c547..acecfcce 100644 --- a/quartodoc/ast.py +++ b/quartodoc/ast.py @@ -76,6 +76,11 @@ def split_sections(text) -> list[tuple[str, str]]: # in order to find the body of text between multiple sections. results = [] while crnt_match is not None: + # if the first section comes later in the text, ensure that we + # create a section for the very beginning + if crnt_pos == 0: + results.append(("", text[: crnt_match.start()])) + next_pos = crnt_pos + crnt_match.end() substr = text[next_pos:] next_match = comp.search(substr) From 3af1233cc34f3ec248133bcfe57cdfd93e333b0b Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Thu, 5 Oct 2023 13:36:51 -0400 Subject: [PATCH 2/3] fix: section transform preserving too much --- quartodoc/ast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quartodoc/ast.py b/quartodoc/ast.py index acecfcce..76c12266 100644 --- a/quartodoc/ast.py +++ b/quartodoc/ast.py @@ -78,7 +78,7 @@ def split_sections(text) -> list[tuple[str, str]]: while crnt_match is not None: # if the first section comes later in the text, ensure that we # create a section for the very beginning - if crnt_pos == 0: + if crnt_pos == 0 and crnt_match.start() > 0: results.append(("", text[: crnt_match.start()])) next_pos = crnt_pos + crnt_match.end() From 7702983e44a4bfd61501e5fc4a0ffbc48c0c40dc Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Tue, 10 Oct 2023 11:15:59 -0400 Subject: [PATCH 3/3] tests: tweak docstring section ast tests --- quartodoc/tests/test_ast.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/quartodoc/tests/test_ast.py b/quartodoc/tests/test_ast.py index fbe5001f..34d178d4 100644 --- a/quartodoc/tests/test_ast.py +++ b/quartodoc/tests/test_ast.py @@ -14,7 +14,6 @@ ("See Also\n---", ""), ("See Also\n---\n", ""), ("See Also\n--------", ""), - ("\n\nSee Also\n---\n", ""), ("See Also\n---\nbody text", "body text"), ("See Also\n---\nbody text", "body text"), ], @@ -29,6 +28,19 @@ def test_transform_docstring_section(el, body): assert res[0].value == body +def test_transform_docstring_section_multiple(): + # Note the starting text is not a section header + # so this should be split into two sections: short description, and see also. + src = ds.DocstringSectionText("A short description\n\nSee Also\n---\n") + res = qast._DocstringSectionPatched.transform(src) + + assert len(res) == 2 + assert res[0].value == "A short description\n\n" + assert isinstance(res[1], qast.DocstringSectionSeeAlso) + assert res[1].title == "See Also" + assert res[1].value == "" + + @pytest.mark.parametrize( "el, cls", [