-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmd.py
52 lines (48 loc) · 1.85 KB
/
md.py
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
# This is mainly Zeon's contribution, not mine
def mdParse(content, is_slack):
content = convertLinks(content, is_slack)
content = convertBold(content, is_slack)
if is_slack:
content = content.replace("<", "<").replace(">", ">").replace("&", "&")
return content
def convertLinks(content, is_slack):
if is_slack:
found_links = content.split('<')
for raw in found_links:
try:
if raw.startswith('http') != True:
continue
content = content.replace("<"+raw.split('>')[0]+ ">", f'[{raw.split('|')[1].split('>')[0]}]({raw.split("|")[0]})')
except IndexError:
pass
else:
found_links = content.split('[')
for raw in found_links:
try:
if raw.split("(")[1].startswith("http") != True:
continue
content = content.replace("[" + raw.split(")")[0] + ")", f"<{raw.split("(")[1].split(')')[0]}|{raw.split("]")[0]}>")
except IndexError:
pass
return content
def convertBold(content, is_slack):
if is_slack:
found_links = content.split('*')
i = 0
for raw in found_links:
if i % 2 != 0:
content = content.replace(f'*{raw}*', f'**{raw}**')
# print(i)
i += 1
else:
found_links = content.split('**')
i = 0
for raw in found_links:
if i % 2 != 0:
content = content.replace(f'**{raw}**', f'*{raw}*')
#print(raw)
i += 1
return content
pass
# examplestrSlack = " txtbeforelink0 <https://saahild.com|test content> txtafterlink0 txtbef1 <https://zeon.saahild.com|zeon> txtaft1 *Bold text* *bt2*"
# print(mdParse(examplestrSlack, True))