From 3096474dbb16cd1344b9136d29ed9db35f477299 Mon Sep 17 00:00:00 2001 From: crossdark Date: Sun, 15 Sep 2024 20:51:20 +0800 Subject: [PATCH] =?UTF-8?q?V0.7=E7=94=A8=E4=BA=86markdown=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CrossDown.py | 35 +++++++++++++++++++++-------------- Editor.py | 3 ++- MarkDown.py | 7 +++++++ README.html | Bin 3990 -> 4054 bytes 4 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 MarkDown.py diff --git a/CrossDown.py b/CrossDown.py index cd23c4e..4fdf09d 100644 --- a/CrossDown.py +++ b/CrossDown.py @@ -1,6 +1,10 @@ from typing import * import re +import markdown + +import MarkDown + class Header: def __init__(self, text: str): @@ -107,14 +111,14 @@ class Style: :param kwargs: :return: """ - self.bold() - self.italic() - self.strikethrough() - self.underline() - self.highlight() + # self.bold() + # self.italic() + # self.strikethrough() + # self.underline() + # self.highlight() self.up() self.hide() - self.split_line() + # self.split_line() return self.text @@ -265,7 +269,7 @@ class Escape: # TODO 还有点问题 :return: 不含代码的文本 """ 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) # 同时转译特殊字符 + self.text = re.sub(fr'{re.escape(index)}', re.escape(item), self.text) # 同时转译特殊字符 print(item) return self.text @@ -276,7 +280,8 @@ class Escape: # TODO 还有点问题 :return: 放回转义字符的文本 """ for index, item in self.escapes.items(): # 替换\0\1\2(id)\2\1\0为转义字符 - self.text = re.sub(item, fr'{index}', text) # 同时转译特殊字符 + print(item) + self.text = re.sub(item, '', text) # 同时转译特殊字符 return self.text def restore(self, new_text: str): @@ -314,7 +319,8 @@ class Syllabus: def __call__(self, *args, **kwargs): for num, txt in self.syllabus.items(): - self.text = re.sub(f'{".".join(num)} {re.escape(txt)}', f'{".".join(num)} {txt}{{#' + '.'.join(num) + f'}}', self.text) # 按照层级为提纲添加不同等级的标题并创建锚点 + self.text = re.sub(f'{".".join(num)} {re.escape(txt)}', f'{"#" * len(num)}{".".join(num)} {txt}{{#' + '.'.join(num) + f'}}\n', self.text) # 按照层级为提纲添加不同等级的标题并创建锚点 + print(self.text) return self.text @@ -388,12 +394,13 @@ def body(text: str) -> Tuple[str, Dict[str, str]]: text = Basic.week_annotation(text) # 移除弱注释 text = Syllabus(text)() # 渲染提纲 text, values = Value(text)() # 提取变量并赋值到文本中 - text = Header(text)() # 渲染标题 + # text = Header(text)() # 渲染标题 text = Style(text)() # 渲染字体样式 - text = Link(text)() # 渲染特殊功能 - text = Cite(text)() # 渲染引用 - text = Basic(text)() # 渲染基础格式 - text = escape.back(text) + # text = Link(text)() # 渲染特殊功能 + # text = Cite(text)() # 渲染引用 + # text = Basic(text)() # 渲染基础格式 + text = markdown.markdown(text, extensions=['markdown.extensions.extra']) # 渲染标准markdown + text = escape.back(text) # 放回被转义的字符 # text = Basic.paragraph(text) # 渲染段落 return text, values diff --git a/Editor.py b/Editor.py index 57a9ae4..b257436 100644 --- a/Editor.py +++ b/Editor.py @@ -1,5 +1,6 @@ import sys -from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction, QMenuBar, QMenu, QFileDialog +from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit, QMenuBar, QMenu, QFileDialog +from PyQt6.QtGui import QAction class TextEditor(QMainWindow): diff --git a/MarkDown.py b/MarkDown.py new file mode 100644 index 0000000..1cea752 --- /dev/null +++ b/MarkDown.py @@ -0,0 +1,7 @@ +import markdown + + +with open('README.md', encoding='utf-8') as text: + html = markdown.markdown(text.read(), extensions=['markdown.extensions.extra']) + print(html) + diff --git a/README.html b/README.html index f8d7c466ce85c843841be2b804f38f7cd84a1ce3..61d0348ed0dfece2a1c8c8d59c0fae23cc3f9daf 100644 GIT binary patch delta 464 zcmbOxe@%Xa9SbX$0uXHWVwuOto0@C)Y}%YBdneoIr{+%n%&I3^TvC*umu~lb({!*X zggaS>O-~UjV*}I!ltpN>v(bmD%4gH%u6weh`)S9d=ezdTO2TTFzFa-y$*y&_wwu-2%NgYq^zFbdvdJjI!koOEBb}+hZZa=tB(sfv!Q@m< z2ld395?iI0llMLATl#eNzUQ6oN(#kA$#9`L8=lT>P*TvJe2VisNb3PE8L-ydTpRdp z3hZJuoc-gCVl;uK*-f6zoyll7c`uLjWKo_%u&P!bE3k+Un8(3u1v0{sHx}nu05s9tE$N)C?JFm3^#8(A&&vtBjHh=QdJyV}gS)rh-8x3JSo4fSc zt_iUq)ewdHNjdq+*@dO~C8-dR$)$XUC)@D5i-XnJ=qKf;RN5&haM|c*l;q~vaRC5y CTK delta 403 zcmca6KTUpv9Se_*et{jA0ua~~*ll)Zna9YTY4>c}oF{uH+vsObe#ffEon-fX({wOD zX|f@ko@l0>4Oj@KD#;F{vVl!kz^2sh$&T))9h08#+Ha#@YBzZYn~s1@vE9>-1uthV z0Sgw}P5#KHBW#nKSd?w|a`lWSyVlw0gE^bk*~=L@?QAlNzDm0rPxnlHK4pbXT7G^>D$tq9nI)-q5V2=-mpoD?6?5&uAC(R