Functions
register
Register string functions with Jinja2 environment.
register
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.
dict_get
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.
truncatewords
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
Truncated text with suffix if neededstr
—
truncatewords_html
Truncate HTML text to word count, preserving HTML structure.
Uses a tag-aware approach that:
1. Co…
truncatewords_html
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:
- Counts only text content words (not tag content)
- Keeps track of open tags
- 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
Truncated HTML with properly closed tagsstr
—
slugify
Convert text to URL-safe slug.
Uses bengal.utils.text.slugify internally.
Converts to lowercase, r…
slugify
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
URL-safe slugstr
—
_convert_docstring_to_markdown
Convert Google/NumPy-style docstrings to markdown.
Handles:
- Indented lists ( - Item) → proper…
_convert_docstring_to_markdown
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
Markdown-formatted textstr
—
markdownify
Render Markdown text to HTML.
Pre-processes Google-style docstrings to markdown, then converts to …
markdownify
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
Rendered HTMLstr
—
strip_html
Remove all HTML tags from text.
Uses bengal.utils.text.strip_html internally.
strip_html
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
Text with HTML tags removedstr
—
truncate_chars
Truncate text to character length.
Uses bengal.utils.text.truncate_chars internally.
truncate_chars
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
Truncated text with suffix if neededstr
—
replace_regex
Replace text using regular expression.
replace_regex
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
Text with replacements madestr
—
pluralize
Return singular or plural form based on count.
Uses bengal.utils.text.pluralize internally.
pluralize
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
Appropriate form based on countstr
—
reading_time
Calculate reading time in minutes.
reading_time
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
Reading time in minutes (minimum 1)int
—
word_count
Count words in text.
Strips HTML tags before counting. Uses same logic as reading_time.
word_count
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
Number of wordsint
—
excerpt
Extract excerpt from text, optionally respecting word boundaries.
excerpt
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
Excerpt with ellipsis if truncatedstr
—
strip_whitespace
Remove extra whitespace (multiple spaces, newlines, tabs).
Uses bengal.utils.text.normalize_whites…
strip_whitespace
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
Text with normalized whitespacestr
—
first_sentence
Extract first sentence from text, or truncate if too long.
Useful for generating short description…
first_sentence
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
First sentence or truncated text with ellipsisstr
—
filesize
Format bytes as human-readable file size.
Wraps bengal.utils.text.humanize_bytes for template use.
filesize
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
Human-readable size string (e.g., "1.5 MB", "256 KB")str
—