Skip to content

Commit

Permalink
fix: Fix error when a digit immediately follows a code tag
Browse files Browse the repository at this point in the history
Fixes #169.
PR #175.

Co-authored-by: Timothée Mazzucotelli <pawamoy@pm.me>
  • Loading branch information
oprypin and pawamoy committed Nov 3, 2020
1 parent 7c0715e commit 9b92341
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/mkdocstrings/references.py
Expand Up @@ -50,7 +50,7 @@ def store(self, value: str) -> str:
Returns:
The ID under which the text is stored.
"""
new_id = f"{self.seed}{len(self._store)}"
new_id = f"\x02{self.seed}{len(self._store)}\x03"
self._store.append(value)
return new_id

Expand Down Expand Up @@ -78,7 +78,7 @@ def restore_code_tags(self, soup_str: str) -> str:
Returns:
The same HTML text with placeholders replaced by their respective original code nodes.
"""
return re.sub(rf"{self.seed}(\d+)", self._replace_id_with_value, soup_str)
return re.sub(rf"\x02{self.seed}(\d+)\x03", self._replace_id_with_value, soup_str)

def _replace_id_with_value(self, match):
return self._store[int(match.group(1))]
Expand Down
21 changes: 20 additions & 1 deletion tests/test_references.py
@@ -1,7 +1,8 @@
"""Tests for the references module."""
import pytest
from bs4 import BeautifulSoup

from mkdocstrings.references import relative_url
from mkdocstrings.references import Placeholder, relative_url


@pytest.mark.parametrize(
Expand Down Expand Up @@ -34,3 +35,21 @@ def test_relative_url(current_url, to_url, href_url):
href_url: The relative URL to put in the `href` HTML field.
"""
assert relative_url(current_url, to_url) == href_url


@pytest.mark.parametrize("html", ["foo<code>code content</code>4"])
def test_placeholder(html):
"""
Test the references "fixing" mechanism.
Arguments:
html: HTML contents in which to fix references.
"""
placeholder = Placeholder()

soup = BeautifulSoup(html, "html.parser")
placeholder.replace_code_tags(soup)
html_with_placeholders = str(soup).replace("code content", "should not be replaced")

html_restored = placeholder.restore_code_tags(html_with_placeholders)
assert html == html_restored

0 comments on commit 9b92341

Please sign in to comment.