Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/specify_cli/markdown_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def replace_section(
headings: list[tuple[int, str, int, int, int]] = []
fence_marker = ""
fence_length = 0
in_comment = False
metadata = content.startswith(("---\n", "---\r\n"))
offset = 0
for number, line in enumerate(
Expand All @@ -150,6 +151,15 @@ def replace_section(
if closing and closing[1][0] == fence_marker and len(closing[1]) >= fence_length:
fence_marker = ""
continue
if in_comment:
in_comment = "-->" not in text
continue
# CommonMark HTML block, start condition 2: an HTML comment runs from
# a line starting with "<!--" to the first line containing "-->",
# which may be the opening line itself.
if re.match(r" {0,3}<!--", text):
in_comment = "-->" not in text[text.index("<!--") + 2:]
continue
opening = re.match(r" {0,3}(`{3,}|~{3,})(.*)$", text)
if opening and not (opening[1][0] == "`" and "`" in opening[2]):
fence_marker, fence_length = opening[1][0], len(opening[1])
Expand Down
40 changes: 40 additions & 0 deletions tests/test_markdown_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,35 @@ def test_fenced_headings_neither_match_nor_end_section(fence: str, close: str) -
) == f"# Doc\n{fence}\n## Target\n{close}\n## Target\nnew\n## Next\nkeep\n"


@pytest.mark.parametrize(
"comment",
[
"<!--\n## Example\n-->",
" <!-- guidance\n# Example\n```\nstill a comment -->",
"<!-- ## Example -->",
],
)
def test_commented_headings_do_not_end_section(comment: str) -> None:
assert replace_section(
f"## Target\nold\n{comment}\nmore\n## Next\nkeep\n", "new\n", "target",
preset="team", contribution="body.md", artifact="spec-template",
) == "## Target\nnew\n## Next\nkeep\n"


def test_commented_heading_does_not_match_selector() -> None:
assert replace_section(
"<!--\n## Target\nexample\n-->\n## Target\nold\n", "new\n", "target",
preset="team", contribution="body.md", artifact="spec-template",
) == "<!--\n## Target\nexample\n-->\n## Target\nnew\n"


def test_heading_after_comment_close_ends_section() -> None:
assert replace_section(
"## Target\nold\n<!--\nnote -->\n## Next\nkeep\n", "new\n", "target",
preset="team", contribution="body.md", artifact="spec-template",
) == "## Target\nnew\n## Next\nkeep\n"


@pytest.mark.parametrize(
"content,code,count,lines",
[
Expand All @@ -174,6 +203,17 @@ def test_fenced_headings_neither_match_nor_end_section(fence: str, close: str) -
(" ## Target\nnot ATX\n", "SECTION_NOT_FOUND", 0, []),
("##Target\nnot ATX\n", "SECTION_NOT_FOUND", 0, []),
("Target\n------\n", "SECTION_NOT_FOUND", 0, []),
("<!--\n## Target\n", "SECTION_NOT_FOUND", 0, []),
(
"<!--\n## Target\n-->\n## Target\n<!-- ## Target -->\n## Target\n",
"SECTION_AMBIGUOUS", 2, [4, 6],
),
("<!-->\n## Target\n<!--->\n## Target\n", "SECTION_AMBIGUOUS", 2, [2, 4]),
(
" <!--\n## Target\n<!-- x\n## Target\n-->\n## Target\n",
"SECTION_AMBIGUOUS", 2, [2, 6],
),
("text <!--\n## Target\n```\n<!--\n```\n## Target\n", "SECTION_AMBIGUOUS", 2, [2, 6]),
],
)
def test_selection_diagnostics(
Expand Down