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. diff --git a/mkdocs_redirects/plugin.py b/mkdocs_redirects/plugin.py index 6012a08..740d7f0 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: '%s'", 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():