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_slug

Clean up a slug by ensuring it’s lowercase and properly formatted.

generate_slug_from_string

Generate a slug from a given string.

is_valid_slug

Check if a string is a valid slug.

join_slug

Join a list of words into a slug.

sanitize_slug

Sanitize a slug to ensure it contains only valid characters.

slugify

Convert a string to a URL-friendly slug.

split_slug

Split a slug into individual words.

truncate_slug

Truncate a slug to a specified maximum length.

unslugify

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

duck.utils.slug.unslugify(slug: str, separator: str = '-') β†’ str[source]ΒΆ

Convert a slug back to a normal string.

Parameters:
  • slug – The slug to be converted.

  • separator – The separator used in the slug (default is β€œ-”).

Returns:

The original string.

Return type:

str