duck.utils.slugΒΆ
Slug Utilities Module
This module provides various utilities for generating, manipulating, and validating slugs. A slug is a URL-friendly string, typically used in website URLs to represent titles or categories. These functions allow for tasks such as slug creation from text, slug-to-text conversion, validation, cleaning, and various string manipulations specific to slugs.
Functions include:
slugify: Converts a string to a URL-friendly slug.
unslugify: Converts a slug back to a human-readable string.
is_valid_slug: Checks if a string is a valid slug.
generate_slug_from_string: Generates a slug from a given string.
clean_slug: Cleans up a slug to ensure itβs properly formatted.
split_slug: Splits a slug into individual words.
join_slug: Joins a list of words into a slug.
truncate_slug: Truncates a slug to a specified maximum length.
sanitize_slug: Sanitizes a slug by removing invalid characters.
These utilities are useful for web developers handling slugs for SEO, URLs, or other string-related tasks.
Module ContentsΒΆ
FunctionsΒΆ
Clean up a slug by ensuring itβs lowercase and properly formatted. |
|
Generate a slug from a given string. |
|
Check if a string is a valid slug. |
|
Join a list of words into a slug. |
|
Sanitize a slug to ensure it contains only valid characters. |
|
Convert a string to a URL-friendly slug. |
|
Split a slug into individual words. |
|
Truncate a slug to a specified maximum length. |
|
Convert a slug back to a normal string. |
APIΒΆ
- duck.utils.slug.clean_slug(slug: str, separator: str = '-') str[source]ΒΆ
Clean up a slug by ensuring itβs lowercase and properly formatted.
- Parameters:
slug β The slug to clean.
separator β The separator used in the slug (default is β-β).
- Returns:
The cleaned slug.
- Return type:
str
- duck.utils.slug.generate_slug_from_string(text: str, separator: str = '-') str[source]ΒΆ
Generate a slug from a given string.
- Parameters:
text β The input string.
separator β The separator to be used in the generated slug (default is β-β).
- Returns:
The generated slug.
- Return type:
str
- duck.utils.slug.is_valid_slug(slug: str, separator: str = '-') bool[source]ΒΆ
Check if a string is a valid slug.
- Parameters:
slug β The slug to be checked.
separator β The separator used in the slug (default is β-β).
- Returns:
True if the string is a valid slug, False otherwise.
- Return type:
bool
- duck.utils.slug.join_slug(words: list, separator: str = '-') str[source]ΒΆ
Join a list of words into a slug.
- Parameters:
words β The list of words to be joined.
separator β The separator to use between words (default is β-β).
- Returns:
The joined slug.
- Return type:
str
- duck.utils.slug.sanitize_slug(slug: str, separator: str = '-') str[source]ΒΆ
Sanitize a slug to ensure it contains only valid characters.
- Parameters:
slug β The slug to sanitize.
separator β The separator used in the slug (default is β-β).
- Returns:
The sanitized slug.
- Return type:
str
- duck.utils.slug.slugify(text: str, separator: str = '-') str[source]ΒΆ
Convert a string to a URL-friendly slug.
- Parameters:
text β The input string to be converted.
separator β The character to replace spaces with (default is β-β).
- Returns:
The generated slug.
- Return type:
str
- duck.utils.slug.split_slug(slug: str, separator: str = '-') list[source]ΒΆ
Split a slug into individual words.
- Parameters:
slug β The slug to be split.
separator β The separator used in the slug (default is β-β).
- Returns:
The list of words in the slug.
- Return type:
list
- duck.utils.slug.truncate_slug(slug: str, max_length: int, separator: str = '-') str[source]ΒΆ
Truncate a slug to a specified maximum length.
- Parameters:
slug β The slug to truncate.
max_length β The maximum allowed length of the slug.
separator β The separator used in the slug (default is β-β).
- Returns:
The truncated slug.
- Return type:
str