Module

rendering.plugins.directives.utils

Shared utilities for directive implementations.

Consolidates common helper functions to eliminate duplication across directive files.

Architecture:

These utilities are used by BengalDirective base class and can be
imported directly by directives needing standalone access.

Related:

  • bengal/rendering/plugins/directives/base.py: Uses these utilities
  • RFC: plan/active/rfc-directive-system-v2.md (Issue 1: duplicated boilerplate)

Functions

escape_html
Escape HTML special characters for use in attributes. Escapes: & < > " '
1 str
def escape_html(text: str) -> str

Escape HTML special characters for use in attributes.

Escapes: & < > " '

Parameters 1

Name Type Default Description
text str

Raw text to escape

Returns

str

HTML-escaped text safe for use in attributes

build_class_string
Build CSS class string from multiple class sources. Filters out empty strings and joins with space.
0 str
def build_class_string(*classes: str) -> str

Build CSS class string from multiple class sources.

Filters out empty strings and joins with space.

Returns

str

Space-joined class string

bool_attr
Return HTML boolean attribute string.
2 str
def bool_attr(name: str, value: bool) -> str

Return HTML boolean attribute string.

Parameters 2

Name Type Default Description
name str

Attribute name (e.g., "open", "disabled")

value bool

Whether to include the attribute

Returns

str

" name" if value is True, "" otherwise

data_attrs
Build data-* attribute string from keyword arguments. Converts underscore names to hyphenated (dat…
0 str
def data_attrs(**attrs: Any) -> str

Build data-* attribute string from keyword arguments.

Converts underscore names to hyphenated (data_foo -> data-foo). Skips None and empty string values.

Returns

str

Space-joined data attribute string

attr_str
Return HTML attribute string if value is truthy.
2 str
def attr_str(name: str, value: str | None) -> str

Return HTML attribute string if value is truthy.

Parameters 2

Name Type Default Description
name str

Attribute name

value str | None

Attribute value (may be None or empty)

Returns

str

' name="value"' if value is truthy, "" otherwise

class_attr
Build class attribute string. Convenience wrapper combining build_class_string with attribute form…
1 str
def class_attr(base_class: str, *extra_classes: str) -> str

Build class attribute string.

Convenience wrapper combining build_class_string with attribute formatting.

Parameters 1

Name Type Default Description
base_class str

Primary class (always included if non-empty) *extra_classes: Additional classes to add

Returns

str

' class="..."' string or "" if no classes