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
e0fc4653a9
commit
931b1dcc5f
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"]
|
||||
@@ -0,0 +1,100 @@
|
||||
import time
|
||||
|
||||
from chemenu.domain.wikilinks import WikiLink, find_wikilinks
|
||||
|
||||
|
||||
def test_targets() -> None:
|
||||
body = "[[Foo]] and [[Bar, Baz]] again [[Foo]]\n[[ ]] [[#X]] [[a\n b]]"
|
||||
assert find_wikilinks(body) == (
|
||||
WikiLink("Foo"),
|
||||
WikiLink("Bar, Baz"),
|
||||
WikiLink("Foo"),
|
||||
)
|
||||
|
||||
|
||||
def test_anchor_text_and_embed() -> None:
|
||||
body = "[[T#A#b]] [[T#^blk|Txt]] ![[E]] [[ T # | ]] [[a [b] c|x [y]]]"
|
||||
assert find_wikilinks(body) == (
|
||||
WikiLink("T", "A#b"),
|
||||
WikiLink("T", "^blk", "Txt"),
|
||||
WikiLink("E", embed=True),
|
||||
WikiLink("T"),
|
||||
WikiLink("a [b] c", None, "x [y"),
|
||||
)
|
||||
|
||||
|
||||
def test_escaped_pipe() -> None:
|
||||
assert find_wikilinks("| [[T\\|Text]] |") == (WikiLink("T", None, "Text"),)
|
||||
|
||||
|
||||
def test_code_is_ignored() -> None:
|
||||
body = (
|
||||
"```\n[[a]]\n```\n`[[b]]` ``[[c]]`` `x` [[d]]\n<!--\n[[e]]\n-->\nx <!-- [[f]] --> [[g]]\n"
|
||||
)
|
||||
assert [link.target for link in find_wikilinks(body)] == ["d", "g"]
|
||||
|
||||
|
||||
def test_unclosed_code_or_comment_hides_nothing() -> None:
|
||||
body = "x <!-- [[a]]\n`[[b]]\n``[[c]]`\n[[d]]\n"
|
||||
assert [link.target for link in find_wikilinks(body)] == ["a", "b", "c", "d"]
|
||||
|
||||
|
||||
def test_first_opener_wins() -> None:
|
||||
body = "`<!--` [[a]] `-->`\nx <!-- ` --> [[b]] `\n"
|
||||
assert [link.target for link in find_wikilinks(body)] == ["a", "b"]
|
||||
|
||||
|
||||
def targets(body: str) -> list[str]:
|
||||
return [link.target for link in find_wikilinks(body)]
|
||||
|
||||
|
||||
def test_anchor_with_escaped_pipe() -> None:
|
||||
assert find_wikilinks("[[T#A\\|Text]]") == (WikiLink("T", "A", "Text"),)
|
||||
|
||||
|
||||
def test_link_text_keeps_inline_code() -> None:
|
||||
assert find_wikilinks("[[Use `x` now]]") == (WikiLink("Use `x` now"),)
|
||||
assert find_wikilinks("[[T|`code`]]") == (WikiLink("T", None, "`code`"),)
|
||||
assert find_wikilinks("[[T#A <!-- c --> b|x]]") == (WikiLink("T", "A <!-- c --> b", "x"),)
|
||||
assert find_wikilinks("[[T#`a#b`|x]]") == (WikiLink("T", "`a#b`", "x"),)
|
||||
assert find_wikilinks("[[T|`a|b`]]") == (WikiLink("T", None, "`a|b`"),)
|
||||
|
||||
|
||||
def test_text_after_comment_counts() -> None:
|
||||
assert targets("<!-- a --> [[B]]") == ["B"]
|
||||
assert targets("<!--\nx [[a]]\n--> [[g]] `[[h]]` [[i]]\n") == ["g", "i"]
|
||||
assert targets("<!-- c --> [[g]] <!-- [[h]] --> [[i]]") == ["g", "i"]
|
||||
|
||||
|
||||
def test_escapes() -> None:
|
||||
assert targets("\\`[[A]]\\`") == ["A"]
|
||||
assert targets("\\[[A]]") == []
|
||||
assert targets("\\\\[[A]]") == ["A"]
|
||||
assert targets("\\\\\\[[A]]") == []
|
||||
assert targets("\\<!-- [[A]] -->") == ["A"]
|
||||
assert targets("`a\\` [[A]] `b`") == ["A"]
|
||||
assert find_wikilinks("[[T\\|x]]") == (WikiLink("T", None, "x"),)
|
||||
|
||||
|
||||
def test_fences_in_lists_and_quotes() -> None:
|
||||
assert targets("- ```bash\n [[a]]\n ```\n[[b]]\n") == ["b"]
|
||||
assert targets("- x\n ```\n [[a]]\n ```\n[[b]]\n") == ["b"]
|
||||
assert targets("- a\n\t```\n\t[[a]]\n\t```\n[[b]]\n") == ["b"]
|
||||
assert targets("> ```\n> [[a]]\n> ```\n[[b]]\n") == ["b"]
|
||||
assert targets("> - ```\n> [[a]]\n") == []
|
||||
assert targets("1. ```\n [[a]]\n## H\n[[b]]\n") == ["b"]
|
||||
assert targets("> ```\n> [[a]]\n\nparagraph [[b]]\n") == ["b"]
|
||||
assert targets("```\n[[a]]\n- x\n[[b]]\n") == []
|
||||
assert targets(" ```\n[[a]]\n") == ["a"]
|
||||
|
||||
|
||||
def test_large_input_is_fast() -> None:
|
||||
units = ["[", "`", "<!--", "> ", "- ", "\\", "\n", "[[", "]]", "> - ", "- > "]
|
||||
texts = [unit * (50_000 // len(unit)) + "\n[[a]]" for unit in units]
|
||||
texts.append("- " * 5_000 + "\n" * 50_000)
|
||||
texts.append("\n".join(" " * n + "- x" for n in range(500)))
|
||||
start = time.perf_counter()
|
||||
for text in texts:
|
||||
find_wikilinks(text)
|
||||
# Takes about a second; quadratic behaviour would take minutes.
|
||||
assert time.perf_counter() - start < 30
|
||||
Reference in new issue
Block a user