Skip to content

Commit

Permalink
Skip nav file from wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed May 17, 2021
1 parent 3d7c6da commit 37b7c23
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
11 changes: 7 additions & 4 deletions mkdocs_literate_nav/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
class NavParser:
def __init__(
self,
get_md_for_root: Callable[[str], Optional[str]],
get_nav_for_dir: Callable[[str], Optional[Tuple[str, str]]],
globber,
implicit_index: bool = False,
):
self.get_md_for_root = get_md_for_root
self.get_nav_for_dir = get_nav_for_dir
self.globber = globber
self.implicit_index = implicit_index

Expand All @@ -43,9 +43,12 @@ def markdown_to_nav(self, roots: RootStack = ("",)) -> Nav:
raise RecursionError(f"Disallowing recursion {rec}")
root = roots[0]
ext = _MarkdownExtension()
md = self.get_md_for_root(root)
if md:
dir_nav = self.get_nav_for_dir(root)
if dir_nav:
nav_file_name, md = dir_nav
markdown.markdown(md, extensions=[ext])
if ext.nav:
seen_items.add(posixpath.normpath(posixpath.join(root, nav_file_name)))
first_item = None
if ext.nav and self.implicit_index:
first_item = self.globber.find_index(root)
Expand Down
15 changes: 6 additions & 9 deletions mkdocs_literate_nav/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import os.path
import posixpath
from typing import Any, Callable, Iterable, Iterator, Optional, Union
from typing import Any, Callable, Iterable, Iterator, Optional, Tuple, Union

import glob2
import mkdocs.config
Expand Down Expand Up @@ -53,18 +53,15 @@ def on_nav(


def resolve_directories_in_nav(
nav_data,
files: mkdocs.structure.files.Files,
nav_file_name: Optional[str],
implicit_index: bool,
nav_data, files: mkdocs.structure.files.Files, nav_file_name: str, implicit_index: bool
):
"""Walk through a standard MkDocs nav config and replace `directory/` references.
Directories, if found, are resolved by the rules of literate nav insertion:
If it has a literate nav file, that is used. Otherwise an implicit nav is generated.
"""

def read_index_of_dir(path: str) -> Optional[str]:
def get_nav_for_dir(path: str) -> Optional[Tuple[str, str]]:
file = files.get_file_from_path(os.path.join(path, nav_file_name))
if not file:
return None
Expand All @@ -77,10 +74,10 @@ def read_index_of_dir(path: str) -> Optional[str]:

# https://github.com/mkdocs/mkdocs/blob/fa5aa4a26e/mkdocs/structure/pages.py#L120
with open(file.abs_src_path, encoding="utf-8-sig") as f:
return f.read()
return nav_file_name, f.read()

globber = MkDocsGlobber(files)
nav_parser = parser.NavParser(read_index_of_dir, globber, implicit_index=implicit_index)
nav_parser = parser.NavParser(get_nav_for_dir, globber, implicit_index=implicit_index)

def try_resolve_directory(path: str):
if path.endswith("/"):
Expand All @@ -89,7 +86,7 @@ def try_resolve_directory(path: str):
return nav_parser.markdown_to_nav((path,))

# If nav file is present in the root dir, discard the pre-existing nav.
if read_index_of_dir("") or not nav_data:
if not nav_data or get_nav_for_dir(""):
return try_resolve_directory("/")
return convert_strings_in_nav(nav_data, try_resolve_directory)

Expand Down
1 change: 0 additions & 1 deletion tests/nav/wildcard/test_basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ files:
item2.md:
section2/item.md:
output:
- null: SUMMARY.md
- null: item1.md
- null: item2.md
- Section2:
Expand Down
1 change: 0 additions & 1 deletion tests/nav/wildcard/test_dirs_then_files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ files:
output:
- Section2:
- null: section2/item.md
- null: SUMMARY.md
- null: item1.md
- null: item2.md
3 changes: 1 addition & 2 deletions tests/nav/wildcard/test_no_files.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
files:
SUMMARY.md: |
- *
output:
- null: SUMMARY.md
output: []
1 change: 0 additions & 1 deletion tests/nav/wildcard/test_no_repeat_with_directory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ files:
b.md:
sub/index.md:
output:
- null: SUMMARY.md
- null: b.md
- Sub:
- null: sub/index.md
Expand Down

0 comments on commit 37b7c23

Please sign in to comment.