-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslugify
executable file
·67 lines (60 loc) · 1.37 KB
/
slugify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import argparse
import re
import sys
# https://en.wikipedia.org/wiki/Romanization_of_Russian
# System: Passport (2013), ICAO
TRANSLIT_RU_EN = str.maketrans({
'а': 'a',
'б': 'b',
'в': 'v',
'г': 'g',
'д': 'd',
'е': 'e',
'ё': 'e',
'ж': 'zh',
'з': 'z',
'и': 'i',
'й': 'i',
'к': 'k',
'л': 'l',
'м': 'm',
'н': 'n',
'о': 'o',
'п': 'p',
'р': 'r',
'с': 's',
'т': 't',
'у': 'u',
'ф': 'f',
'х': 'kh',
'ц': 'ts',
'ч': 'ch',
'ш': 'sh',
'щ': 'shch',
'ъ': 'ie',
'ы': 'y',
'ь': None,
'ю': 'iu',
'я': 'ua',
})
def get_names(args):
if args.words:
return args.words
return [line.rstrip() for line in sys.stdin]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--translit", action='store_true', help="Transliterate Russian")
parser.add_argument("words", nargs="*", help="Words to slugify")
args = parser.parse_args()
pattern = r"[^\w\d]+"
for name in get_names(args):
slug = re.sub(pattern, "-", name)
slug = re.sub(pattern + "$", "", slug)
slug = re.sub("^" + pattern, "", slug)
slug = slug.lower()
if args.translit:
slug = slug.translate(TRANSLIT_RU_EN)
print(slug)
if __name__ == "__main__":
main()