diff --git a/CrossDown.py b/CrossDown.py index 782558a..8dcb044 100644 --- a/CrossDown.py +++ b/CrossDown.py @@ -254,9 +254,8 @@ class Escape: """ self.text = text self.escapes = { - i: f'\0\1\2{i}\2\1\0' for i in re.findall(r'\\(.)', text) + i: f'\0\1\2{i}\2\1\0' for i in re.findall(r'(\\.)', text) } # 找出要转义的字符 - print(self.escapes) def __call__(self, *args, **kwargs): """ @@ -266,8 +265,19 @@ class Escape: :return: 不含代码的文本 """ # TODO - for index, item in enumerate(self.escapes): # 替换代码块为-@@-(ID)-@@- - self.text = re.sub(fr'{index}', f'\0\1\2{index}\2\1\0', self.text) # 同时转译特殊字符 + for index, item in self.escapes.items(): # 替换代码块为\0\1\2(id)\2\1\0 + self.text = re.sub(fr'{re.escape(index)}', item, self.text) # 同时转译特殊字符 + print(item) + return self.text + + def back(self, text): + """ + 将被转义的字符放回文本中 + :param text: 新文本 + :return: 放回转义字符的文本 + """ + for index, item in self.escapes.items(): # 替换\0\1\2(id)\2\1\0为转义字符 + self.text = re.sub(item, fr'{index}', text) # 同时转译特殊字符 return self.text def restore(self, new_text: str): @@ -329,7 +339,7 @@ class Basic: :param text: 原始文本 :return: 移除弱注释后的文本 """ - return re.sub('// .*?\n', '', text) + return re.sub('// .*?\n', '\n', text) def paragraph(self): """ @@ -374,7 +384,8 @@ def body(text: str) -> Tuple[str, Dict[str, str]]: :param text: 输入正文 :return: 输出渲染后的正文 """ - Escape(text) + escape = Escape(text) # 转义 + text = escape() text = Basic.week_annotation(text) # 移除弱注释 text = Syllabus(text)() # 渲染提纲 text, values = Value(text)() # 提取变量并赋值到文本中 @@ -383,6 +394,7 @@ def body(text: str) -> Tuple[str, Dict[str, str]]: text = Link(text)() # 渲染特殊功能 text = Cite(text)() # 渲染引用 text = Basic(text)() # 渲染基础格式 + text = escape.back(text) # text = Basic.paragraph(text) # 渲染段落 return text, values @@ -401,9 +413,9 @@ def main(origen: str): if __name__ == '__main__': - with open('Example.mdc', encoding='utf-8') as test: + with open('README.md', encoding='utf-8') as test: cd = main(test.read()) - with open('Example.html', 'w', encoding='utf-8') as html: + with open('README.html', 'w', encoding='utf-8') as html: html.write(f"""
diff --git a/Editor.py b/Editor.py new file mode 100644 index 0000000..57a9ae4 --- /dev/null +++ b/Editor.py @@ -0,0 +1,54 @@ +import sys +from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction, QMenuBar, QMenu, QFileDialog + + +class TextEditor(QMainWindow): + def __init__(self): + super().__init__() + + # 设置窗口标题和初始大小 + self.setWindowTitle('PyQt6 Text Editor') + self.setGeometry(100, 100, 800, 600) + + # 创建文本编辑器控件 + self.text_edit = QTextEdit(self) + self.setCentralWidget(self.text_edit) + + # 创建菜单栏和文件菜单 + self.menu_bar = QMenuBar(self) + self.setMenuBar(self.menu_bar) + + file_menu = QMenu('File', self) + self.menu_bar.addMenu(file_menu) + + # 创建动作并添加到文件菜单 + open_action = QAction('Open', self) + open_action.triggered.connect(self.open_file) + file_menu.addAction(open_action) + + save_action = QAction('Save', self) + save_action.triggered.connect(self.save_file) + file_menu.addAction(save_action) + + exit_action = QAction('Exit', self) + exit_action.triggered.connect(self.close) + file_menu.addAction(exit_action) + + def open_file(self): + file_name, _ = QFileDialog.getOpenFileName(self, 'Open File', '', "Text Files (*.txt *.py)") + if file_name: + with open(file_name, 'r') as file: + self.text_edit.setPlainText(file.read()) + + def save_file(self): + file_name, _ = QFileDialog.getSaveFileName(self, 'Save File', '', "Text Files (*.txt *.py)") + if file_name: + with open(file_name, 'w') as file: + file.write(self.text_edit.toPlainText()) + + +if __name__ == '__main__': + app = QApplication(sys.argv) + editor = TextEditor() + editor.show() + sys.exit(app.exec()) diff --git a/README.html b/README.html index d4594e2..d6f6fd2 100644 --- a/README.html +++ b/README.html @@ -23,7 +23,9 @@ -自制的markdown,添加了一些自定义的语法
+效果请见README.html
\(CO^2\)
+\(CO_2\)
@@ -80,12 +85,13 @@
C-->D
4 转义
- \\
+ \
5 引用
渲染引用
6 提纲
7 注释
7.1 强注释
7.2 弱注释
只有在
+ // 代码中的注释弱不会被移除
diff --git a/README.md b/README.md
index ec6ebe6..1d6ed4e 100644
--- a/README.md
+++ b/README.md
@@ -23,8 +23,11 @@
___
***
1.3 链接
-1.3.1 [链接文本](链接地址)
-1.3.2 ![链接图片](链接地址)
+1.3.1 普通链接
+[链接文本](链接地址)
+[CrossDark](https://crossdark.com)
+1.3.2 图片
+![链接图片](链接地址)
2 变量
2.1 定义
{变量名} = 值
@@ -35,7 +38,7 @@ ___
3 代码块
3.1 `单行`
3.1.1 LaTex
-`$CO^2$`
+`$CO_2$`
3.2 多行
3.2.1 YAML
`
@@ -73,3 +76,4 @@ graph LR
=|
7.2 弱注释
只有在 // 后面才会被移除
+`// 代码中的注释弱不会被移除`