Skip to content

Commit

Permalink
feat(toolkit): Splitting text on role
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Feb 24, 2024
1 parent 77fb7cf commit ba85025
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/rst_package_refs/toolkit.py
Original file line number Diff line number Diff line change
@@ -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>``
* ``target``
"""
matched = re.match(r"^(?P<display>.+) <(?P<target>.+)>$", source)
if matched:
return matched.group("display"), matched.group("target")
return source, source
20 changes: 20 additions & 0 deletions tests/test_toolkit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest
from rst_package_refs import toolkit


@pytest.mark.parametrize(
"text,display_text,target_text",
[
("text", "text", "text"),
("text <target", "text <target", "text <target"),
("text target>", "text target>", "text target>"),
("text<target>", "text<target>", "text<target>"),
("text <target>", "text", "target"),
("text <<target>>", "text", "<target>"),
("text <><target>", "text", "><target"),
],
)
def test_split_text(text, display_text, target_text):
display, target = toolkit.split_text(text)
assert display == display_text
assert target == target_text

0 comments on commit ba85025

Please sign in to comment.