Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: un33k/python-slugify
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v6.0.1
Choose a base ref
...
head repository: un33k/python-slugify
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v6.1.0
Choose a head ref

Commits on Feb 16, 2022

  1. github actions

    un33k committed Feb 16, 2022
    Copy the full SHA
    45e0ae3 View commit details
  2. simplify action

    un33k committed Feb 16, 2022
    Copy the full SHA
    6404c1d View commit details
  3. git actions

    un33k committed Feb 16, 2022
    Copy the full SHA
    9b9b68f View commit details
  4. version file, github action

    un33k committed Feb 16, 2022
    Copy the full SHA
    ad33581 View commit details
  5. actions

    un33k committed Feb 16, 2022
    Copy the full SHA
    614f183 View commit details
  6. fix coverage

    un33k committed Feb 16, 2022
    Copy the full SHA
    d5e3586 View commit details
  7. actions

    un33k committed Feb 16, 2022
    Copy the full SHA
    9ada774 View commit details
  8. version

    un33k committed Feb 16, 2022
    Copy the full SHA
    7534199 View commit details
  9. manifest

    un33k committed Feb 16, 2022
    Copy the full SHA
    97915d9 View commit details
  10. github action

    un33k committed Feb 16, 2022
    Copy the full SHA
    86161b4 View commit details
  11. setup

    un33k committed Feb 16, 2022
    Copy the full SHA
    8ea91da View commit details
  12. coverall coverage

    un33k committed Feb 16, 2022
    Copy the full SHA
    7c4802e View commit details
  13. coverall omit

    un33k committed Feb 16, 2022
    Copy the full SHA
    c23bbc1 View commit details
  14. coverage run

    un33k committed Feb 16, 2022
    Copy the full SHA
    57ee951 View commit details
  15. remove tox

    un33k committed Feb 16, 2022
    Copy the full SHA
    28ac37c View commit details
  16. clean readme

    un33k committed Feb 16, 2022
    Copy the full SHA
    341aa39 View commit details
  17. clean up

    un33k committed Feb 16, 2022
    Copy the full SHA
    bfb5170 View commit details
  18. change log

    un33k committed Feb 16, 2022
    Copy the full SHA
    e6e488d View commit details
  19. update changelog

    un33k committed Feb 16, 2022
    Copy the full SHA
    f4c176e View commit details
  20. Merge branch 'sandbox' into ci

    un33k committed Feb 16, 2022
    Copy the full SHA
    401487e View commit details
  21. add staging to ci

    un33k committed Feb 16, 2022
    Copy the full SHA
    b2aaccd View commit details
  22. Fix misleading pattern name and documentation (#109)

    * Add better typing for slugify.slugify
    
    Currently, mypy understands the type as `Iterable[str]`, which doesn't match what should actually be passed in, which is `Iterable[Iterable[str]]` or, ideally, `Iterable[Tuple[str, str]]`
    
    * whitespace around =
    
    * fix misleading pattern name and documentation
    
    * fix README.md and cli doc as well
    
    Co-authored-by: Fahrzin Hemmati <fahhem@users.noreply.github.com>
    Co-authored-by: Val Neekman (AvidCoder) <un33kvu@gmail.com>
    3 people authored Feb 16, 2022
    Copy the full SHA
    dce2189 View commit details
  23. regex_patter to disallow

    un33k committed Feb 16, 2022
    Copy the full SHA
    d8c9d8a View commit details
  24. Merge branch 'master' into ci

    un33k committed Feb 16, 2022
    Copy the full SHA
    07b87da View commit details

Commits on Feb 22, 2022

  1. allow unicode (#111)

    * initial commit to allow unicode
    
    * update version and changelog
    
    * add the flag to the CLI
    
    * update README.md
    mrezzamoradi authored Feb 22, 2022
    Copy the full SHA
    d968ca7 View commit details
Showing with 328 additions and 7 deletions.
  1. +4 −0 CHANGELOG.md
  2. +15 −1 README.md
  3. +4 −1 slugify/__main__.py
  4. +1 −1 slugify/__version__.py
  5. +16 −4 slugify/slugify.py
  6. +288 −0 test.py
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.1.0

- Add `allow_unicode` flag to allow unicode characters in the slug

## 6.0.1

- Rework regex_pattern to mean the opposite (disallowed chars instead of allowed)
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -42,7 +42,8 @@ def slugify(
stopwords=(),
regex_pattern=None,
lowercase=True,
replacements=()
replacements=(),
allow_unicode=False
):
"""
Make a slug from the given text.
@@ -58,6 +59,7 @@ def slugify(
: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
"""
```
@@ -75,6 +77,10 @@ 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")
@@ -133,6 +139,14 @@ txt = 'ÜBER Über German Umlaut'
r = slugify(txt, replacements=[['Ü', 'UE'], ['ü', 'ue']])
self.assertEqual(r, "ueber-ueber-german-umlaut")

txt = 'i love 🦄'
r = slugify(txt, allow_unicode=True)
self.assertEqual(r, "i-love")

txt = 'i love 🦄'
r = slugify(txt, allow_unicode=True, regex_pattern=r'[^🦄]+')
self.assertEqual(r, "🦄")

```

For more examples, have a look at the [test.py](test.py) file.
5 changes: 4 additions & 1 deletion slugify/__main__.py
Original file line number Diff line number Diff line change
@@ -36,6 +36,8 @@ def parse_args(argv):
help="Activate case sensitivity")
parser.add_argument("--replacements", nargs='+',
help="""Additional replacement rules e.g. "|->or", "%%->percent".""")
parser.add_argument("--allow-unicode", action='store_true', default=False,
help="Allow unicode characters")

args = parser.parse_args(argv[1:])

@@ -73,7 +75,8 @@ def slugify_params(args):
separator=args.separator,
stopwords=args.stopwords,
lowercase=args.lowercase,
replacements=args.replacements
replacements=args.replacements,
allow_unicode=args.allow_unicode
)


