single |
# Python Slugify
**A Python slugify application that handles unicode**.
[![status-image]][status-link]
[![version-image]][version-link]
[![coverage-image]][coverage-link]
# Overview
**Best attempt** to create slugs from unicode strings while keeping it
**DRY**.
# Notice
This module, by default installs and uses [text-unidecode] _(GPL & Perl
Artistic)_ for its decoding needs.
However, there is an alternative decoding package called [Unidecode]
_(GPL)_. It can be installed as `python-slugify[unidecode]` for those who
prefer it. `Unidecode` is believed to be more [advanced].
### `Official` Support Matrix
| Python | Slugify |
| -------------- | ------------------ |
| `>= 2.7 < 3.6` | `< 5.0.0` |
| `>= 3.6 < 3.7` | `>= 5.0.0 < 7.0.0` |
| `>= 3.7` | `>= 7.0.0` |
# How to install
easy_install python-slugify |OR| easy_install python-slugify[unidecode]
-- OR --
pip install python-slugify |OR| pip install python-slugify[unidecode]
# Options
```python
def slugify(
text: str,
entities: bool = True,
decimal: bool = True,
hexadecimal: bool = True,
max_length: int = 0,
word_boundary: bool = False,
separator: str = DEFAULT_SEPARATOR,
save_order: bool = False,
stopwords: Iterable[str] = (),
regex_pattern: str | None = None,
lowercase: bool = True,
replacements: Iterable[Iterable[str]] = (),
allow_unicode: bool = False,
) -> str:
"""
Make a slug from the given text.
:param text (str): initial text
:param entities (bool): converts html entities to unicode (foo & bar
-> foo-bar)
:param decimal (bool): converts html decimal to unicode (Ž -> Ž ->
z)
:param hexadecimal (bool): converts html hexadecimal to unicode (Ž
-> Ž -> z)
:param max_length (int): output string length
:param word_boundary (bool): truncates to end of full words (length may
be 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 disallowed characters
:param lowercase (bool): activate case sensitivity by setting it to False
:param replacements (iterable): list of replacement rules e.g. [['|',
'or'], ['%', 'percent']]
:param allow_unicode (bool): allow unicode characters
:return (str): slugify text
"""
```
# How to use
```python
from slugify import slugify
txt = "This is a test ---"
r = slugify(txt)
self.assertEqual(r, "this-is-a-test")
txt = '影師嗎'
r = slugify(txt)
self.assertEqual(r, "ying-shi-ma")
txt = '影師嗎'
r = slugify(txt, allow_unicode=True)
self.assertEqual(r, "影師嗎")
txt = 'C\'est déjà l\'été.'
r = slugify(txt)
self.assertEqual(r, "c-est-deja-l-ete")
|