Urlizer
Convert any URLs in text into clickable links.
Attributes
| Attribute | Type | Description |
|---|---|---|
| trailing_punctuation_chars | string = .,:;! | A string of characters to be stripped from the end of a word before evaluating it as a potential URL. |
| wrapping_punctuation | list = [("(", ")"), ("[", "]")] | A list of tuples containing opening and closing character pairs that should be stripped from around a word. |
| simple_url_re | regex pattern | A compiled regular expression used to identify and validate strings starting with http:// or https:// protocols. |
| simple_url_2_re | regex pattern | A compiled regular expression used to identify URLs starting with 'www.' or ending in one of the original seven gTLDs. |
| word_split_re | regex pattern | A compiled regular expression used to split input text into individual words and whitespace for processing. |
| mailto_template | string = mailto:{local}@{domain} | A format string used to construct the href value for email addresses. |
| url_template | string = < a href="{href}"{attrs} >{url}< /a > | A format string used to generate the final HTML anchor tag for identified links. |
Constructor
Signature
def Urlizer()
Methods
handle_word()
@classmethod
def handle_word(
word: string,
safe_input: boolean,
trim_url_limit: integer = null,
nofollow: boolean = false,
autoescape: boolean = false
) - > string
Processes an individual word to determine if it is a valid URL or email and converts it to an HTML link if necessary.
Parameters
| Name | Type | Description |
|---|---|---|
| word | string | The individual string token to evaluate for link conversion |
| safe_input | boolean | Indicates if the input word is already marked as safe from XSS |
| trim_url_limit | integer = null | The character limit used to truncate the display text of the generated link |
| nofollow | boolean = false | Whether to apply the nofollow attribute to the resulting link |
| autoescape | boolean = false | Whether to apply HTML escaping to the word and its components |
Returns
| Type | Description |
|---|---|
string | The original word, an escaped version of the word, or an HTML anchor tag string |
trim_url()
@classmethod
def trim_url(
x: string,
limit: integer
) - > string
Shortens a URL string to a specified length and appends an ellipsis if the limit is exceeded.
Parameters
| Name | Type | Description |
|---|---|---|
| x | string | The URL string to be truncated |
| limit | integer | The maximum allowed length for the URL string |
Returns
| Type | Description |
|---|---|
string | The truncated URL string with an ellipsis, or the original string if within the limit |
wrapping_punctuation_openings()
@classmethod
def wrapping_punctuation_openings() - > string
Retrieves a string containing all defined opening punctuation characters used for wrapping URLs.
Returns
| Type | Description |
|---|---|
string | A concatenated string of opening characters like '(' and '[' |
trailing_punctuation_chars_no_semicolon()
@classmethod
def trailing_punctuation_chars_no_semicolon() - > string
Provides the set of trailing punctuation characters excluding the semicolon to avoid breaking HTML entities.
Returns
| Type | Description |
|---|---|
string | The trailing punctuation character set with ';' removed |
trailing_punctuation_chars_has_semicolon()
@classmethod
def trailing_punctuation_chars_has_semicolon() - > boolean
Checks if the semicolon is included in the class's trailing punctuation configuration.
Returns
| Type | Description |
|---|---|
boolean | True if ';' is in the trailing_punctuation_chars string, False otherwise |
trim_punctuation()
@classmethod
def trim_punctuation(
word: string
) - > tuple
Trim trailing and wrapping punctuation from word. Return the items of the new state.
Parameters
| Name | Type | Description |
|---|---|---|
| word | string | The raw word string that may contain surrounding punctuation |
Returns
| Type | Description |
|---|---|
tuple | A tuple containing (lead, middle, trail) where middle is the potential URL |
is_email_simple()
@classmethod
def is_email_simple(
value: string
) - > boolean
Return True if value looks like an email address.
Parameters
| Name | Type | Description |
|---|---|---|
| value | string | The string to be validated as an email address |
Returns
| Type | Description |
|---|---|
boolean | True if the value passes basic email validation, False otherwise |