forked from yym68686/ChatGPT-Telegram-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd2tgmd.py
144 lines (119 loc) · 3.56 KB
/
md2tgmd.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import re
def find_all_index(str, pattern):
index_list = [0]
for match in re.finditer(pattern, str, re.MULTILINE):
if match.group(1) != None:
start = match.start(1)
end = match.end(1)
index_list += [start, end]
index_list.append(len(str))
return index_list
def replace_all(text, pattern, function):
poslist = [0]
strlist = []
originstr = []
poslist = find_all_index(text, pattern)
for i in range(1, len(poslist[:-1]), 2):
start, end = poslist[i:i+2]
strlist.append(function(text[start:end]))
for i in range(0, len(poslist), 2):
j, k = poslist[i:i+2]
originstr.append(text[j:k])
if len(strlist) < len(originstr):
strlist.append('')
else:
originstr.append('')
new_list = [item for pair in zip(originstr, strlist) for item in pair]
return ''.join(new_list)
def escapeshape(text):
return '▎*' + text.split()[1] + '*'
def escapeminus(text):
return '\\' + text
def escapebackquote(text):
return r'\`\`'
def escapeplus(text):
return '\\' + text
def escape(text, flag=0):
# In all other places characters
# _ * [ ] ( ) ~ ` > # + - = | { } . !
# must be escaped with the preceding character '\'.
if flag:
text = re.sub(r"\\\\", '@@@', text)
text = re.sub(r"\\", r"\\\\", text)
if flag:
text = re.sub(r"\@{3}", r"\\\\", text)
text = re.sub(r"_", '\_', text)
text = re.sub(r"\*{2}(.*?)\*{2}", '@@@\\1@@@', text)
text = re.sub(r"\n{1,2}\*\s", '\n\n• ', text)
text = re.sub(r"\*", '\*', text)
text = re.sub(r"\@{3}(.*?)\@{3}", '*\\1*', text)
text = re.sub(r"\!?\[(.*?)\]\((.*?)\)", '@@@\\1@@@^^^\\2^^^', text)
text = re.sub(r"\[", '\[', text)
text = re.sub(r"\]", '\]', text)
text = re.sub(r"\(", '\(', text)
text = re.sub(r"\)", '\)', text)
text = re.sub(r"\@{3}(.*?)\@{3}\^{3}(.*?)\^{3}", '[\\1](\\2)', text)
text = re.sub(r"~", '\~', text)
text = re.sub(r">", '\>', text)
text = replace_all(text, r"(^#+\s.+?$)|```[\D\d\s]+?```", escapeshape)
text = re.sub(r"#", '\#', text)
text = replace_all(text, r"(\+)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`", escapeplus)
text = re.sub(r"\n{1,2}(\s*)-\s", '\n\n\\1• ', text)
text = re.sub(r"\n{1,2}(\s*\d{1,2}\.\s)", '\n\n\\1', text)
text = replace_all(text, r"(-)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`", escapeminus)
text = re.sub(r"```([\D\d\s]+?)```", '@@@\\1@@@', text)
text = replace_all(text, r"(``)", escapebackquote)
text = re.sub(r"\@{3}([\D\d\s]+?)\@{3}", '```\\1```', text)
text = re.sub(r"=", '\=', text)
text = re.sub(r"\|", '\|', text)
text = re.sub(r"{", '\{', text)
text = re.sub(r"}", '\}', text)
text = re.sub(r"\.", '\.', text)
text = re.sub(r"!", '\!', text)
return text
text = r'''
# title
**bold**
```
# comment
print(qwer) # ferfe
ni1
```
# bn
# b
# Header
## Subheader
[1.0.0](http://version.com)

- item 1 -
- item 1 -
- item 1 -
* item 2 #
* item 3 ~
1. item 1
2. item 2
1. item 1
2. item 2
sudo apt install mesa-utils # 安装
```python
# comment
print("1.1\n")_
\subsubsection{1.1}
```
\subsubsection{1.1}
And simple text `with-ten` `with+ten` + some - **symbols**. # `with-ten`里面的`-`不会被转义
```
print("Hello, World!") -
```
Cxy = abs (Pxy)**2/ (Pxx*Pyy)
`a`a-b-c`n`
`-a----++++`++a-b-c`-n-`
`[^``]*`a``b-c``d``
# pattern = r"`[^`]*`-([^`-]*)``
w`-a----`ccccc`-n-`bbbb``a
'''
if __name__ == '__main__':
import os
os.system('clear')
text = escape(text)
print(text)