diff --git a/src/rst_package_refs/toolkit.py b/src/rst_package_refs/toolkit.py new file mode 100644 index 0000000..94b5110 --- /dev/null +++ b/src/rst_package_refs/toolkit.py @@ -0,0 +1,17 @@ +"""Toolkit to create original custom role.""" +import re +from typing import Tuple + + +def split_text(source: str) -> Tuple[str, str]: + """Split from content to displaying text and target text. + + Support text patterns: + + * ``text `` + * ``target`` + """ + matched = re.match(r"^(?P.+) <(?P.+)>$", source) + if matched: + return matched.group("display"), matched.group("target") + return source, source diff --git a/tests/test_toolkit.py b/tests/test_toolkit.py new file mode 100644 index 0000000..8abfea1 --- /dev/null +++ b/tests/test_toolkit.py @@ -0,0 +1,20 @@ +import pytest +from rst_package_refs import toolkit + + +@pytest.mark.parametrize( + "text,display_text,target_text", + [ + ("text", "text", "text"), + ("text ", "text target>", "text target>"), + ("text", "text", "text"), + ("text ", "text", "target"), + ("text <>", "text", ""), + ("text <>", "text", ">