diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 6deafc2..0000000 --- a/.flake8 +++ /dev/null @@ -1,2 +0,0 @@ -[flake8] -max-line-length = 120 diff --git a/mkdocs_redirects/plugin.py b/mkdocs_redirects/plugin.py index bd3dfe1..ece6a2d 100644 --- a/mkdocs_redirects/plugin.py +++ b/mkdocs_redirects/plugin.py @@ -16,7 +16,7 @@ def write_html(site_dir, old_path, new_path): - """ Write an HTML file in the site_dir with a meta redirect to the new page """ + """Write an HTML file in the site_dir with a meta redirect to the new page""" # Determine all relevant paths old_path_abs = os.path.join(site_dir, old_path) old_dir = os.path.dirname(old_path) @@ -29,9 +29,8 @@ def write_html(site_dir, old_path, new_path): # Write the HTML redirect file in place of the old file with open(old_path_abs, 'w', encoding='utf-8') as f: - log.debug("Creating redirect: '%s' -> '%s'", - old_path, new_path) - f.write(textwrap.dedent( + log.debug("Creating redirect: '%s' -> '%s'", old_path, new_path) + content = textwrap.dedent( """ @@ -48,11 +47,12 @@ def write_html(site_dir, old_path, new_path): """ - ).format(url=new_path)) + ).format(url=new_path) + f.write(content) 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""" + """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) @@ -70,7 +70,7 @@ def get_relative_html_path(old_page, new_page, use_directory_urls): def get_html_path(path, use_directory_urls): - """ Return the HTML file path for a given markdown file """ + """Return the HTML file path for a given markdown file""" parent, filename = posixpath.split(path) name_orig = posixpath.splitext(filename)[0] @@ -105,8 +105,10 @@ def on_files(self, files, config, **kwargs): # SHIM! Produce a warning if the old root-level 'redirects' config is present if config.get('redirects'): - log.warn("The root-level 'redirects:' setting is not valid and has been changed in version 1.0! " - "The plugin-level 'redirect-map' must be used instead. See https://git.io/fjdBN") + log.warn( + "The root-level 'redirects:' setting is not valid and has been changed in version 1.0! " + "The plugin-level 'redirect-map' must be used instead. See https://git.io/fjdBN" + ) # Validate user-provided redirect "old files" for page_old in self.redirects.keys(): @@ -143,9 +145,11 @@ def on_post_build(self, config, **kwargs): continue # DO IT! - write_html(config['site_dir'], - get_html_path(page_old, use_directory_urls), - dest_path) + write_html( + config['site_dir'], + get_html_path(page_old, use_directory_urls), + dest_path, + ) def _split_hash_fragment(path): diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..9ed7937 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,7 @@ +[flake8] +max-line-length = 119 +extend-ignore = E203 + +[isort] +profile = black +line_length = 100 diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 514d0b7..57466f7 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -3,55 +3,62 @@ All rights reserved. """ import pytest + from mkdocs_redirects.plugin import get_relative_html_path -@pytest.mark.parametrize(["old_page", "new_page", "expected"], [ - ("old.md", "index.md", "../"), - ("old.md", "README.md", "../"), - ("old.md", "new.md", "../new/"), - ("old.md", "new/index.md", "../new/"), - ("old.md", "new/README.md", "../new/"), - ("foo/old.md", "foo/new.md", "../new/"), - ("foo/fizz/old.md", "foo/bar/new.md", "../../bar/new/"), - ("fizz/old.md", "foo/bar/new.md", "../../foo/bar/new/"), - ("old.md", "index.md#hash", "../#hash"), - ("old.md", "README.md#hash", "../#hash"), - ("old.md", "new.md#hash", "../new/#hash"), - ("old.md", "new/index.md#hash", "../new/#hash"), - ("old.md", "new/README.md#hash", "../new/#hash"), - ("foo/old.md", "foo/new.md#hash", "../new/#hash"), - ("foo/fizz/old.md", "foo/bar/new.md#hash", "../../bar/new/#hash"), - ("fizz/old.md", "foo/bar/new.md#hash", "../../foo/bar/new/#hash"), -]) +@pytest.mark.parametrize( + ["old_page", "new_page", "expected"], + [ + ("old.md", "index.md", "../"), + ("old.md", "README.md", "../"), + ("old.md", "new.md", "../new/"), + ("old.md", "new/index.md", "../new/"), + ("old.md", "new/README.md", "../new/"), + ("foo/old.md", "foo/new.md", "../new/"), + ("foo/fizz/old.md", "foo/bar/new.md", "../../bar/new/"), + ("fizz/old.md", "foo/bar/new.md", "../../foo/bar/new/"), + ("old.md", "index.md#hash", "../#hash"), + ("old.md", "README.md#hash", "../#hash"), + ("old.md", "new.md#hash", "../new/#hash"), + ("old.md", "new/index.md#hash", "../new/#hash"), + ("old.md", "new/README.md#hash", "../new/#hash"), + ("foo/old.md", "foo/new.md#hash", "../new/#hash"), + ("foo/fizz/old.md", "foo/bar/new.md#hash", "../../bar/new/#hash"), + ("fizz/old.md", "foo/bar/new.md#hash", "../../foo/bar/new/#hash"), + ], +) def test_relative_redirect_directory_urls(old_page, new_page, expected): result = get_relative_html_path(old_page, new_page, use_directory_urls=True) assert result == expected -@pytest.mark.parametrize(["old_page", "new_page", "expected"], [ - ("old.md", "index.md", "index.html"), - ("old.md", "README.md", "index.html"), - ("old.md", "new.md", "new.html"), - ("old.md", "new/index.md", "new/index.html"), - ("old.md", "new/README.md", "new/index.html"), - ("foo/old.md", "foo/new.md", "new.html"), - ("foo/fizz/old.md", "foo/bar/new.md", "../bar/new.html"), - ("fizz/old.md", "foo/bar/new.md", "../foo/bar/new.html"), - ("foo.md", "foo/index.md", "foo/index.html"), - ("foo.md", "foo/README.md", "foo/index.html"), - ("old.md", "index.md#hash", "index.html#hash"), - ("old.md", "README.md#hash", "index.html#hash"), - ("old.md", "new.md#hash", "new.html#hash"), - ("old.md", "new/index.md#hash", "new/index.html#hash"), - ("old.md", "new/README.md#hash", "new/index.html#hash"), - ("foo/old.md", "foo/new.md#hash", "new.html#hash"), - ("foo/fizz/old.md", "foo/bar/new.md#hash", "../bar/new.html#hash"), - ("fizz/old.md", "foo/bar/new.md#hash", "../foo/bar/new.html#hash"), - ("foo.md", "foo/index.md#hash", "foo/index.html#hash"), - ("foo.md", "foo/README.md#hash", "foo/index.html#hash"), -]) +@pytest.mark.parametrize( + ["old_page", "new_page", "expected"], + [ + ("old.md", "index.md", "index.html"), + ("old.md", "README.md", "index.html"), + ("old.md", "new.md", "new.html"), + ("old.md", "new/index.md", "new/index.html"), + ("old.md", "new/README.md", "new/index.html"), + ("foo/old.md", "foo/new.md", "new.html"), + ("foo/fizz/old.md", "foo/bar/new.md", "../bar/new.html"), + ("fizz/old.md", "foo/bar/new.md", "../foo/bar/new.html"), + ("foo.md", "foo/index.md", "foo/index.html"), + ("foo.md", "foo/README.md", "foo/index.html"), + ("old.md", "index.md#hash", "index.html#hash"), + ("old.md", "README.md#hash", "index.html#hash"), + ("old.md", "new.md#hash", "new.html#hash"), + ("old.md", "new/index.md#hash", "new/index.html#hash"), + ("old.md", "new/README.md#hash", "new/index.html#hash"), + ("foo/old.md", "foo/new.md#hash", "new.html#hash"), + ("foo/fizz/old.md", "foo/bar/new.md#hash", "../bar/new.html#hash"), + ("fizz/old.md", "foo/bar/new.md#hash", "../foo/bar/new.html#hash"), + ("foo.md", "foo/index.md#hash", "foo/index.html#hash"), + ("foo.md", "foo/README.md#hash", "foo/index.html#hash"), + ], +) 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)