Skip to main content

Urlizer

Convert any URLs in text into clickable links.

Attributes

AttributeTypeDescription
trailing_punctuation_charsstring = .,:;!A string of characters to be stripped from the end of a word before evaluating it as a potential URL.
wrapping_punctuationlist = [("(", ")"), ("[", "]")]A list of tuples containing opening and closing character pairs that should be stripped from around a word.
simple_url_reregex patternA compiled regular expression used to identify and validate strings starting with http:// or https:// protocols.
simple_url_2_reregex patternA compiled regular expression used to identify URLs starting with 'www.' or ending in one of the original seven gTLDs.
word_split_reregex patternA compiled regular expression used to split input text into individual words and whitespace for processing.
mailto_templatestring = mailto:{local}@{domain}A format string used to construct the href value for email addresses.
url_templatestring = < 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

NameTypeDescription
wordstringThe individual string token to evaluate for link conversion
safe_inputbooleanIndicates if the input word is already marked as safe from XSS
trim_url_limitinteger = nullThe character limit used to truncate the display text of the generated link
nofollowboolean = falseWhether to apply the nofollow attribute to the resulting link
autoescapeboolean = falseWhether to apply HTML escaping to the word and its components

Returns

TypeDescription
stringThe 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

NameTypeDescription
xstringThe URL string to be truncated
limitintegerThe maximum allowed length for the URL string

Returns

TypeDescription
stringThe 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

TypeDescription
stringA 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

TypeDescription
stringThe 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

TypeDescription
booleanTrue 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

NameTypeDescription
wordstringThe raw word string that may contain surrounding punctuation

Returns

TypeDescription
tupleA 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

NameTypeDescription
valuestringThe string to be validated as an email address

Returns

TypeDescription
booleanTrue if the value passes basic email validation, False otherwise