20 lines
656 B
Python
20 lines
656 B
Python
import re
|
||
|
||
|
||
TRANSLIT_TABLE = {
|
||
'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', 'е': 'e', 'ё': 'e',
|
||
'ж': 'zh', 'з': 'z', 'и': 'i', 'й': 'y', 'к': 'k', 'л': 'l', 'м': 'm',
|
||
'н': 'n', 'о': 'o', 'п': 'p', 'р': 'r', 'с': 's', 'т': 't', 'у': 'u',
|
||
'ф': 'f', 'х': 'h', 'ц': 'c', 'ч': 'ch', 'ш': 'sh', 'щ': 'sch', 'ъ': '',
|
||
'ы': 'y', 'ь': '', 'э': 'e', 'ю': 'yu', 'я': 'ya',
|
||
}
|
||
|
||
|
||
def slugify(text: str) -> str:
|
||
text = text.lower()
|
||
for ru, en in TRANSLIT_TABLE.items():
|
||
text = text.replace(ru, en)
|
||
text = re.sub(r'[^a-z0-9]+', '-', text)
|
||
text = re.sub(r'^-+|-+$', '', text)
|
||
return text
|