Skip to content

Commit

Permalink
nav filter fix (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszPorowski authored Feb 21, 2023
1 parent 7350410 commit f02133d
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
run: poetry install --verbose --only=dev

- name: Check imports sort
run: poetry run isort --check-only .
run: poetry run isort --profile black --check-only .

- name: Check formatting
run: poetry run black --check .
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# MkDocs
site/**
docs/Contributions
docs1/
docs2/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ repos:
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
Expand Down
6 changes: 2 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
},
"python.linting.enabled": true,
"markdown.extension.toc.levels": "2..6",
"python.analysis.extraPaths": [
".venv/lib/site-packages"
],
"editor.formatOnSave": true
"editor.formatOnSave": true,
"python.formatting.provider": "black"
}
3 changes: 2 additions & 1 deletion dev.build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

rm -rf dist && rm -rf build && rm -rf site && rm -rf *.egg-info

poetry run isort .
poetry run isort --profile black .
poetry run black .
poetry run flake8 --count .
# poetry run bandit --recursive .
Expand All @@ -14,3 +14,4 @@ pip uninstall -y mkdocs_file_filter_plugin
pip install -e .

poetry run mkdocs serve --verbose
#--dev-addr 127.0.0.1:9001
11 changes: 11 additions & 0 deletions mkdocs.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ site_name: Test Site

theme:
name: material
features:
- navigation.instant
- navigation.indexes
- navigation.tracking
- navigation.top
- content.tooltips
- search.suggest
- search.highlight
- search.share
- navigation.tabs
- navigation.tabs.sticky
9 changes: 7 additions & 2 deletions mkdocs_file_filter_plugin/judger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import pathlib
import re
from typing import Union
from urllib.parse import urlsplit

import igittigitt
Expand All @@ -14,6 +15,8 @@
from . import util as LOG
from .plugin_config import PluginConfig

NavigationItem = Union[MkDocsPage, MkDocsSection, MkDocsLink, None]


class Judger:
def __init__(self, plugin_config: PluginConfig, mkdocs_config: MkDocsConfig):
Expand All @@ -26,14 +29,15 @@ def __init__(self, plugin_config: PluginConfig, mkdocs_config: MkDocsConfig):
pathlib.Path(self.plugin_config.mkdocsignore_file)
)

def evaluate_nav(self, nav):
def evaluate_nav(self, nav: NavigationItem) -> NavigationItem:
if isinstance(nav, MkDocsSection):
nev_section = [self.evaluate_nav(child) for child in nav.children]
nev_section = list(filter(lambda item: item is not None, nev_section))
if nev_section != []:
return MkDocsSection(nav.title, nev_section)
else:
LOG.debug(f"remove navigation section: {nav.title}")
return None
else:
scheme, netloc, path, query, fragment = urlsplit(nav.url)
if (
Expand All @@ -42,7 +46,8 @@ def evaluate_nav(self, nav):
and not scheme
and not netloc
):
LOG.debug(f"remove navigation item: {nav.title} {nav.url}")
LOG.debug(f"remove navigation link: {nav.title} {nav.url}")
return None
else:
return nav

Expand Down
14 changes: 11 additions & 3 deletions mkdocs_file_filter_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
from mkdocs.plugins import BasePlugin as MkDocsPlugin
from mkdocs.structure.files import Files as MkDocsFiles
from mkdocs.structure.nav import Navigation as MkDocsNavigation
from mkdocs.structure.nav import (
_add_parent_links,
_add_previous_and_next_links,
_get_by_type,
)
from mkdocs.structure.pages import Page as MkDocsPage

from . import util as LOG
from .external_config import ExternalConfig
Expand Down Expand Up @@ -99,11 +105,13 @@ def on_nav(self, nav: MkDocsNavigation, config: MkDocsConfig, files: MkDocsFiles

judger = Judger(self.config, config)
nav_items_new = []
for nav_item in nav:
for nav_item in nav.items:
result = judger.evaluate_nav(nav_item)
if result is not None:
nav_items_new.append(result)

nav_items_new = list(filter(lambda item: item is not None, nav_items_new))
pages = _get_by_type(nav_items_new, MkDocsPage)
_add_previous_and_next_links(pages)
_add_parent_links(nav_items_new)

return MkDocsNavigation(nav_items_new, nav.pages)
return MkDocsNavigation(nav_items_new, pages)
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ isort = "^5.12.0"
flake8 = "^6.0.0"
bandit = "^1.7.4"

[tool.isort]
profile = "black"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

0 comments on commit f02133d

Please sign in to comment.