Skip to content

Commit

Permalink
Add slugify to utils (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelho authored Aug 6, 2022
1 parent 9fa4839 commit b38b345
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions sdk/python/flet/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import platform
import sys
import unicodedata
import webbrowser


Expand Down Expand Up @@ -82,3 +83,23 @@ def is_localhost_url(url):
def get_current_script_dir():
pathname = os.path.dirname(sys.argv[0])
return os.path.abspath(pathname)


def slugify(original: str) -> str:
"""
Make a string url friendly. Useful for creating routes for navigation.
>>> slugify("What's up?")
'whats-up'
>>> slugify(" Mitä kuuluu? ")
'mitä-kuuluu'
"""
slugified = original.strip()
slugified = " ".join(slugified.split()) # Remove extra spaces between words
slugified = slugified.lower()
# Remove unicode punctuation
slugified = "".join(character for character in slugified if not unicodedata.category(character).startswith("P"))
slugified = slugified.replace(" ", "-")

return slugified

0 comments on commit b38b345

Please sign in to comment.