Skip to content

Commit

Permalink
Format code with black+isort
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Aug 21, 2022
1 parent 52f95e0 commit 707ceac
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 54 deletions.
2 changes: 0 additions & 2 deletions .flake8

This file was deleted.

28 changes: 16 additions & 12 deletions mkdocs_redirects/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
"""
<!doctype html>
<html lang="en">
Expand All @@ -48,11 +47,12 @@ def write_html(site_dir, old_path, new_path):
</body>
</html>
"""
).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)
Expand All @@ -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]

Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[flake8]
max-line-length = 119
extend-ignore = E203

[isort]
profile = black
line_length = 100
87 changes: 47 additions & 40 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 707ceac

Please sign in to comment.