diff --git a/mkdocs_literate_nav/plugin.py b/mkdocs_literate_nav/plugin.py index 773b2a5..807ac48 100644 --- a/mkdocs_literate_nav/plugin.py +++ b/mkdocs_literate_nav/plugin.py @@ -13,6 +13,11 @@ import mkdocs.structure.nav import mkdocs.structure.pages +try: + from mkdocs.exceptions import PluginError +except ImportError: + PluginError = SystemExit + from mkdocs_literate_nav import parser log = logging.getLogger(f"mkdocs.plugins.{__name__}") @@ -21,7 +26,7 @@ class LiterateNavPlugin(mkdocs.plugins.BasePlugin): config_scheme = ( - ("nav_file", mkdocs.config.config_options.Type(str, default=None)), + ("nav_file", mkdocs.config.config_options.Type(str, default="SUMMARY.md")), ("implicit_index", mkdocs.config.config_options.Type(bool, default=False)), ) @@ -60,7 +65,7 @@ def resolve_directories_in_nav( """ def read_index_of_dir(path: str) -> Optional[str]: - file = find_index_of_dir(files, path, nav_file_name) + file = files.get_file_from_path(os.path.join(path, nav_file_name)) if not file: return None log.debug(f"Navigation for {path!r} based on {file.src_path!r}.") @@ -93,24 +98,6 @@ def try_resolve_directory(path: str): return convert_strings_in_nav(nav_data, try_resolve_directory) -def find_index_of_dir( - files: mkdocs.structure.files.Files, path: str, nav_file_name: Optional[str] = None -) -> mkdocs.structure.files.File: - """Find the directory's index (or nav if specified) Markdown file. - - If `nav_file` is configured, unconditionally get that file from this dir (could be None). - Else try README.md. Else try whatever maps to the index (effectively index.md or readme.md). - """ - if nav_file_name: - return files.get_file_from_path(os.path.join(path, nav_file_name)) - f = files.get_file_from_path(os.path.join(path, "README.md")) - if f: - return f - for f in files.documentation_pages(): - if os.path.split(f.src_path)[0] == path and f.name == "index": - return f - - def convert_strings_in_nav(nav_data, converter: Callable[[str], Optional[Any]]): """Walk a nav dict and replace strings in it with the callback.""" if isinstance(nav_data, str): diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 5703857..439e382 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -21,7 +21,10 @@ def test_resolve_directories_in_nav(tmp_path_factory, use_directory_urls, golden with golden.capture_logs("mkdocs.plugins.mkdocs_literate_nav"): output = plugin.resolve_directories_in_nav( - golden.get("nav"), files, golden.get("nav_file_name"), golden.get("implicit_index") + golden.get("nav"), + files, + nav_file_name=golden.get("nav_file_name") or "index.md", + implicit_index=golden.get("implicit_index"), ) assert output == golden.out["output"] @@ -29,36 +32,3 @@ def test_resolve_directories_in_nav(tmp_path_factory, use_directory_urls, golden def _file_sort_key(f: File): parts = pathlib.PurePath(f.src_path).parts return tuple(chr(i != len(parts) - 1) + chr(f.name != "index") + p for i, p in enumerate(parts)) - - -@pytest.mark.parametrize("use_directory_urls", [True, False]) -def test_find_index_of_dir(tmp_path_factory, use_directory_urls): - src_dir, dest_dir = map(tmp_path_factory.mktemp, ["src", "dest"]) - file_names = [ - " README.md", - "README.md ", - "index.md", - "bad/index.html", - "bad/foo.md", - "bad/foo.index.md", - "both1/index.md", - "both1/README.md", - "both2/readme.md", - "both2/index.md", - "a/a/a/a/a/README.md", - ] - files = {f: File(f, src_dir, dest_dir, use_directory_urls) for f in file_names} - files_o = Files(list(files.values())) - - assert plugin.find_index_of_dir(files_o, "") == files["index.md"] - assert plugin.find_index_of_dir(files_o, "nonexistent") is None - assert plugin.find_index_of_dir(files_o, "bad") is None - assert plugin.find_index_of_dir(files_o, "bad/foo") is None - assert plugin.find_index_of_dir(files_o, "bad/foo.md") is None - assert plugin.find_index_of_dir(files_o, "both1") == files["both1/README.md"] - assert plugin.find_index_of_dir(files_o, "both2") == files["both2/index.md"] - assert plugin.find_index_of_dir(files_o, "bad", "foo.md") == files["bad/foo.md"] - assert plugin.find_index_of_dir(files_o, "both2", "foo.md") is None - assert plugin.find_index_of_dir(files_o, "a/a/a/a", "a") is None - assert plugin.find_index_of_dir(files_o, "a/a/a/a/a") == files["a/a/a/a/a/README.md"] - assert plugin.find_index_of_dir(files_o, "a/a/a/a/a/a") is None