This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
"""
|
||||
This module defines the NavSpec class, which represents the user's intent for
|
||||
documentation navigation as defined in a YAML specification (usually
|
||||
docforge.nav.yml).
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
@@ -5,18 +11,44 @@ import yaml
|
||||
|
||||
|
||||
class NavSpec:
|
||||
"""Parsed representation of docforge.nav.yml."""
|
||||
"""
|
||||
Parsed representation of the docforge navigation specification file.
|
||||
|
||||
Attributes:
|
||||
home: Path to the home document (e.g., 'index.md').
|
||||
groups: Mapping of group titles to lists of path patterns/globs.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
home: Optional[str],
|
||||
groups: Dict[str, List[str]],
|
||||
) -> None:
|
||||
"""
|
||||
Initialize a NavSpec.
|
||||
|
||||
Args:
|
||||
home: The path to the home document.
|
||||
groups: A mapping of group names to lists of path patterns (globs).
|
||||
"""
|
||||
self.home = home
|
||||
self.groups = groups
|
||||
|
||||
@classmethod
|
||||
def load(cls, path: Path) -> "NavSpec":
|
||||
"""
|
||||
Load a NavSpec from a YAML file.
|
||||
|
||||
Args:
|
||||
path: The filesystem path to the YAML file.
|
||||
|
||||
Returns:
|
||||
A NavSpec instance.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: If the path does not exist.
|
||||
ValueError: If the file content is not a valid NavSpec mapping.
|
||||
"""
|
||||
if not path.exists():
|
||||
raise FileNotFoundError(path)
|
||||
|
||||
@@ -45,6 +77,12 @@ class NavSpec:
|
||||
return cls(home=home, groups=groups)
|
||||
|
||||
def all_patterns(self) -> List[str]:
|
||||
"""
|
||||
Get all path patterns referenced in the specification.
|
||||
|
||||
Returns:
|
||||
A list of all patterns (home plus all groups).
|
||||
"""
|
||||
patterns: List[str] = []
|
||||
if self.home:
|
||||
patterns.append(self.home)
|
||||
@@ -54,6 +92,15 @@ class NavSpec:
|
||||
|
||||
|
||||
def load_nav_spec(path: Path) -> NavSpec:
|
||||
"""
|
||||
Utility function to load a NavSpec from a file.
|
||||
|
||||
Args:
|
||||
path: Path to the navigation specification file.
|
||||
|
||||
Returns:
|
||||
A loaded NavSpec instance.
|
||||
"""
|
||||
if not path.exists():
|
||||
raise FileNotFoundError(path)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user