Skip to content

Commit

Permalink
Handle plugin generated content and config (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
thejcannon authored Aug 19, 2023
1 parent 572c230 commit e679f2c
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 64 deletions.
16 changes: 12 additions & 4 deletions mkdocs_awesome_pages_plugin/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import PurePath
from typing import Optional, List, Union, Any, Iterator

from mkdocs.structure.files import Files
import yaml
from wcmatch import glob

Expand Down Expand Up @@ -147,13 +148,20 @@ def __init__(
self.order_by = order_by

@staticmethod
def try_load_from(path: Optional[str]) -> "Meta":
if path is None:
def try_load_from_files(rel_path: Optional[str], files: "Files") -> "Meta":
if rel_path is None:
return Meta()

file = files.src_paths.get(rel_path)
if file is None:
return Meta(path=rel_path)

try:
return Meta.load_from(path)
meta = Meta.load_from(file.abs_src_path)
meta.path = file.src_path # Use the relative path
return meta
except FileNotFoundError:
return Meta(path=path)
return Meta(path=file.src_path)

@staticmethod
def load_from(path: str) -> "Meta":
Expand Down
18 changes: 9 additions & 9 deletions mkdocs_awesome_pages_plugin/navigation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import warnings
from pathlib import Path

import mkdocs.utils
import mkdocs.utils.meta
Expand All @@ -13,6 +12,7 @@
_add_parent_links,
_add_previous_and_next_links,
)
from mkdocs.structure.files import Files
from mkdocs.structure.pages import Page

from .meta import Meta, MetaNavItem, MetaNavRestItem, RestItemList
Expand Down Expand Up @@ -50,13 +50,13 @@ def __init__(
self,
items: List[NavigationItem],
options: Options,
docs_dir: str,
files: Files,
explicit_sections: Set[Section],
):
self.options = options
self.explicit_sections = explicit_sections

self.meta = NavigationMeta(items, options, docs_dir, explicit_sections)
self.meta = NavigationMeta(items, options, files, explicit_sections)

if self.meta.root.title is not None:
warnings.warn(TitleInRootHasNoEffect(self.options.filename))
Expand Down Expand Up @@ -193,7 +193,7 @@ def _get_item_path(self, item: NavigationItem) -> Optional[str]:
if isinstance(item, Section):
return dirname(self.meta.sections[item].path)
elif isinstance(item, Page):
return item.file.abs_src_path
return item.file.src_path

def _get_item_title(self, item: NavigationItem) -> str:
# Handle custom section titles in the ".pages" file
Expand Down Expand Up @@ -261,12 +261,12 @@ def __init__(
self,
items: List[NavigationItem],
options: Options,
docs_dir: str,
files: Files,
explicit_sections: Set[Section],
):
self.options = options
self.sections: Dict[Section, Meta] = {}
self.docs_dir = docs_dir
self.files = files
self.explicit_sections = explicit_sections

self.root: Meta = self._gather_metadata(items)
Expand All @@ -275,8 +275,7 @@ def _gather_metadata(self, items: List[NavigationItem]) -> Meta:
paths: List[str] = []
for item in items:
if isinstance(item, Page):
if Path(self.docs_dir) in Path(item.file.abs_src_path).parents:
paths.append(item.file.abs_src_path)
paths.append(item.file.src_path)
elif isinstance(item, Section):
section_meta = self._gather_metadata(item.children)

Expand All @@ -289,7 +288,8 @@ def _gather_metadata(self, items: List[NavigationItem]) -> Meta:

self.sections[item] = section_meta

return Meta.try_load_from(join_paths(self._common_dirname(paths), self.options.filename))
rel_config_path = join_paths(self._common_dirname(paths), self.options.filename)
return Meta.try_load_from_files(rel_config_path, self.files)

@staticmethod
def _common_dirname(paths: List[str]) -> Optional[str]:
Expand Down
25 changes: 24 additions & 1 deletion mkdocs_awesome_pages_plugin/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os.path
import warnings
import glob
from typing import List, Dict

from mkdocs.config import config_options, Config
Expand Down Expand Up @@ -45,6 +47,22 @@ def __init__(self):
self.rest_items = RestItemList()
self.rest_blocks = {}

def on_files(self, files: Files, config: Config):
# Add config files to Files, so we can unconditionally load files/configs from Files
config_files = glob.glob(os.path.join(config["docs_dir"], f"**/{self.config['filename']}"), recursive=True)
initial_src_paths = files.src_paths
for filename in config_files:
config_path = os.path.relpath(filename, config["docs_dir"])
if config_path not in initial_src_paths:
file = File(
os.path.relpath(filename, config["docs_dir"]),
src_dir=config["docs_dir"],
dest_dir=config["site_dir"],
use_directory_urls=config["use_directory_urls"],
)
files.append(file)
return files

def on_nav(self, nav: MkDocsNavigation, config: Config, files: Files):
explicit_nav = nav if config["nav"] else None

Expand All @@ -60,7 +78,12 @@ def on_nav(self, nav: MkDocsNavigation, config: Config, files: Files):
self._insert_rest(explicit_nav.items)
nav = explicit_nav

return AwesomeNavigation(nav.items, Options(**self.config), config["docs_dir"], explicit_sections).to_mkdocs()
return AwesomeNavigation(
nav.items,
Options(**self.config),
files,
explicit_sections,
).to_mkdocs()

def on_config(self, config: Config):
for name, plugin in config["plugins"].items():
Expand Down
23 changes: 19 additions & 4 deletions mkdocs_awesome_pages_plugin/tests/e2e/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import tempfile
import warnings
from typing import Optional, List, Tuple, Union, Dict
from typing import Iterable, Optional, List, Tuple, Union, Dict, TypeVar
from unittest import TestCase

import yaml
Expand All @@ -13,10 +13,13 @@

from ...utils import cd

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


class E2ETestCase(TestCase):
MIN_ROOT_ITEMS = 2
DUMMY_NAME = "__dummy__"
PLUGINS: Tuple[PluginBaseT] = ()

def setUp(self):
self.config = self.createConfig()
Expand Down Expand Up @@ -72,13 +75,17 @@ def createConfig(
plugins_entry = "awesome-pages"
if plugin_options:
plugins_entry = {plugins_entry: plugin_options}
plugins = [
plugins_entry,
*[class_plugin.__name__.lower() for class_plugin in getattr(type(self), "PLUGINS", [])],
]

if mkdocs_nav is not None:
# mkdocs requires a minimum amount of top-level items to render the navigation properly
# ensure that this requirement is met by adding dummy pages
self._addDummyPages(mkdocs_nav, self.MIN_ROOT_ITEMS - len(mkdocs_nav))

return {"plugins": [plugins_entry], "nav": mkdocs_nav}
return {"plugins": plugins, "nav": mkdocs_nav}

def mkdocs(
self,
Expand Down Expand Up @@ -165,12 +172,20 @@ def _parseNav(self, ul: BeautifulSoup):

return pages

@staticmethod
def _patchGetPlugins():
@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(
name="awesome-pages",
value="mkdocs_awesome_pages_plugin.plugin:AwesomePagesPlugin",
Expand Down
92 changes: 92 additions & 0 deletions mkdocs_awesome_pages_plugin/tests/e2e/test_gen_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import os.path
import tempfile
from pathlib import Path

import yaml
from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import File

from .base import E2ETestCase


class GeneratedFiles(BasePlugin):
def on_pre_build(self, *args, **kwargs):
self.src_dir = tempfile.TemporaryDirectory()

def on_post_build(self, *args, **kwargs):
self.src_dir.cleanup()

def on_files(self, files, config):
docs_dir = Path(self.src_dir.name)
section_dir = docs_dir / "section"
section_dir.mkdir()
(section_dir / "2.md").touch()
(section_dir / "3.md").touch()
(section_dir / ".pages").write_text(yaml.dump({"arrange": ["3.md", "sub", "..."]}))
subsection_dir = section_dir / "sub"
subsection_dir.mkdir()
(subsection_dir / "a.md").touch()
(subsection_dir / ".pages").write_text(yaml.dump({"nav": ["a.md"]}))

def write_files(path):
for entry in path.iterdir():
if entry.is_file():
files.append(
File(
os.path.join(path.relative_to(docs_dir), entry.name),
src_dir=self.src_dir.name,
dest_dir=config["site_dir"],
use_directory_urls=config["use_directory_urls"],
)
)
else:
write_files(path / entry.name)

write_files(section_dir)
return files


class TestGeneratedFiles(E2ETestCase):
"""See https://github.com/lukasgeiter/mkdocs-awesome-pages-plugin/issues/78."""

PLUGINS = (GeneratedFiles,)

def test_mixed(self):
navigation = self.mkdocs(
self.config,
[
(
"section",
["1.md"],
)
],
)
self.assertEqual(
navigation,
[
(
"Section",
[
("3", "/section/3"),
("Sub", [("A", "/section/sub/a")]),
("1", "/section/1"),
("2", "/section/2"),
],
)
],
)

def test_all_virtual(self):
navigation = self.mkdocs(
self.config,
[],
)
self.assertEqual(
navigation,
[
(
"Section",
[("3", "/section/3"), ("Sub", [("A", "/section/sub/a")]), ("2", "/section/2")],
)
],
)
10 changes: 5 additions & 5 deletions mkdocs_awesome_pages_plugin/tests/navigation/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import os.path
from typing import List, Union, Optional
from unittest import TestCase, mock

from mkdocs.structure.files import File
from mkdocs.structure.files import File, Files
from mkdocs.structure.nav import (
Navigation as MkDocsNavigation,
Section,
Expand All @@ -25,8 +25,8 @@ def __init__(self):

class NavigationTestCase(TestCase):
@staticmethod
def page(title: str, path: Optional[str] = None, docs_dir: str = "") -> Page:
return Page(title, File(path or title + ".md", docs_dir, "", False), {})
def page(title: str, path: Optional[str] = None) -> Page:
return Page(title, File(path or title + ".md", os.path.abspath("docs"), "", False), {})

@staticmethod
def link(title: str, url: Optional[str] = None):
Expand Down Expand Up @@ -75,7 +75,7 @@ def createAwesomeNavigation(
collapse_single_pages=collapse_single_pages,
strict=strict,
),
docs_dir="",
files=Files([]),
explicit_sections=set(),
)

Expand Down
Loading

0 comments on commit e679f2c

Please sign in to comment.