Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hover text #10

Merged
merged 1 commit into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/mkdocs_autorefs/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from markdown.util import INLINE_PLACEHOLDER_RE

AUTO_REF_RE = re.compile(
r"<span data-(?P<kind>autorefs-identifier|autorefs-optional|mkdocstrings-identifier)="
r"<span data-(?P<kind>autorefs-identifier|autorefs-optional|autorefs-optional-hover)="
r'("?)(?P<identifier>[^"<>]*)\2>(?P<title>.*?)</span>'
)
"""A regular expression to match mkdocs-autorefs' special reference markers
Expand Down Expand Up @@ -150,17 +150,22 @@ def fix_ref(url_mapper: Callable[[str], str], unmapped: List[str]) -> Callable:
def inner(match: Match):
identifier = match["identifier"]
title = match["title"]
kind = match["kind"]

try:
url = url_mapper(unescape(identifier))
except KeyError:
if match["kind"] == "autorefs-optional":
if kind == "autorefs-optional":
return title
elif kind == "autorefs-optional-hover":
return f'<span title="{identifier}">{title}</span>'
unmapped.append(identifier)
if title == identifier:
return f"[{identifier}][]"
return f"[{title}][{identifier}]"

if kind == "autorefs-optional-hover":
return f'<a title="{identifier}" href="{escape(url)}">{title}</a>'
return f'<a href="{escape(url)}">{title}</a>'

return inner
Expand Down
9 changes: 9 additions & 0 deletions tests/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,12 @@ def test_custom_optional_reference():
output, unmapped = fix_refs(source, url_map.__getitem__)
assert output == 'foo <a href="ok.html#ok">ok</a>'
assert unmapped == []


def test_custom_optional_hover_reference():
"""Check that optional-hover HTML-based references are expanded and never reported missing."""
url_map = {"ok": "ok.html#ok"}
source = '<span data-autorefs-optional-hover="bar">foo</span> <span data-autorefs-optional-hover=ok>ok</span>'
output, unmapped = fix_refs(source, url_map.__getitem__)
assert output == '<span title="bar">foo</span> <a title="ok" href="ok.html#ok">ok</a>'
assert unmapped == []