worked out some bugs in extension loading

This commit is contained in:
Amy G. Bowersox 2024-08-19 21:55:42 -06:00
parent 13bdcf7f58
commit 2b5f9e9b43
3 changed files with 18 additions and 15 deletions

View File

@ -65,8 +65,8 @@ sitetitle = ""
[extensions]
# Full classnames of the extensions to be loaded.
load = ["dragonglass.extensions.breadcrumbs.BreadcrumbExtension"]
load = ["dragonglass.extensions.breadcrumbs.Breadcrumbs"]
[extensions.BreadcrumbExtension]
# Configuration data for BreadcrumbExtension.
[extensions.Breadcrumbs]
# Configuration data for Breadcrumbs.
var = "value"

View File

@ -49,8 +49,6 @@ CALLOUT_ICONS = {
'todo': 'circle-check',
'warning': 'triangle-alert'
}
"""Cache of extension module names."""
EXTENSION_PKGS = {}
def load_extension_class(name: str, sect: dict[str, Any]) -> Extension:
@ -65,15 +63,14 @@ def load_extension_class(name: str, sect: dict[str, Any]) -> Extension:
Extension: Instance of the extension class.
"""
components = name.split('.')
classname = components.pop()
modname = ".".join(components)
if modname not in EXTENSION_PKGS:
EXTENSION_PKGS[modname] = __import__(modname)
extclass = getattr(EXTENSION_PKGS[modname], classname)
cfg_sect = sect.get(classname, {})
rc = extclass(cfg_sect)
modname = ".".join(components[:-1])
b = __import__(modname)
for c in components[1:]:
b = getattr(b, c)
cfg_sect = sect.get(components[-1], {})
rc = b(cfg_sect)
if not isinstance(rc, Extension):
raise RuntimeError(f"class {name} is not a valid Dragonglass extension")
raise RuntimeError(f"class {name} is not a valid dragonglass extension")
return rc

View File

@ -9,11 +9,17 @@ from ..tree import SourceNode, SourceIndex
"""The extension for generating breadcrumb informatioon."""
class BreadcrumbExtension(Extension):
class Breadcrumbs(Extension):
LINK = re.compile(r'^\[\[(.+)(?:\|.+)?\]\]')
def __init__(self, config: dict[str, Any]):
super(BreadcrumbExtension, self).__init__(config)
"""
Initialize the Breadcrumbs extension.
Args:
config (dict[str, Any]): Configuration variables for this extension.
"""
super(Breadcrumbs, self).__init__(config)
self._root_nodes: list[SourceNode] = []
self._raw_targets: list[tuple[SourceNode, str]] = []
self._parents: dict[SourceNode, SourceNode] = {}