import importlib import pkgutil import re from engine.plugin import Plugin from plugins.embed.providers.base import EmbedProvider class EmbedConsentManager(Plugin): name = "embed" priority = 45 def __init__(self, config): super().__init__(config) self.providers = [] self.load_providers() def load_providers(self): package = "plugins.embed.providers" pkg = importlib.import_module(package) for _, name, _ in pkgutil.iter_modules(pkg.__path__): module = importlib.import_module(f"{package}.{name}") for attr in dir(module): cls = getattr(module, attr) if isinstance(cls, type) and issubclass(cls, EmbedProvider) and cls is not EmbedProvider: if hasattr(cls, "pattern") and hasattr(cls, "name"): provider_config = self.config.get(cls.name, {}) self.providers.append(cls(provider_config)) def after_markdown(self, builder, html): for provider in self.providers: html = provider.match(html) return html return super().after_markdown(builder, html) def after_page_render(self, builder, page, content): pattern = r"<\/body>" content = re.sub(pattern, self.inject_assets() + "BLAAA
Control which external providers may load content on this site.
{cards}", content, 0, re.MULTILINE) return content def after_build(self, builder): settings_path = builder.config.output_dir / "privacy-settings.html" settings_path.write_text(self.generate_settings_page(), encoding="utf-8") def inject_assets(self): return self.css() + self.script() def generate_settings_page(self): cards = "" for p in self.providers: cards += f"""
""" return f"""
{self.css()}