Skip to content

HtmlInjectorPluginBase gives plugins no access to the engine/db, #26

Description

@github-actions

so the site's theme colors/font can no longer be used as a fallback

here (only hardcoded defaults, from MsgBarConfig's own field defaults).

# TODO: HtmlInjectorPluginBase gives plugins no access to the engine/db,
# so the site's theme colors/font can no longer be used as a fallback
# here (only hardcoded defaults, from MsgBarConfig's own field defaults).

"""


class MsgBarPlugin(HtmlInjectorPluginBase):
    """Plugin that displays a customizable message bar at the top of web pages."""

    accepted_page_sections: frozenset[PageSection] = frozenset({"body"})

    def __init__(self, config: dict[str, Any]) -> None:
        """Validate the configuration and precompute the message bar HTML.

        Args:
            config: Raw configuration dict from the platzky engine.

        Raises:
            ConfigPluginError: If the configuration is invalid.
        """
        super().__init__(config)
        try:
            msgbar_config = MsgBarConfig.model_validate(config)
        except ValidationError as e:
            raise ConfigPluginError(f"Invalid configuration: {e}") from e

        # TODO: HtmlInjectorPluginBase gives plugins no access to the engine/db,
        # so the site's theme colors/font can no longer be used as a fallback
        # here (only hardcoded defaults, from MsgBarConfig's own field defaults).
        self._bar_html = _build_bar_html(
            message=_render_message(msgbar_config.message),
            background_color=msgbar_config.get_validated_background_color(),
            text_color=msgbar_config.get_validated_text_color(),
            font_size=msgbar_config.get_validated_font_size(),
            font_family=msgbar_config.get_validated_font_family(),
            bar_height=msgbar_config.get_validated_bar_height(),
        )

    def get_body_html(self) -> str:
        """Return the message bar HTML to inject at the start of the page body."""
        return self._bar_html

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions