diff --git a/CrossDown.py b/CrossDown.py index 741c9f6..ae8e744 100644 --- a/CrossDown.py +++ b/CrossDown.py @@ -2,14 +2,15 @@ import re class Header: - @staticmethod - def header(text: str) -> str: + def __init__(self, text: str): + self.text = text + + def __call__(self, *args, **kwargs): """ 渲染标题 - :param text: 原始文本 :return: 处理后的文本 """ - h6 = re.sub(r'###### (.*?)\n', r'
{line}
' for line in original_string.splitlines()]) - return re.sub(r'(<.+?>.*?<.+?>)
\n', r'\1\n', text) + def __init__(self, text: str): + self.text: str = text + + def paragraph(self): + """ + 为普通的行套上段落标签 + """ + self.text = re.sub(r'(<.+?>.*?<.+?>)
\n', + r'\1\n', # 移除已被标签包裹的行的额外的标签 + '\n'.join( + [ + f'
{line}
' for line in self.text.splitlines() if + not re.search( # 把所有非空的行都套上标签 + r'^\s*\n?$', line # 识别空行或空白行 + ) + ] + ) + ) + + def __call__(self, *args, **kwargs): + self.paragraph() + return self.text def add_indent_to_string(input_string: str, indent_spaces: int = 4): @@ -204,10 +224,10 @@ def body(text: str) -> str: :return: 输出渲染后的正文 """ text = Value(text)() # 提取变量并赋值到文本中 - text = Header.header(text) # 渲染标题 + text = Header(text)() # 渲染标题 text = Style(text)() # 渲染字体样式 text = Function(text)() # 渲染特殊功能 - text = Basic.paragraph(text) + text = Basic(text)() # 渲染基础格式 # text = Basic.paragraph(text) # 渲染段落 return text