2 changes: 1 addition & 1 deletion slugify/__version__.py
Original file line number Diff line number Diff line change
@@ -5,4 +5,4 @@
__url__ = 'https://github.com/un33k/python-slugify'
__license__ = 'MIT'
__copyright__ = 'Copyright 2022 Val Neekman @ Neekware Inc.'
__version__ = '6.0.1'
__version__ = '6.1.0'
20 changes: 16 additions & 4 deletions slugify/slugify.py
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
HEX_PATTERN = re.compile(r'&#x([\da-fA-F]+);')
QUOTE_PATTERN = re.compile(r'[\']+')
DISALLOWED_CHARS_PATTERN = re.compile(r'[^-a-zA-Z0-9]+')
DISALLOWED_UNICODE_CHARS_PATTERN = re.compile(r'[\W_]+')
DUPLICATE_DASH_PATTERN = re.compile(r'-{2,}')
NUMBERS_PATTERN = re.compile(r'(?<=\d),(?=\d)')
DEFAULT_SEPARATOR = '-'
@@ -66,7 +67,8 @@ def smart_truncate(string, max_length=0, word_boundary=False, separator=' ', sav

def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False,
separator=DEFAULT_SEPARATOR, save_order=False, stopwords=(), regex_pattern=None, lowercase=True,
replacements: typing.Iterable[typing.Iterable[str]] = ()):
replacements: typing.Iterable[typing.Iterable[str]] = (),
allow_unicode=False):
"""
Make a slug from the given text.
:param text (str): initial text
@@ -81,6 +83,7 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
: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):
"""

@@ -97,7 +100,8 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
text = QUOTE_PATTERN.sub(DEFAULT_SEPARATOR, text)

# decode unicode
text = unidecode.unidecode(text)
if not allow_unicode:
text = unidecode.unidecode(text)

# ensure text is still in unicode
if not isinstance(text, str):
@@ -122,7 +126,11 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
pass

# translate
text = unicodedata.normalize('NFKD', text)
if allow_unicode:
text = unicodedata.normalize('NFKC', text)
else:
text = unicodedata.normalize('NFKD', text)

if sys.version_info < (3,):
text = text.encode('ascii', 'ignore')

@@ -137,7 +145,11 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
text = NUMBERS_PATTERN.sub('', text)

# replace all other unwanted characters
pattern = regex_pattern or DISALLOWED_CHARS_PATTERN
if allow_unicode:
pattern = regex_pattern or DISALLOWED_UNICODE_CHARS_PATTERN
else:
pattern = regex_pattern or DISALLOWED_CHARS_PATTERN

text = re.sub(pattern, DEFAULT_SEPARATOR, text)

# remove redundant
Loading