V0.7用了markdown库

This commit is contained in:
跨越晨昏 2024-09-15 20:51:20 +08:00
parent a11153182a
commit 3096474dbb
4 changed files with 30 additions and 15 deletions

View File

@ -1,6 +1,10 @@
from typing import * from typing import *
import re import re
import markdown
import MarkDown
class Header: class Header:
def __init__(self, text: str): def __init__(self, text: str):
@ -107,14 +111,14 @@ class Style:
:param kwargs: :param kwargs:
:return: :return:
""" """
self.bold() # self.bold()
self.italic() # self.italic()
self.strikethrough() # self.strikethrough()
self.underline() # self.underline()
self.highlight() # self.highlight()
self.up() self.up()
self.hide() self.hide()
self.split_line() # self.split_line()
return self.text return self.text
@ -265,7 +269,7 @@ class Escape: # TODO 还有点问题
:return: 不含代码的文本 :return: 不含代码的文本
""" """
for index, item in self.escapes.items(): # 替换代码块为\0\1\2(id)\2\1\0 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) print(item)
return self.text return self.text
@ -276,7 +280,8 @@ class Escape: # TODO 还有点问题
:return: 放回转义字符的文本 :return: 放回转义字符的文本
""" """
for index, item in self.escapes.items(): # 替换\0\1\2(id)\2\1\0为转义字符 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 return self.text
def restore(self, new_text: str): def restore(self, new_text: str):
@ -314,7 +319,8 @@ class Syllabus:
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
for num, txt in self.syllabus.items(): for num, txt in self.syllabus.items():
self.text = re.sub(f'{".".join(num)} {re.escape(txt)}', f'<h{len(num)}>{".".join(num)} {txt}{{#' + '.'.join(num) + f'}}</h{len(num)}>', 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 return self.text
@ -388,12 +394,13 @@ def body(text: str) -> Tuple[str, Dict[str, str]]:
text = Basic.week_annotation(text) # 移除弱注释 text = Basic.week_annotation(text) # 移除弱注释
text = Syllabus(text)() # 渲染提纲 text = Syllabus(text)() # 渲染提纲
text, values = Value(text)() # 提取变量并赋值到文本中 text, values = Value(text)() # 提取变量并赋值到文本中
text = Header(text)() # 渲染标题 # text = Header(text)() # 渲染标题
text = Style(text)() # 渲染字体样式 text = Style(text)() # 渲染字体样式
text = Link(text)() # 渲染特殊功能 # text = Link(text)() # 渲染特殊功能
text = Cite(text)() # 渲染引用 # text = Cite(text)() # 渲染引用
text = Basic(text)() # 渲染基础格式 # text = Basic(text)() # 渲染基础格式
text = escape.back(text) text = markdown.markdown(text, extensions=['markdown.extensions.extra']) # 渲染标准markdown
text = escape.back(text) # 放回被转义的字符
# text = Basic.paragraph(text) # 渲染段落 # text = Basic.paragraph(text) # 渲染段落
return text, values return text, values

View File

@ -1,5 +1,6 @@
import sys 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): class TextEditor(QMainWindow):

7
MarkDown.py Normal file
View File

@ -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)

Binary file not shown.