diff options
author | neodarz <neodarz@neodarz.net> | 2019-05-26 16:29:43 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-05-26 16:29:43 +0200 |
commit | 3fbed66ded190db27d67b006172f10bb03f54cc7 (patch) | |
tree | 6490471ad1d71273b8d890bb063b49c20ab8d8cd /utils/utils.py | |
parent | 6f36091aa8bf5d4ae446346a945c6217c2493e97 (diff) | |
download | my_new_personal_website-3fbed66ded190db27d67b006172f10bb03f54cc7.tar.xz my_new_personal_website-3fbed66ded190db27d67b006172f10bb03f54cc7.zip |
Move sanitize to external file
Diffstat (limited to 'utils/utils.py')
-rw-r--r-- | utils/utils.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/utils/utils.py b/utils/utils.py index 44cf8e59..db4bab57 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -201,3 +201,17 @@ def static_vars(**kwargs): setattr(func, k, kwargs[k]) return func return decorate + + +def sanitize(string): + """Sanitize string (title) for URI consumption.""" + if isinstance(string, bytes): + string = string.decode('utf-8') + # to lowercase + string = string.lower() + # strip all non-word, non-hyphen and non-whitespace characters + string = re.sub(r"[^\w\s-]", "", string) + # replace consecutive whitespaces with a single hyphen + string = re.sub(r"\s+", "-", string) + # percent encode the result + return urllib.parse.quote(string) |