Skip to content

Commit

Permalink
Fix Bug that caused wrong text references
Browse files Browse the repository at this point in the history
This commit fixes a bug in the post processing which caused
ids which contain other ids to be replaced with the wrong text.
  • Loading branch information
tobiasah committed Aug 9, 2024
1 parent e258f4c commit ebdc5d7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Version 1.2.1

* Fix bug in the post processing which cause references with ids which contain other ids
to be replaced with the wrong text.

## Version 1.2.0

* Allow automatic link text generation across different pages.
Expand Down
6 changes: 3 additions & 3 deletions src/mkdocs_caption/post_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def register_target(self, identifier: str, text: str, page: Page) -> None:
"{page_title}",
page.title,
).replace("{local_ref}", text)
self._regex_to_apply[rf"{page.file.src_path[:-3]}/#{identifier}"] = (
self._regex_to_apply[rf'{page.file.src_path[:-3]}/#{identifier}"'] = (
re.compile(
rf"({page.file.src_path[:-3]}/#{identifier}.*?>)(</a>)",
rf'({page.file.src_path[:-3]}/#{identifier}".*?>)(</a>)',
flags=re.MULTILINE | re.DOTALL,
),
rf"\1{target_text}\2",
Expand All @@ -48,7 +48,7 @@ def register_target(self, identifier: str, text: str, page: Page) -> None:
self._local_regex[page.file.src_uri].append(
(
re.compile(
rf"(\"#{identifier}.*?>)(</a>)",
rf'("#{identifier}".*?>)(</a>)',
flags=re.MULTILINE | re.DOTALL,
),
rf"\1{text}\2",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def test_postprocess_default_reference(dummy_page):
page=page,
post_processor=post_processor,
)
assert "test/#_list-1" in post_processor.regex_to_apply
assert 'test/#_list-1"' in post_processor.regex_to_apply


def test_postprocess_ignore_reference_with_text(dummy_page):
Expand Down Expand Up @@ -499,7 +499,7 @@ def test_postprocess_custom_reference(dummy_page):
page=page,
post_processor=post_processor,
)
assert "test/#_list-1" in post_processor.regex_to_apply
assert 'test/#_list-1"' in post_processor.regex_to_apply


def test_custom_caption_no_target(caplog, dummy_page):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def test_postprocess_default_reference(dummy_page):
page=dummy_page,
post_processor=post_processor,
)
assert "test/#_figure-1" in post_processor.regex_to_apply
assert 'test/#_figure-1"' in post_processor.regex_to_apply


def test_postprocess_ignore_reference_with_text(dummy_page):
Expand Down Expand Up @@ -557,7 +557,7 @@ def test_postprocess_custom_reference(dummy_page):
page=dummy_page,
post_processor=post_processor,
)
assert "test/#_figure-1" in post_processor.regex_to_apply
assert 'test/#_figure-1"' in post_processor.regex_to_apply


def test_figure_caption_with_no_img(caplog, dummy_page):
Expand Down
12 changes: 11 additions & 1 deletion tests/test_post_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def test_post_processor_register(dummy_page):
post_processor = PostProcessor()
post_processor.register_target("identifier", "text", dummy_page)
assert "test/#identifier" in post_processor.regex_to_apply
assert 'test/#identifier"' in post_processor.regex_to_apply
assert dummy_page.file.src_uri in post_processor._local_regex # noqa: SLF001


Expand Down Expand Up @@ -49,3 +49,13 @@ def test_post_processor_replace_existing_text(dummy_page):
post_processor.post_process(dummy_page, content)
== '<a href="test/#identifier">hello</a>'
)


def test_post_processor_similar_tag(dummy_page):
post_processor = PostProcessor()
post_processor.register_target("test", "wrong", dummy_page)
post_processor.register_target("test2", "right", dummy_page)
content = '<a href="#test2"></a>'
assert (
post_processor.post_process(dummy_page, content) == '<a href="#test2">right</a>'
)
4 changes: 2 additions & 2 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def test_postprocess_default_reference(dummy_page):
post_processor=post_processor,
)

assert "test/#_table-1" in post_processor.regex_to_apply
assert 'test/#_table-1"' in post_processor.regex_to_apply


def test_postprocess_ignore_reference_with_text(dummy_page):
Expand Down Expand Up @@ -447,7 +447,7 @@ def test_postprocess_custom_reference(dummy_page):
post_processor=post_processor,
)

assert "test/#_table-1" in post_processor.regex_to_apply
assert 'test/#_table-1"' in post_processor.regex_to_apply


def test_colgroups(dummy_page):
Expand Down

0 comments on commit ebdc5d7

Please sign in to comment.