From 811b2f451a0ad6455311bb084455bdd43ec71628 Mon Sep 17 00:00:00 2001 From: crossdark Date: Thu, 29 Aug 2024 16:36:37 +0800 Subject: [PATCH] =?UTF-8?q?V0.2=E6=AE=B5=E8=90=BD=E8=BF=98=E6=9C=89?= =?UTF-8?q?=E7=82=B9=E5=B0=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CrossDown.py | 58 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/CrossDown.py b/CrossDown.py index a1795df..c8e14bd 100644 --- a/CrossDown.py +++ b/CrossDown.py @@ -129,21 +129,54 @@ class Value: class CodeBlock: def __init__(self, text: str): - self.text = text - self.codes = [i for i in re.findall('`([^`]*)`', self.text) if i != ''] - print(self.codes) + """ + 找出代码块并移除代码标识 + :param text: 输入的文本 + """ + self.codes = [i for i in re.findall(r'`([^`]*)`', text) if i != ''] # 找出代码快 + self.text = re.sub(r'``', '', text) # 移除代码标识` def __call__(self, *args, **kwargs): - return re.sub(r'`[^`]*`', '', self.text) + """ + 临时移除代码块 + :param args: + :param kwargs: + :return: 不含代码的文本 + """ + for index, item in enumerate(self.codes): # 替换代码块为-@@-(ID)-@@- + self.text = re.sub(fr'`{re.escape(item)}`', f'-@@-{index}-@@-', self.text) # 同时转译特殊字符 + return self.text - def restore(self): - pass + def rendering(self): + """ + 渲染代码 + :return: + """ + for index, code in enumerate(self.codes): + if re.search(r'\n', code): # 是多行代码 + head = re.findall(r'(.*?)\n', code)[0] + if head == 'python': + pass + elif head == 'shell': + pass + else: # 是单行代码 + self.codes[index] = f'\({code}\)' + + def restore(self, new_text: str): + """ + 将渲染好的代码重新放回处理好的正文 + :param new_text: 处理好的正文 + :return: 加上代码的文章 + """ + for index, item in enumerate(self.codes): + new_text = re.sub(fr'-@@-{index}-@@-', f'{item}', new_text, flags=re.DOTALL) + return new_text class Basic: @staticmethod def paragraph(text: str): - return re.sub(r'(.*?)\n', r'

\1

\n', text) + return re.sub(r'(<.+?>.*?<.+?>)\n', r'

\1

\n', text) def add_indent_to_string(input_string: str, indent_spaces: int = 4): @@ -173,6 +206,7 @@ def body(text: str) -> str: text = Header.header(text) # 渲染标题 text = Style(text)() # 渲染字体样式 text = Function(text)() # 渲染特殊功能 + text = Basic.paragraph(text) # text = Basic.paragraph(text) # 渲染段落 return text @@ -180,8 +214,11 @@ def body(text: str) -> str: def main(origen: str): # 预处理 - text = CodeBlock(origen)() - return body(text) # 处理正文 + code_block = CodeBlock(origen) # 获取代码内容 + text = code_block() # 暂时移除代码 + code_block.rendering() # 渲染代码 + # 处理正文 + return code_block.restore(body(text)) # 放回代码 # @@ -195,6 +232,9 @@ if __name__ == '__main__': UTF-8编码示例 +