Truncator
An object used to truncate text, either by characters or words.
Constructor
Signature
def Truncator(
text: Any
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| text | Any | The text content to be truncated. |
Methods
chars()
@classmethod
def chars(
num: int,
truncate: string = None,
html: boolean = False
) - > string
Return the text truncated to be no longer than the specified number of characters.
Parameters
| Name | Type | Description |
|---|---|---|
| num | int | The maximum number of characters allowed in the output string. |
| truncate | string = None | The string used to notify that the text has been truncated, such as an ellipsis. |
| html | boolean = False | A flag indicating whether to treat the input as HTML and preserve tag integrity during truncation. |
Returns
| Type | Description |
|---|---|
string | The truncated string, optionally including an ellipsis or custom replacement string. |
words()
@classmethod
def words(
num: int,
truncate: string = None,
html: boolean = False
) - > string
Truncate a string after a certain number of words. truncate specifies what should be used to notify that the string has been truncated, defaulting to ellipsis.
Parameters
| Name | Type | Description |
|---|---|---|
| num | int | The maximum number of words allowed in the output string. |
| truncate | string = None | The string used to notify that the text has been truncated. |
| html | boolean = False | A flag indicating whether to parse the input as HTML to avoid breaking tags. |
Returns
| Type | Description |
|---|---|
string | The text truncated to the specified word count, formatted as plain text or HTML. |