Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
fix: Fix error when a digit immediately follows a code tag
Fixes #169.
PR #175.
Co-authored-by: Timothée Mazzucotelli <pawamoy@pm.me>
- Loading branch information
Showing
with
22 additions
and
3 deletions.
-
+2
−2
src/mkdocstrings/references.py
-
+20
−1
tests/test_references.py
|
|
@@ -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 |
|
|
|
|
|
@@ -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))] |
|
|
|
|
|
@@ -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( |
|
|
@@ -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 |