Module

utils.url_normalization

URL Normalization Utilities

Centralized URL normalization and validation for Bengal SSG. All URL construction should use these utilities to ensure consistency.

Design Principles:

  • Single source of truth for URL normalization
  • Normalize at construction time, not access time
  • Validate URLs when created
  • Handle edge cases (multiple slashes, trailing slashes, etc.)

Functions

normalize_url
Normalize a relative URL to a consistent format. Rules: - Always starts with / - No multiple conse…
2 str
def normalize_url(url: str, ensure_trailing_slash: bool = True) -> str

Normalize a relative URL to a consistent format.

Rules:

  • Always starts with /
  • No multiple consecutive slashes (except after protocol)
  • Trailing slash for directory-like URLs (if ensure_trailing_slash=True)
  • Root is "/"

Parameters 2

Name Type Default Description
url str

URL to normalize (can be empty, relative, or absolute)

ensure_trailing_slash bool True

Whether to ensure trailing slash (default: True)

Returns

str

Normalized URL string

join_url_paths
Join URL path components, normalizing slashes.
0 str
def join_url_paths(*parts: str) -> str

Join URL path components, normalizing slashes.

Returns

str

Normalized joined URL

validate_url
Validate that a URL is in correct format.
1 bool
def validate_url(url: str) -> bool

Validate that a URL is in correct format.

Parameters 1

Name Type Default Description
url str

URL to validate

Returns

bool

True if URL is valid, False otherwise