Add section splitting and wikilink detection to the domain
Sections are split at level 1 and 2 headings outside code and comments, each with a content hash. Wikilinks are found outside code and comments. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f5f78a972d
commit
cce8afada5
5 files changed
+722
No files matched your search
@@ -0,0 +1,101 @@
|
||||
from chemenu.domain.sections import SectionKey, split_sections
|
||||
|
||||
BODY = "# Title\nintro\n\n## A\nalpha\n\n## B\nbeta\n"
|
||||
|
||||
|
||||
def versions(body: str) -> dict[SectionKey, str]:
|
||||
return {s.key: s.version for s in split_sections(body)}
|
||||
|
||||
|
||||
def test_texts_join_to_body() -> None:
|
||||
for body in ["", "x", BODY, "a\r\n## b\rc\n```\n## no", "\n\n## A", "## A\n## A\n"]:
|
||||
assert "".join(s.text for s in split_sections(body)) == body
|
||||
assert split_sections("")[0].key == SectionKey(None)
|
||||
|
||||
|
||||
def test_change_affects_one_version() -> None:
|
||||
before = versions(BODY)
|
||||
after = versions(BODY.replace("alpha", "ALPHA"))
|
||||
changed = [k for k in before if before[k] != after[k]]
|
||||
assert changed == [SectionKey("A")]
|
||||
|
||||
|
||||
def test_append_keeps_versions() -> None:
|
||||
before = versions(BODY)
|
||||
after = versions(BODY + "\n## C\ngamma\n")
|
||||
assert all(after[k] == v for k, v in before.items())
|
||||
assert SectionKey("C") in after
|
||||
|
||||
|
||||
def test_title_line_not_hashed() -> None:
|
||||
a = split_sections("# One\n\ntext\n")[0]
|
||||
b = split_sections("# Two\n\ntext\n")[0]
|
||||
assert a.key == SectionKey(None)
|
||||
assert a.version == b.version
|
||||
assert len(split_sections("# One\ntext\n")) == 1
|
||||
# a second level-1 heading separates
|
||||
assert [s.key.heading for s in split_sections("# One\n# Two\n")] == [None, "Two"]
|
||||
|
||||
|
||||
def test_subheadings_belong_to_section() -> None:
|
||||
body = "## A\n### sub\ntext\nSetext\n======\n ## indented\n#tag\n"
|
||||
assert [s.key.heading for s in split_sections(body)] == [None, "A"]
|
||||
|
||||
|
||||
def test_code_and_comments_do_not_split() -> None:
|
||||
body = (
|
||||
"intro\n```\n## no\n```\n## yes\n~~~\n## no\n~~~\n"
|
||||
"<!--\n## no\n-->\n## also\n <!-- ```\n## no\n-->\n## last\n"
|
||||
"```\n## open\n"
|
||||
)
|
||||
assert [s.key.heading for s in split_sections(body)] == [None, "yes", "also", "last"]
|
||||
# "<!-->" is a complete comment, a tab-indented fence does not close
|
||||
tricky = "<!-->\n## A\n```\n\t```\n## no\n```\n## B\n"
|
||||
assert [s.key.heading for s in split_sections(tricky)] == [None, "A", "B"]
|
||||
inner = "## A\n<!--\n```\n-->\n## B\n"
|
||||
assert [s.key.heading for s in split_sections(inner)] == [None, "A", "B"]
|
||||
|
||||
|
||||
def test_duplicate_headings() -> None:
|
||||
keys = [s.key for s in split_sections("## A\n# A\n## B\n## A\n")]
|
||||
assert keys == [
|
||||
SectionKey(None),
|
||||
SectionKey("A", 1),
|
||||
SectionKey("A", 2),
|
||||
SectionKey("B", 1),
|
||||
SectionKey("A", 3),
|
||||
]
|
||||
|
||||
|
||||
def test_crlf_same_version() -> None:
|
||||
assert versions(BODY) == versions(BODY.replace("\n", "\r\n"))
|
||||
assert versions(BODY) == versions(BODY.replace("\n", "\r"))
|
||||
assert split_sections("## A ## \nx")[1].key == SectionKey("A")
|
||||
|
||||
|
||||
def test_inner_whitespace_changes_version() -> None:
|
||||
plain, blank_edges, indented, gap = (
|
||||
split_sections(f"## A\n{text}")[1].version
|
||||
for text in ("a\nb\n", " \n\na\nb\n\t\n\n", " a\nb\n", "a\n\nb\n")
|
||||
)
|
||||
assert plain == blank_edges
|
||||
assert len({plain, indented, gap}) == 3
|
||||
|
||||
|
||||
def test_container_fences_do_not_hide_headings() -> None:
|
||||
def headings(body: str) -> list[str | None]:
|
||||
return [s.key.heading for s in split_sections(body)]
|
||||
|
||||
assert headings("- ```bash\n ls\n ```\n## Next\n[[L]]\n") == [None, "Next"]
|
||||
# unclosed fence in a list ends with the list
|
||||
assert headings("- ```\n ## no\n## Next\n") == [None, "Next"]
|
||||
assert headings("1. a\n ```\n ## no\n ```\n## B\n") == [None, "B"]
|
||||
# unclosed fence in a quote ends with the quote
|
||||
assert headings("> ```\n> ## no\n## Next\n") == [None, "Next"]
|
||||
assert headings("> ```\n> ## no\n\n## Next\n") == [None, "Next"]
|
||||
# at top level an unclosed fence runs to the end
|
||||
assert headings("```\n## no\n- x\n## no\n") == [None]
|
||||
# indented code is not recognised and opens no fence
|
||||
assert headings(" ```\n## A\n") == [None, "A"]
|
||||
# thematic breaks are not list items
|
||||
assert headings("---\n```\n## no\n```\n## A\n* * *\n## B\n") == [None, "A", "B"]
|
||||
Reference in new issue
Block a user