From 241aedbdee4e586f1543c8d3eb5ac1ace4c92939 Mon Sep 17 00:00:00 2001 From: crossdark Date: Thu, 3 Oct 2024 22:08:59 +0800 Subject: [PATCH] =?UTF-8?q?1.3=E5=8F=98=E9=87=8F=E5=A5=BD=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CrossDown/Core.py | 25 ++++++++----------------- README.html | 2 +- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/CrossDown/Core.py b/CrossDown/Core.py index 47b151f..704b29b 100644 --- a/CrossDown/Core.py +++ b/CrossDown/Core.py @@ -120,23 +120,14 @@ class Value(Preprocessor): for key, value in [match.groups()] } # 识别变量定义 anchors = re.findall(r'\{#([^{}#]+)}', '\n'.join(lines)) # 识别锚点定义 - for index, line in enumerate(lines): - if any(value in line for value in values): # 匹配到了变量 - for key, value in values.items(): - if '=' in line: # 是变量定义 - lines.remove(line) # 删除 - else: # 应该被赋值 - lines[index] = re.sub('{' + key + '}', value, line) # 对变量赋值 - if any(anchor in line for anchor in anchors): # 匹配到了锚点 - if re.search('\{#', line): # 是锚点定义 - lines[index] = re.sub(r'\{#(.+)}', r'', line) # 定义锚点 - else: # 是页内链接 - lines[index] = re.sub(r'\{#' - + (lambda x: x if x in anchors else '\0') # 识别ID是否为锚点 - ((lambda x: x[0] if x is not None else '\0') # 排除匹配不成功的情况 - (re.search(r'\{#(.+)}', line))) + '}', # 匹配链接id - r'\1', line) # 添加页内链接 - return lines + text = '\n'.join(lines) # 先合并成一行 + for item in anchors: + text = re.sub(r'\{#(' + item + ')}', r'', text) # 添加锚点 + text = re.sub(r'\{' + item + '}', fr'{item}', text) # 添加页内链接 + for k, v in values.items(): + text = re.sub(r'\{' + k + '} ?= ?(.+?)(?=\n|$)', '', text) # 移除变量的定义 + text = re.sub(r'\{' + k + '}', fr'{v}', text) # 给变量赋值 + return text.split('\n') # 再分割为列表 class Tag(Treeprocessor): diff --git a/README.html b/README.html index c2cb4ae..f8d84c7 100644 --- a/README.html +++ b/README.html @@ -150,7 +150,7 @@

2 变量

2.1 定义

2.2 赋值

-

{变量名} {锚点名}

+

锚点名

提纲的编号已经自动配置为了锚点,可直接使用{2}

2.3 添加锚点