From 2596f7ae7787bc76e355ee37de0375aac009351a Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 16 Apr 2021 18:32:44 +0200 Subject: [PATCH] tools (common/regex): move code to make user friendly URLs from web frontend --- sat/tools/common/regex.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/sat/tools/common/regex.py b/sat/tools/common/regex.py index 22fd1cf9..82f2430f 100644 --- a/sat/tools/common/regex.py +++ b/sat/tools/common/regex.py @@ -20,12 +20,17 @@ """ regex tools common to backend and frontends """ import re +import unicodedata path_escape = {"%": "%25", "/": "%2F", "\\": "%5c"} path_escape_rev = {re.escape(v): k for k, v in path_escape.items()} path_escape = {re.escape(k): v for k, v in path_escape.items()} #  thanks to Martijn Pieters (https://stackoverflow.com/a/14693789) -RE_ANSI_REMOVE = re.compile(r"\x1b[^m]*m") +RE_ANSI_REMOVE = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') +RE_TEXT_URL = re.compile(r'[^a-zA-Z0-9,_]+') +TEXT_MAX_LEN = 60 +# min lenght is currently deactivated +TEXT_WORD_MIN_LENGHT = 0 def reJoin(exps): @@ -73,3 +78,18 @@ def ansiRemove(string): @return (str, unicode): string without ANSI escape codes """ return RE_ANSI_REMOVE.sub("", string) + + +def urlFriendlyText(text): + """Convert text to url-friendly one""" + # we change special chars to ascii one, + # trick found at https://stackoverflow.com/a/3194567 + text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore').decode('utf-8') + text = RE_TEXT_URL.sub(' ', text).lower() + text = '-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT]) + while len(text) > TEXT_MAX_LEN: + if '-' in text: + text = text.rsplit('-', 1)[0] + else: + text = text[:TEXT_MAX_LEN] + return text