Module

rendering.template_functions.strings

String manipulation functions for templates.

Provides 14 essential string functions for text processing in templates.

Many of these functions are now thin wrappers around bengal.utils.text utilities to avoid code duplication and ensure consistency.

Functions

register
Register string functions with Jinja2 environment.
2 None
def register(env: Environment, site: Site) -> None

Register string functions with Jinja2 environment.

Parameters 2

Name Type Default Description
env Environment
site Site
dict_get
Safe get supporting dict-like objects for component preview contexts.
3 Any
def dict_get(obj: Any, key: str, default: Any = None) -> Any

Safe get supporting dict-like objects for component preview contexts.

Parameters 3

Name Type Default Description
obj Any
key str
default Any None

Returns

Any

truncatewords
Truncate text to a specified number of words. Uses bengal.utils.text.truncate_words internally.
3 str
def truncatewords(text: str, count: int, suffix: str = '...') -> str

Truncate text to a specified number of words.

Uses bengal.utils.text.truncate_words internally.

Parameters 3

Name Type Default Description
text str

Text to truncate

count int

Maximum number of words

suffix str '...'

Text to append when truncated (default: "...")

Returns

str

Truncated text with suffix if needed

truncatewords_html
Truncate HTML text to word count, preserving HTML structure. Uses a tag-aware approach that: 1. Co…
3 str
def truncatewords_html(html: str, count: int, suffix: str = '...') -> str

Truncate HTML text to word count, preserving HTML structure.

Uses a tag-aware approach that:

  1. Counts only text content words (not tag content)
  2. Keeps track of open tags
  3. Closes any unclosed tags at truncation point

Parameters 3

Name Type Default Description
html str

HTML text to truncate

count int

Maximum number of words

suffix str '...'

Text to append when truncated

Returns

str

Truncated HTML with properly closed tags

slugify
Convert text to URL-safe slug. Uses bengal.utils.text.slugify internally. Converts to lowercase, r…
1 str
def slugify(text: str) -> str

Convert text to URL-safe slug.

Uses bengal.utils.text.slugify internally. Converts to lowercase, removes special characters, replaces spaces with hyphens.

Parameters 1

Name Type Default Description
text str

Text to convert

Returns

str

URL-safe slug

_convert_docstring_to_markdown
Convert Google/NumPy-style docstrings to markdown. Handles: - Indented lists ( - Item) → proper…
1 str
def _convert_docstring_to_markdown(text: str) -> str

Convert Google/NumPy-style docstrings to markdown.

Handles:

  • Indented lists ( - Item) → proper markdown lists
  • Section headers (Section:) → bold labels or headings
  • Preserves code blocks

Parameters 1

Name Type Default Description
text str

Docstring text

Returns

str

Markdown-formatted text

markdownify
Render Markdown text to HTML. Pre-processes Google-style docstrings to markdown, then converts to …
1 str
def markdownify(text: str) -> str

Render Markdown text to HTML.

Pre-processes Google-style docstrings to markdown, then converts to HTML using mistune (production dependency) with table support.

Parameters 1

Name Type Default Description
text str

Markdown or docstring text

Returns

str

Rendered HTML

strip_html
Remove all HTML tags from text. Uses bengal.utils.text.strip_html internally.
1 str
def strip_html(text: str) -> str

Remove all HTML tags from text.

Uses bengal.utils.text.strip_html internally.

Parameters 1

Name Type Default Description
text str

HTML text

Returns

str

Text with HTML tags removed

truncate_chars
Truncate text to character length. Uses bengal.utils.text.truncate_chars internally.
3 str
def truncate_chars(text: str, length: int, suffix: str = '...') -> str

Truncate text to character length.

Uses bengal.utils.text.truncate_chars internally.

Parameters 3

Name Type Default Description
text str

Text to truncate

length int

Maximum character length

suffix str '...'

Text to append when truncated

Returns

str

Truncated text with suffix if needed

replace_regex
Replace text using regular expression.
3 str
def replace_regex(text: str, pattern: str, replacement: str) -> str

Replace text using regular expression.

Parameters 3

Name Type Default Description
text str

Text to search in

pattern str

Regular expression pattern

replacement str

Replacement text

Returns

str

Text with replacements made

pluralize
Return singular or plural form based on count. Uses bengal.utils.text.pluralize internally.
3 str
def pluralize(count: int, singular: str, plural: str | None = None) -> str

Return singular or plural form based on count.

Uses bengal.utils.text.pluralize internally.

Parameters 3

Name Type Default Description
count int

Number to check

singular str

Singular form

plural str | None None

Plural form (default: singular + 's')

Returns

str

Appropriate form based on count

reading_time
Calculate reading time in minutes.
2 int
def reading_time(text: str, wpm: int = 200) -> int

Calculate reading time in minutes.

Parameters 2

Name Type Default Description
text str

Text to analyze

wpm int 200

Words per minute reading speed (default: 200)

Returns

int

Reading time in minutes (minimum 1)

word_count
Count words in text. Strips HTML tags before counting. Uses same logic as reading_time.
1 int
def word_count(text: str) -> int

Count words in text.

Strips HTML tags before counting. Uses same logic as reading_time.

Parameters 1

Name Type Default Description
text str

Text to count (can contain HTML)

Returns

int

Number of words

excerpt
Extract excerpt from text, optionally respecting word boundaries.
3 str
def excerpt(text: str, length: int = 200, respect_word_boundaries: bool = True) -> str

Extract excerpt from text, optionally respecting word boundaries.

Parameters 3

Name Type Default Description
text str

Text to excerpt from

length int 200

Maximum length in characters

respect_word_boundaries bool True

Don't cut words in half (default: True)

Returns

str

Excerpt with ellipsis if truncated

strip_whitespace
Remove extra whitespace (multiple spaces, newlines, tabs). Uses bengal.utils.text.normalize_whites…
1 str
def strip_whitespace(text: str) -> str

Remove extra whitespace (multiple spaces, newlines, tabs).

Uses bengal.utils.text.normalize_whitespace internally. Replaces all whitespace sequences with a single space.

Parameters 1

Name Type Default Description
text str

Text to clean

Returns

str

Text with normalized whitespace

first_sentence
Extract first sentence from text, or truncate if too long. Useful for generating short description…
2 str
def first_sentence(text: str, max_length: int = 120) -> str

Extract first sentence from text, or truncate if too long.

Useful for generating short descriptions from longer text blocks. Looks for sentence-ending punctuation (. ! ?) followed by whitespace.

Parameters 2

Name Type Default Description
text str

Text to extract first sentence from

max_length int 120

Maximum length before truncation (default: 120)

Returns

str

First sentence or truncated text with ellipsis

filesize
Format bytes as human-readable file size. Wraps bengal.utils.text.humanize_bytes for template use.
1 str
def filesize(size_bytes: int) -> str

Format bytes as human-readable file size.

Wraps bengal.utils.text.humanize_bytes for template use.

Parameters 1

Name Type Default Description
size_bytes int

Size in bytes

Returns

str

Human-readable size string (e.g., "1.5 MB", "256 KB")