Module

fonts

Font helper for Bengal SSG.

Provides simple font downloading and CSS generation for Google Fonts.

Usage:

# In bengal.toml:
[fonts]
primary = "Inter:400,600,700"
heading = "Playfair Display:700"

# Bengal automatically downloads fonts and generates CSS

Classes

FontHelper
Main font helper interface. Usage: helper = FontHelper(config) helper.process(output_dir)
3

Main font helper interface.

Usage:

helper = FontHelper(config)
helper.process(output_dir)

Methods 1

process
Process fonts: download files and generate CSS.
1 Path | None
def process(self, assets_dir: Path) -> Path | None

Process fonts: download files and generate CSS.

Parameters 1
assets_dir Path

Assets directory (fonts go in assets/fonts/)

Returns

Path | None

Path to generated fonts.css, or None if no fonts configured

Internal Methods 2
__init__
Initialize font helper with configuration.
1 None
def __init__(self, font_config: dict[str, Any])

Initialize font helper with configuration.

Parameters 1
font_config dict[str, Any]

[fonts] section from bengal.toml

_parse_config
Parse [fonts] configuration into normalized format. Supports two formats: 1. S…
0 dict[str, dict[str, Any]]
def _parse_config(self) -> dict[str, dict[str, Any]]

Parse [fonts] configuration into normalized format.

Supports two formats:

  1. Simple string: "Inter:400,600,700"
  2. Detailed dict: {family = "Inter", weights = [400, 600, 700]}
Returns

dict[str, dict[str, Any]]

Dict mapping font name to font specification

Functions

rewrite_font_urls_with_fingerprints
Rewrite font URLs in fonts.css to use fingerprinted filenames. After asset fingerprinting, font fi…
2 bool
def rewrite_font_urls_with_fingerprints(fonts_css_path: Path, asset_manifest: dict[str, Any]) -> bool

Rewrite font URLs in fonts.css to use fingerprinted filenames.

After asset fingerprinting, font files have hashed names like: fonts/outfit-400.6c18d579.woff2

This function updates fonts.css to reference these fingerprinted names instead of the original names like fonts/outfit-400.woff2.

Parameters 2

Name Type Default Description
fonts_css_path Path

Path to fonts.css in output directory

asset_manifest dict[str, Any]

Asset manifest dict with 'assets' key mapping logical paths to fingerprinted output paths

Returns

bool

True if fonts.css was updated, False otherwise