From a12b5f60077fd96969858013ff81f5b7f07bb708 Mon Sep 17 00:00:00 2001 From: yym68686 Date: Wed, 31 Jan 2024 17:41:01 +0800 Subject: [PATCH] Add feature: Add dependencies, split md2tgmd into a separate project and upload it to pypi --- bot.py | 2 +- requirements.txt | 1 + utils/md2tgmd.py | 153 ----------------------------------------------- 3 files changed, 2 insertions(+), 154 deletions(-) delete mode 100644 utils/md2tgmd.py diff --git a/bot.py b/bot.py index 4f81d8ec..81826c69 100644 --- a/bot.py +++ b/bot.py @@ -4,7 +4,7 @@ import logging import traceback import utils.decorators as decorators -from utils.md2tgmd import escape +from md2tgmd import escape from utils.chatgpt2api import Chatbot as GPT from utils.chatgpt2api import claudebot from utils.prompt import translator_en2zh_prompt, translator_prompt diff --git a/requirements.txt b/requirements.txt index b699b22d..cd2d9fd1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ --index-url https://pypi.python.org/simple/ tiktoken requests +md2tgmd # jieba python-dotenv beautifulsoup4 diff --git a/utils/md2tgmd.py b/utils/md2tgmd.py deleted file mode 100644 index ea9052b1..00000000 --- a/utils/md2tgmd.py +++ /dev/null @@ -1,153 +0,0 @@ -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 '\'. - text = re.sub(r"\\\[", '@->@', text) - text = re.sub(r"\\\]", '@<-@', text) - text = re.sub(r"\\\(", '@-->@', text) - text = re.sub(r"\\\)", '@<--@', text) - 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"\@\-\>\@", '\[', 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) -![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` -\[ E[X^4] = \int_{-\infty}^{\infty} x^4 f(x) dx \] - -`-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) \ No newline at end of file