Module

rendering.template_engine.asset_url

Asset URL generation for template engine.

Provides asset URL generation with fingerprinting, manifest lookup, and file:// protocol support.

Related Modules:

  • bengal.rendering.template_engine.core: Uses these helpers
  • bengal.assets.manifest: Asset manifest handling

Classes

AssetURLMixin
Mixin providing asset URL generation for TemplateEngine. Requires these attributes on the host cla…
3

Mixin providing asset URL generation for TemplateEngine.

Requires these attributes on the host class:

  • site: Site instance
  • _asset_manifest_path: Path

Requires these methods from ManifestHelpersMixin (must come BEFORE this mixin in MRO):

  • _get_manifest_entry(logical_path: str) -> AssetManifestEntry | None
  • _warn_manifest_fallback(logical_path: str) -> None

Attributes

Name Type Description
site Any
_asset_manifest_path Path
Internal Methods 3
_asset_url
Generate URL for an asset. Handles: - Manifest lookup for fingerprinted assets…
2 str
def _asset_url(self, asset_path: str, page_context: Any = None) -> str

Generate URL for an asset.

Handles:

  • Manifest lookup for fingerprinted assets
  • file:// protocol with relative paths
  • Dev server mode (no fingerprints)
  • Fallback to direct asset paths
Parameters 2
asset_path str

Path to asset file

page_context Any

Optional page context for computing relative paths

Returns

str

Asset URL

_asset_url_file_protocol
Generate asset URL for file:// protocol using relative paths.
2 str
def _asset_url_file_protocol(self, safe_asset_path: str, page_context: Any) -> str

Generate asset URL for file:// protocol using relative paths.

Parameters 2
safe_asset_path str

Validated asset path

page_context Any

Page context for computing relative path

Returns

str

Relative asset URL

_find_fingerprinted_asset
Find fingerprinted version of asset in output directory.
1 str | None
def _find_fingerprinted_asset(self, safe_asset_path: str) -> str | None

Find fingerprinted version of asset in output directory.

Parameters 1
safe_asset_path str

Validated asset path

Returns

str | None

Fingerprinted asset path if found, None otherwise

Functions

normalize_and_validate_asset_path
Normalize and validate the provided asset path to prevent traversal/absolute paths.
1 str
def normalize_and_validate_asset_path(raw_path: str) -> str

Normalize and validate the provided asset path to prevent traversal/absolute paths.

Parameters 1

Name Type Default Description
raw_path str

Raw asset path from template

Returns

str

Sanitized asset path, or empty string if invalid

compute_relative_asset_path
Compute relative path from page to asset for file:// protocol.
3 str | None
def compute_relative_asset_path(asset_path: str, page_context: Any, output_dir: Path) -> str | None

Compute relative path from page to asset for file:// protocol.

Parameters 3

Name Type Default Description
asset_path str

Asset path (e.g., 'assets/css/style.css')

page_context Any

Page context with output_path

output_dir Path

Site output directory

Returns

str | None

Relative path string, or None if cannot compute