Skip to content

Commit

Permalink
Add support for mkdocs 1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasgeiter committed Jul 25, 2024
1 parent f12694c commit 35cd23d
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 35 deletions.
3 changes: 3 additions & 0 deletions mkdocs_awesome_pages_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def on_nav(self, nav: MkDocsNavigation, config: Config, files: Files):

if self.nav_config_with_rest:
# restore explicit config with rest placeholder and build nav
for file in files.documentation_pages():
file.page = None # clear all pages, so mkdocs actually generates the pages based on the nav config
config["nav"] = self.nav_config_with_rest
explicit_nav = get_navigation(files, config)

Expand Down Expand Up @@ -121,6 +123,7 @@ def _generate_rest_blocks(
for item in items[:]: # loop over a shallow copy of items so removing items doesn't break iteration
if isinstance(item, Page):
if item.file not in exclude_files:
item.file.page = item # restore previously cleared page on the file (line 69)
for rest_item in self.rest_items:
if rest_item.matches(item.file.src_path):
items.remove(item)
Expand Down
51 changes: 33 additions & 18 deletions mkdocs_awesome_pages_plugin/tests/e2e/base.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import os
import sys
import tempfile
import warnings
from typing import Dict, List, Optional, Tuple, TypeVar, Union
from unittest import TestCase

import yaml
from bs4 import BeautifulSoup
from importlib_metadata import EntryPoint
from mkdocs import __version__ as mkdocs_version
from mkdocs import plugins
from mkdocs.commands.build import build
from mkdocs.config import load_config

from ...utils import cd

if sys.version_info >= (3, 10):
from importlib.metadata import EntryPoint
else:
from importlib_metadata import EntryPoint

PluginBaseT = TypeVar("PluginBaseT", bound=plugins.BasePlugin)


Expand Down Expand Up @@ -134,7 +140,7 @@ def _createFiles(self, directory: str, files: List[Union[str, Tuple[str, Union[s
self._createFiles(path, contents)

def _mkdocsBuild(self, **options):
self._patchGetPlugins()
self._patchInstalledPlugins()

with warnings.catch_warnings():
# ignore deprecation warnings within mkdocs
Expand Down Expand Up @@ -173,27 +179,36 @@ def _parseNav(self, ul: BeautifulSoup):
return pages

@classmethod
def _patchGetPlugins(cls):
_original_get_plugins = plugins.get_plugins

def _patched_get_plugins():
result = _original_get_plugins()

for class_plugin in cls.PLUGINS:
name = class_plugin.__name__.lower()
result[name] = EntryPoint(
name=name,
value=f"{class_plugin.__module__}:{class_plugin.__name__}",
group="mkdocs.plugins",
)
result["awesome-pages"] = EntryPoint(
def _patchInstalledPlugins(cls):
entry_points = {
"awesome-pages": EntryPoint(
name="awesome-pages",
value="mkdocs_awesome_pages_plugin.plugin:AwesomePagesPlugin",
group="mkdocs.plugins",
)
return result
}

for class_plugin in cls.PLUGINS:
name = class_plugin.__name__.lower()
entry_points[name] = EntryPoint(
name=name,
value=f"{class_plugin.__module__}:{class_plugin.__name__}",
group="mkdocs.plugins",
)

if mkdocs_version >= "1.4.0":
from mkdocs.config.defaults import MkDocsConfig

MkDocsConfig.plugins.installed_plugins.update(entry_points)
else:
_original_get_plugins = plugins.get_plugins

def _patched_get_plugins():
result = _original_get_plugins()
result.update(entry_points)
return result

plugins.get_plugins = _patched_get_plugins
plugins.get_plugins = _patched_get_plugins

@staticmethod
def _writeToFile(path: str, content: str):
Expand Down
18 changes: 17 additions & 1 deletion mkdocs_awesome_pages_plugin/tests/e2e/test_mkdocs_nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,30 @@ def test_end(self):

self.assertEqual(navigation, [("2", "/2"), ("1", "/1"), ("3", "/3")])

def test_duplicate_item(self):
@pytest.mark.skipif(
mkdocs_version >= "1.6.0",
reason="Handling of duplicates changed with version 1.6",
)
def test_duplicate_item_old(self):
navigation = self.mkdocs(
self.createConfig(mkdocs_nav=[{"2a": "2.md"}, {"2b": "2.md"}, "1.md", "..."]),
["1.md", "2.md", "3.md"],
)

self.assertEqual(navigation, [("2a", "/2"), ("2b", "/2"), ("1", "/1"), ("3", "/3")])

@pytest.mark.skipif(
mkdocs_version < "1.6.0",
reason="Handling of duplicates changed with version 1.6",
)
def test_duplicate_item_new(self):
navigation = self.mkdocs(
self.createConfig(mkdocs_nav=[{"2a": "2.md"}, {"2b": "2.md"}, "1.md", "..."]),
["1.md", "2.md", "3.md"],
)

self.assertEqual(navigation, [("2a", "/2"), ("2a", "/2"), ("1", "/1"), ("3", "/3")])

def test_duplicate_rest_token(self):
with self.assertRaises(DuplicateRestItemError):
self.mkdocs(
Expand Down
39 changes: 37 additions & 2 deletions mkdocs_awesome_pages_plugin/tests/e2e/test_nav.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest
from mkdocs import __version__ as mkdocs_version

from ...meta import DuplicateRestItemError
from ...navigation import NavEntryNotFound
from .base import E2ETestCase
Expand Down Expand Up @@ -344,22 +347,54 @@ def test_collapsed(self):

self.assertEqual(navigation, [("1", "/b/1"), ("A", [("1", "/a/1"), ("2", "/a/2")])])

def test_duplicate_file(self):
@pytest.mark.skipif(
mkdocs_version >= "1.6.0",
reason="Handling of duplicates changed with version 1.6",
)
def test_duplicate_file_old(self):
navigation = self.mkdocs(
self.createConfig(mkdocs_nav=[{"1a": "1.md"}, {"2": "2.md"}, {"1b": "1.md"}]),
["1.md", "2.md", self.pagesFile(nav=["2.md", "1.md"])],
)

self.assertEqual(navigation, [("2", "/2"), ("1b", "/1")])

def test_duplicate_file_rest(self):
@pytest.mark.skipif(
mkdocs_version < "1.6.0",
reason="Handling of duplicates changed with version 1.6",
)
def test_duplicate_file_new(self):
navigation = self.mkdocs(
self.createConfig(mkdocs_nav=[{"1a": "1.md"}, {"2": "2.md"}, {"1b": "1.md"}]),
["1.md", "2.md", self.pagesFile(nav=["2.md", "1.md"])],
)

self.assertEqual(navigation, [("2", "/2"), ("1a", "/1")])

@pytest.mark.skipif(
mkdocs_version >= "1.6.0",
reason="Handling of duplicates changed with version 1.6",
)
def test_duplicate_file_rest_old(self):
navigation = self.mkdocs(
self.createConfig(mkdocs_nav=[{"1a": "1.md"}, {"2": "2.md"}, {"1b": "1.md"}]),
["1.md", "2.md", self.pagesFile(nav=["2.md", "..."])],
)

self.assertEqual(navigation, [("2", "/2"), ("1a", "/1"), ("1b", "/1")])

@pytest.mark.skipif(
mkdocs_version < "1.6.0",
reason="Handling of duplicates changed with version 1.6",
)
def test_duplicate_file_rest_new(self):
navigation = self.mkdocs(
self.createConfig(mkdocs_nav=[{"1a": "1.md"}, {"2": "2.md"}, {"1b": "1.md"}]),
["1.md", "2.md", self.pagesFile(nav=["2.md", "..."])],
)

self.assertEqual(navigation, [("2", "/2"), ("1a", "/1"), ("1a", "/1")])

def test_duplicate_entry(self):
navigation = self.mkdocs(
self.config,
Expand Down
50 changes: 36 additions & 14 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 35cd23d

Please sign in to comment.