From ee970172fe3868b5185c43ad5c3a2b3e160f0fd7 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Mon, 12 Sep 2022 17:31:35 +0200 Subject: [PATCH 1/7] Fixed redirects for computed destinations and URLs --- mkdocs_redirects/plugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mkdocs_redirects/plugin.py b/mkdocs_redirects/plugin.py index 25d5f36..12973b3 100644 --- a/mkdocs_redirects/plugin.py +++ b/mkdocs_redirects/plugin.py @@ -55,7 +55,6 @@ def get_relative_html_path(old_page, new_page, use_directory_urls): """Return the relative path from the old html path to the new html path""" old_path = get_html_path(old_page, use_directory_urls) new_path, new_hash_fragment = _split_hash_fragment(new_page) - new_path = get_html_path(new_path, use_directory_urls) if use_directory_urls: # remove /index.html from end of path @@ -136,7 +135,8 @@ def on_post_build(self, config, **kwargs): dest_path = page_new elif page_new_without_hash in self.doc_pages: - dest_path = get_relative_html_path(page_old, page_new, use_directory_urls) + file = self.doc_pages[page_new_without_hash] + dest_path = get_relative_html_path(page_old, file.url, use_directory_urls) # If the redirect target isn't external or a valid internal page, throw an error # Note: we use 'warn' here specifically; mkdocs treats warnings specially when in strict mode From f1f4d7d0fed040b1ff02771d7b6e70eff72cf73b Mon Sep 17 00:00:00 2001 From: squidfunk Date: Tue, 13 Sep 2022 14:43:49 +0200 Subject: [PATCH 2/7] Fix tests --- mkdocs_redirects/plugin.py | 4 ++-- tests/test_plugin.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/mkdocs_redirects/plugin.py b/mkdocs_redirects/plugin.py index 12973b3..fc41c27 100644 --- a/mkdocs_redirects/plugin.py +++ b/mkdocs_redirects/plugin.py @@ -128,7 +128,7 @@ def on_post_build(self, config, **kwargs): # Walk through the redirect map and write their HTML files for page_old, page_new in self.redirects.items(): # Need to remove hash fragment from new page to verify existence - page_new_without_hash, _ = _split_hash_fragment(str(page_new)) + page_new_without_hash, hash = _split_hash_fragment(str(page_new)) # External redirect targets are easy, just use it as the target path if page_new.lower().startswith(('http://', 'https://')): @@ -136,7 +136,7 @@ def on_post_build(self, config, **kwargs): elif page_new_without_hash in self.doc_pages: file = self.doc_pages[page_new_without_hash] - dest_path = get_relative_html_path(page_old, file.url, use_directory_urls) + dest_path = get_relative_html_path(page_old, f"{file.url}{hash}", use_directory_urls) # If the redirect target isn't external or a valid internal page, throw an error # Note: we use 'warn' here specifically; mkdocs treats warnings specially when in strict mode diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 57466f7..e39b427 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -4,7 +4,8 @@ """ import pytest -from mkdocs_redirects.plugin import get_relative_html_path +from mkdocs_redirects.plugin import get_relative_html_path, _split_hash_fragment +from mkdocs.structure.files import File @pytest.mark.parametrize( @@ -29,7 +30,9 @@ ], ) def test_relative_redirect_directory_urls(old_page, new_page, expected): - result = get_relative_html_path(old_page, new_page, use_directory_urls=True) + page_new_without_hash, hash = _split_hash_fragment(new_page) + file = File(page_new_without_hash, ".", ".", True) + result = get_relative_html_path(old_page, f"{file.url}{hash}", use_directory_urls=True) assert result == expected @@ -60,6 +63,8 @@ def test_relative_redirect_directory_urls(old_page, new_page, expected): ], ) def test_relative_redirect_no_directory_urls(old_page, new_page, expected): - result = get_relative_html_path(old_page, new_page, use_directory_urls=False) + page_new_without_hash, hash = _split_hash_fragment(new_page) + file = File(page_new_without_hash, ".", ".", False) + result = get_relative_html_path(old_page, f"{file.url}{hash}", use_directory_urls=False) assert result == expected From 472cbdbaa779246832467b52b8d3b1a4878d30a7 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Tue, 13 Sep 2022 14:45:45 +0200 Subject: [PATCH 3/7] Fix Python 2.7 --- mkdocs_redirects/plugin.py | 2 +- tests/test_plugin.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mkdocs_redirects/plugin.py b/mkdocs_redirects/plugin.py index fc41c27..669dcb7 100644 --- a/mkdocs_redirects/plugin.py +++ b/mkdocs_redirects/plugin.py @@ -136,7 +136,7 @@ def on_post_build(self, config, **kwargs): elif page_new_without_hash in self.doc_pages: file = self.doc_pages[page_new_without_hash] - dest_path = get_relative_html_path(page_old, f"{file.url}{hash}", use_directory_urls) + dest_path = get_relative_html_path(page_old, "".join([file.url, hash]), use_directory_urls) # If the redirect target isn't external or a valid internal page, throw an error # Note: we use 'warn' here specifically; mkdocs treats warnings specially when in strict mode diff --git a/tests/test_plugin.py b/tests/test_plugin.py index e39b427..c3cdc90 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -32,7 +32,7 @@ def test_relative_redirect_directory_urls(old_page, new_page, expected): page_new_without_hash, hash = _split_hash_fragment(new_page) file = File(page_new_without_hash, ".", ".", True) - result = get_relative_html_path(old_page, f"{file.url}{hash}", use_directory_urls=True) + result = get_relative_html_path(old_page, "".join([file.url, hash]), use_directory_urls=True) assert result == expected @@ -65,6 +65,6 @@ def test_relative_redirect_directory_urls(old_page, new_page, expected): def test_relative_redirect_no_directory_urls(old_page, new_page, expected): page_new_without_hash, hash = _split_hash_fragment(new_page) file = File(page_new_without_hash, ".", ".", False) - result = get_relative_html_path(old_page, f"{file.url}{hash}", use_directory_urls=False) + result = get_relative_html_path(old_page, "".join([file.url, hash]), use_directory_urls=False) assert result == expected From 0afdf03cfc6cceae1047bbff5439d325c18163ff Mon Sep 17 00:00:00 2001 From: Martin Donath Date: Sun, 18 Sep 2022 10:21:03 +0200 Subject: [PATCH 4/7] Update mkdocs_redirects/plugin.py Co-authored-by: Oleh Prypin --- mkdocs_redirects/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs_redirects/plugin.py b/mkdocs_redirects/plugin.py index 669dcb7..12bdf12 100644 --- a/mkdocs_redirects/plugin.py +++ b/mkdocs_redirects/plugin.py @@ -136,7 +136,7 @@ def on_post_build(self, config, **kwargs): elif page_new_without_hash in self.doc_pages: file = self.doc_pages[page_new_without_hash] - dest_path = get_relative_html_path(page_old, "".join([file.url, hash]), use_directory_urls) + dest_path = get_relative_html_path(page_old, file.url + hash, use_directory_urls) # If the redirect target isn't external or a valid internal page, throw an error # Note: we use 'warn' here specifically; mkdocs treats warnings specially when in strict mode From c2fc2b35e19a2275b9af66e85bc49aa093743227 Mon Sep 17 00:00:00 2001 From: Martin Donath Date: Sun, 18 Sep 2022 10:21:09 +0200 Subject: [PATCH 5/7] Update tests/test_plugin.py Co-authored-by: Oleh Prypin --- tests/test_plugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index c3cdc90..885bb3b 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -31,8 +31,8 @@ ) def test_relative_redirect_directory_urls(old_page, new_page, expected): page_new_without_hash, hash = _split_hash_fragment(new_page) - file = File(page_new_without_hash, ".", ".", True) - result = get_relative_html_path(old_page, "".join([file.url, hash]), use_directory_urls=True) + file = File(page_new_without_hash, "docs", "site", True) + result = get_relative_html_path(old_page, file.url + hash, use_directory_urls=True) assert result == expected From a2abf985e1739edc5403bb0c32c633ad91a756cb Mon Sep 17 00:00:00 2001 From: Martin Donath Date: Sun, 18 Sep 2022 10:21:17 +0200 Subject: [PATCH 6/7] Update tests/test_plugin.py Co-authored-by: Oleh Prypin --- tests/test_plugin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 885bb3b..89759a6 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -4,9 +4,10 @@ """ import pytest -from mkdocs_redirects.plugin import get_relative_html_path, _split_hash_fragment from mkdocs.structure.files import File +from mkdocs_redirects.plugin import _split_hash_fragment, get_relative_html_path + @pytest.mark.parametrize( ["old_page", "new_page", "expected"], From 3269e78429d61993efaa79802451a5dca92224ce Mon Sep 17 00:00:00 2001 From: Martin Donath Date: Sun, 18 Sep 2022 10:21:29 +0200 Subject: [PATCH 7/7] Update tests/test_plugin.py Co-authored-by: Oleh Prypin --- tests/test_plugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 89759a6..1a2ba4a 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -65,7 +65,7 @@ def test_relative_redirect_directory_urls(old_page, new_page, expected): ) def test_relative_redirect_no_directory_urls(old_page, new_page, expected): page_new_without_hash, hash = _split_hash_fragment(new_page) - file = File(page_new_without_hash, ".", ".", False) - result = get_relative_html_path(old_page, "".join([file.url, hash]), use_directory_urls=False) + file = File(page_new_without_hash, "docs", "site", False) + result = get_relative_html_path(old_page, file.url + hash, use_directory_urls=False) assert result == expected