kwutil.slugify_ext module

Fork of python-slugify.

https://pypi.org/project/python-slugify/1.2.2/

kwutil.slugify_ext.slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False, separator='-', save_order=False, stopwords=(), regex_pattern=None, lowercase=True, replacements=(), trunc_loc=1.0)[source]

Make a slug from the given text. :param text (str): initial text :param entities (bool): converts html entities to unicode :param decimal (bool): converts html decimal to unicode :param hexadecimal (bool): converts html hexadecimal to unicode :param max_length (int): output string length :param word_boundary (bool): truncates to complete word even if length ends up shorter than max_length :param save_order (bool): if parameter is True and max_length > 0 return whole words in the initial order :param separator (str): separator between words :param stopwords (iterable): words to discount :param regex_pattern (str): regex pattern for allowed characters :param lowercase (bool): activate case sensitivity by setting it to False :param replacements (iterable): list of replacement rules e.g. [[‘|’, ‘or’], [‘%’, ‘percent’]] :return (str):

# Example: # >>> from kwutil.slugify_ext import slugify # NOQA # >>> import ubelt as ub # >>> text = ub.cmd(‘diceware -n 12’)[‘out’].strip() # >>> print(‘text = {!r}’.format(text)) # >>> slug = slugify(text, max_length=10, lowercase=0, trunc_loc=1.0) # >>> print(‘slug = {!r}’.format(slug)) # >>> slug = slugify(text, max_length=10, lowercase=0, trunc_loc=0.8) # >>> print(‘slug = {!r}’.format(slug)) # >>> slug = slugify(text, max_length=10, lowercase=0, trunc_loc=0.5) # >>> print(‘slug = {!r}’.format(slug)) # >>> slug = slugify(text, max_length=10, lowercase=0, trunc_loc=0.2) # >>> print(‘slug = {!r}’.format(slug)) # >>> slug = slugify(text, max_length=10, lowercase=0, trunc_loc=0.0) # >>> print(‘slug = {!r}’.format(slug))

kwutil.slugify_ext.smart_truncate(string, max_length=0, word_boundary=False, separator=' ', save_order=False, trunc_loc=0.5, hash_len=None, head='~', tail='~')[source]

Truncate a string to a maximum length, replacing the truncated part with a hash representing the removed part.

Parameters:
  • string (str) – string for modification

  • max_length (int) – output string length

  • word_boundary (bool)

  • save_order (bool) – if True then word order of output string is like input string

  • separator (str) – separator between words

  • trunc_loc (float) – fraction of location where to remove the text

Returns:

str

Example

>>> from kwutil.slugify_ext import smart_truncate
>>> print(smart_truncate('f' * 100, max_length=10, trunc_loc=0.0))
>>> print(smart_truncate('f' * 100, max_length=10, trunc_loc=0.5))
>>> print(smart_truncate('f' * 100, max_length=10, trunc_loc=1.0))
~944b6~fff
ff~944b6~fff
fff~944b6~