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,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