From d255f8e2aa03e504f524668fadead12953a3e130 Mon Sep 17 00:00:00 2001 From: crossdark Date: Wed, 2 Oct 2024 21:04:14 +0800 Subject: [PATCH] =?UTF-8?q?1.1.0=E9=94=9A=E7=82=B9=E6=9C=89=E7=82=B9?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CrossDown/Core.py | 50 +++++++++++++++++++++++++++++++++-------------- README.html | 9 ++++++--- README.md | 6 +++++- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/CrossDown/Core.py b/CrossDown/Core.py index d27ca74..5abbb29 100644 --- a/CrossDown/Core.py +++ b/CrossDown/Core.py @@ -26,9 +26,9 @@ Extensions = { "WikiLinks": "markdown.extensions.wikilinks", } - try: # 检测当前平台是否支持扩展语法 import Extra + Extensions += Extra.EXTRA except ModuleNotFoundError: EXTRA_ABLE = False @@ -43,7 +43,7 @@ class Style(Preprocessor): def underline(text): """ ~下划线~ - :return: + :return: text """ return re.sub(r'~([^~\n]+)~', r'\1', text) @@ -51,7 +51,7 @@ class Style(Preprocessor): def strikethrough(text): """ ~~删除线~~ - :return: + :return: text """ return re.sub(r'~~([^~\n]+)~~', r'\1', text) @@ -59,7 +59,7 @@ class Style(Preprocessor): def highlight(text): """ ==高亮== - :return: + :return: text """ return re.sub(r'==([^=\n]+)==', r'\1', text) @@ -67,7 +67,7 @@ class Style(Preprocessor): def up(text): """ [在文本的正上方添加一行小文本]^(主要用于标拼音) - :return: + :return: text """ return re.sub(r'\[(.*?)]\^\((.*?)\)', r'\1\2', text) @@ -75,7 +75,7 @@ class Style(Preprocessor): def hide(text): """ [在指定的文本里面隐藏一段文本]-(只有鼠标放在上面才会显示隐藏文本) - :return: + :return: text """ return re.sub(r'\[(.*?)]-\((.*?)\)', r'\1', text) @@ -99,11 +99,11 @@ class Syllabus(Preprocessor): fr'{"#" * len(match.groups()[0].split("."))} \1', origen) if match is not None else origen) # 对于不是提纲的行,直接返回原始字符 ((lambda x: re.match(r'^([\d.]+) ', x) # 判断是否是提纲 - if not any((x.startswith('.'), # 以.开头 - re.search('\. ', x) is not None, # 存在.+空格 - re.search('\.{2,}', x), # 存在连续的. - )) - else None)(line), line) # 排除.在提纲号开头或结尾的情况 + if not any((x.startswith('.'), # 以.开头 + re.search('\. ', x) is not None, # 存在.+空格 + re.search('\.{2,}', x), # 存在连续的. + )) + else None)(line), line) # 排除.在提纲号开头或结尾的情况 for line in lines # 分割并遍历文本的每一行 ] @@ -117,7 +117,8 @@ class Value(Preprocessor): if match for key, value in [match.groups()] } # 识别变量定义 - print(values) + anchors = re.findall(r'\{#([^{}#]+)}', '\n'.join(lines)) # 识别锚点定义 + print(anchors) for index, line in enumerate(lines): if any(value in line for value in values): # 匹配到了变量 for key, value in values.items(): @@ -125,10 +126,15 @@ class Value(Preprocessor): lines.remove(line) # 删除 else: # 应该被赋值 lines[index] = re.sub('{' + key + '}', value, line) # 对变量赋值 + elif any(anchor in line for anchor in anchors): # 匹配到了锚点 + if re.search('\{#', line): # 是锚点定义 + lines[index] = re.sub(r'\{#(.+)}', r'', line) # 定义锚点 + else: # 是页内链接 + pass return lines -class Header(Treeprocessor): +class Tag(Treeprocessor): def run(self, root): """ 通过修改AST来给标题添加锚点 @@ -136,9 +142,15 @@ class Header(Treeprocessor): for header in root.iter(): if header.tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'): # 查找标题 header.set('id', header.text.split(' ')[0]) # 给标题添加锚点 + if header.text is not None: # 不是空行 + pass class Basic(Extension): + """ + 基本扩展 + """ + def extendMarkdown(self, md): md.registerExtension(self) # 注册扩展 md.preprocessors.register(Style(md), 'style', 0) @@ -146,15 +158,23 @@ class Basic(Extension): class More(Extension): + """ + 高级扩展 + """ + def extendMarkdown(self, md): md.preprocessors.register(Value(md), 'values', 0) class Decorate(Extension): + """ + 修饰扩展,最后处理 + """ + def extendMarkdown(self, md): - md.treeprocessors.register(Header(md), 'header', 0) + md.treeprocessors.register(Tag(md), 'header', 0) def main(text: str) -> Tuple[str, Dict[str, List[str]]]: - md = Markdown(extensions=[Basic(), More()] + list(Extensions.values()) + [Decorate()]) + md = Markdown(extensions=[Basic(), More()] + list(Extensions.values()) + [Decorate()], safe_mode=False) return md.convert(text), md.Meta diff --git a/README.html b/README.html index 7052124..443941f 100644 --- a/README.html +++ b/README.html @@ -77,6 +77,7 @@
  • 3.1 单行
  • 3.2 多行