Skip to content

Commit

Permalink
Remove glob2 dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed May 24, 2021
1 parent d54cdc1 commit 1b02eb7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
2 changes: 1 addition & 1 deletion mkdocs_literate_nav/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def markdown_to_nav(self, roots: Tuple[str, ...] = (".",)) -> Nav:
if ext.nav:
self.seen_items.add(posixpath.normpath(posixpath.join(root, nav_file_name)))
first_item = None
if ext.nav and self.implicit_index:
if ext.nav and self.implicit_index and root != ".":
first_item = self.globber.find_index(root)
if first_item:
first_item = Wildcard(root, "/" + first_item, fallback=False)
Expand Down
49 changes: 20 additions & 29 deletions mkdocs_literate_nav/plugin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import collections
import logging
import os
import os.path
import posixpath
from typing import Iterable, Iterator, Optional, Tuple
from pathlib import PurePath, PurePosixPath
from typing import Iterator, Optional, Tuple

import glob2
import mkdocs.config
import mkdocs.config.config_options
import mkdocs.plugins
Expand Down Expand Up @@ -87,43 +85,36 @@ def get_nav_for_dir(path: str) -> Optional[Tuple[str, str]]:
return result or []


class MkDocsGlobber(glob2.Globber):
class MkDocsGlobber:
def __init__(self, files: mkdocs.structure.files.Files):
self.files = set()
self.dirs = collections.defaultdict(dict)
self.files = {} # Ordered set
self.dirs = {} # Ordered set
self.index_dirs = {}
for f in files:
if not f.is_documentation_page():
continue
path = f.src_path.replace(os.sep, "/")
self.files.add(path)
tail, head = posixpath.split(path)
path = PurePosixPath("/", PurePath(f.src_path).as_posix())
self.files[path] = True
tail, head = path.parent, path.name
if f.name == "index":
self.index_dirs[tail] = path
while True:
self.dirs[tail or "."][head] = True
if not tail:
self.dirs[tail] = True
if not head:
break
tail, head = posixpath.split(tail)

def listdir(self, path: str) -> Iterable[str]:
if path not in self.dirs:
raise NotADirectoryError(path)
return self.dirs[path]

def exists(self, path: str) -> bool:
return path in self.files or path in self.dirs
tail, head = tail.parent, tail.name

def isdir(self, path: str) -> bool:
return path in self.dirs

def islink(self, path: str) -> bool:
return False
return PurePosixPath("/", path) in self.dirs

def iglob(self, *args, **kwargs) -> Iterator[str]:
for p in super().iglob(*args, **kwargs):
yield p.replace(os.sep, "/")
def glob(self, pattern: str) -> Iterator[str]:
pattern = "/" + pattern.lstrip("/")
for collection in self.files, self.dirs:
for path in collection:
if path.match(pattern):
yield str(path)[1:]

def find_index(self, root: str) -> Optional[str]:
root = PurePosixPath("/", root)
if root in self.index_dirs:
return self.index_dirs[root]
return str(self.index_dirs[root])[1:]
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ literate-nav = "mkdocs_literate_nav.plugin:LiterateNavPlugin"
[tool.poetry.dependencies]
python = "^3.6"
mkdocs = "^1.0.3"
glob2 = "^0.7"
dataclasses = {version = ">=0.7", python = "<3.7"}

[tool.poetry.dev-dependencies]
Expand Down

0 comments on commit 1b02eb7

Please sign in to comment.