From c1022ce6b48d97a8e6aadb3d824cda81473e9516 Mon Sep 17 00:00:00 2001 From: Cal <35017184+CallumWalley@users.noreply.github.com> Date: Sun, 12 Nov 2023 13:15:55 +1300 Subject: [PATCH 1/5] Add map_file argument --- mkdocs_redirects/plugin.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mkdocs_redirects/plugin.py b/mkdocs_redirects/plugin.py index 6012a08..fe744a3 100644 --- a/mkdocs_redirects/plugin.py +++ b/mkdocs_redirects/plugin.py @@ -73,11 +73,22 @@ class RedirectPlugin(BasePlugin): # Any options that this plugin supplies should go here. config_scheme = ( ("redirect_maps", config_options.Type(dict, default={})), # note the trailing comma + ("map_file", config_options.Type(str)), ) # Build a list of redirects on file generation def on_files(self, files, config, **kwargs): - self.redirects = self.config.get("redirect_maps", {}) + if self.config.get('map_file'): + filename = self.config.get('map_file') + if os.path.isfile(filename): + with open(filename) as f: + self.redirects = utils.yaml_load(f) + log.debug("Loading yaml file: ", filename) + else: + log.warning("yaml configuration file '%s' was not found!", filename) + # If no mapfile, fall back to regular method. + if not hasattr(self, 'redirects'): + self.redirects = self.config.get('redirect_maps', {}) # Validate user-provided redirect "old files" for page_old in self.redirects.keys(): From cce743d06f1d243fb1e78b6ede21b07f0dc59f29 Mon Sep 17 00:00:00 2001 From: Cal <35017184+CallumWalley@users.noreply.github.com> Date: Sun, 12 Nov 2023 13:20:55 +1300 Subject: [PATCH 2/5] Added map_file example --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 3ec1ae6..1e17523 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,16 @@ plugins: 'some_file.md': 'http://external.url.com/foobar' ``` +Alternatively, get the redirects from a seperate `yml` file with `map_file`. + +```yaml +plugins: + - redirects: + map_file: 'redirect_map.yml' +``` + +Where the `map_file` uses the same syntax as above. + > **Note** > Don't forget that specifying the `plugins` setting will override the defaults if you didn't already have it set! See [this page](https://www.mkdocs.org/user-guide/configuration/#plugins) for more information. From 1c843fcb8039deb3245f603ba6acfe600b18db25 Mon Sep 17 00:00:00 2001 From: Cal <35017184+CallumWalley@users.noreply.github.com> Date: Sun, 12 Nov 2023 14:18:56 +1300 Subject: [PATCH 3/5] Fix hatch identified issue. --- 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 fe744a3..740d7f0 100644 --- a/mkdocs_redirects/plugin.py +++ b/mkdocs_redirects/plugin.py @@ -83,7 +83,7 @@ def on_files(self, files, config, **kwargs): if os.path.isfile(filename): with open(filename) as f: self.redirects = utils.yaml_load(f) - log.debug("Loading yaml file: ", filename) + log.debug("Loading yaml file: '%s'", filename) else: log.warning("yaml configuration file '%s' was not found!", filename) # If no mapfile, fall back to regular method. From bc7a53186568aef2867014e7ba91aac995a297ac Mon Sep 17 00:00:00 2001 From: Cal <35017184+CallumWalley@users.noreply.github.com> Date: Mon, 24 Nov 2025 14:29:18 +1300 Subject: [PATCH 4/5] Hatch suggestion --- 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 79fa1bc..4fd390d 100644 --- a/mkdocs_redirects/plugin.py +++ b/mkdocs_redirects/plugin.py @@ -80,7 +80,7 @@ def on_files(self, files, config, **kwargs): if self.config.get('map_file'): filename = self.config.get('map_file') if os.path.isfile(filename): - with open(filename) as f: + with open(filename, encoding=locale.getpreferredencoding(False)) as f: self.redirects = utils.yaml_load(f) log.debug("Loading yaml file: '%s'", filename) else: From ff797e75e48e88ead0bb705ae601330c27912efa Mon Sep 17 00:00:00 2001 From: "callumnmw@gmail.com" Date: Mon, 24 Nov 2025 15:20:38 +1300 Subject: [PATCH 5/5] HATCH FIXES --- mkdocs_redirects/plugin.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mkdocs_redirects/plugin.py b/mkdocs_redirects/plugin.py index 4fd390d..4577e48 100644 --- a/mkdocs_redirects/plugin.py +++ b/mkdocs_redirects/plugin.py @@ -2,6 +2,7 @@ Copyright 2019-2022 DataRobot, Inc. and its affiliates. All rights reserved. """ +import locale import logging import os import posixpath @@ -77,8 +78,8 @@ class RedirectPlugin(BasePlugin): # Build a list of redirects on file generation def on_files(self, files, config, **kwargs): - if self.config.get('map_file'): - filename = self.config.get('map_file') + if self.config.get("map_file"): + filename = self.config.get("map_file") if os.path.isfile(filename): with open(filename, encoding=locale.getpreferredencoding(False)) as f: self.redirects = utils.yaml_load(f) @@ -86,8 +87,8 @@ def on_files(self, files, config, **kwargs): else: log.warning("yaml configuration file '%s' was not found!", filename) # If no mapfile, fall back to regular method. - if not hasattr(self, 'redirects'): - self.redirects = self.config.get('redirect_maps', {}) + if not hasattr(self, "redirects"): + self.redirects = self.config.get("redirect_maps", {}) # Validate user-provided redirect "old files" for page_old in self.redirects: