1.3变量好了

This commit is contained in:
跨越晨昏 2024-10-03 22:08:59 +08:00
parent aa42d52b9c
commit 241aedbdee
2 changed files with 9 additions and 18 deletions

View File

@ -120,23 +120,14 @@ class Value(Preprocessor):
for key, value in [match.groups()] for key, value in [match.groups()]
} # 识别变量定义 } # 识别变量定义
anchors = re.findall(r'\{#([^{}#]+)}', '\n'.join(lines)) # 识别锚点定义 anchors = re.findall(r'\{#([^{}#]+)}', '\n'.join(lines)) # 识别锚点定义
for index, line in enumerate(lines): text = '\n'.join(lines) # 先合并成一行
if any(value in line for value in values): # 匹配到了变量 for item in anchors:
for key, value in values.items(): text = re.sub(r'\{#(' + item + ')}', r'<span id="\1"></span>', text) # 添加锚点
if '=' in line: # 是变量定义 text = re.sub(r'\{' + item + '}', fr'<a href="#{item}">{item}</a>', text) # 添加页内链接
lines.remove(line) # 删除 for k, v in values.items():
else: # 应该被赋值 text = re.sub(r'\{' + k + '} ?= ?(.+?)(?=\n|$)', '', text) # 移除变量的定义
lines[index] = re.sub('{' + key + '}', value, line) # 对变量赋值 text = re.sub(r'\{' + k + '}', fr'{v}', text) # 给变量赋值
if any(anchor in line for anchor in anchors): # 匹配到了锚点 return text.split('\n') # 再分割为列表
if re.search('\{#', line): # 是锚点定义
lines[index] = re.sub(r'\{#(.+)}', r'<span id="\1"></span>', 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'<a href="#\1">\1</a>', line) # 添加页内链接
return lines
class Tag(Treeprocessor): class Tag(Treeprocessor):

View File

@ -150,7 +150,7 @@
<h1 id="2">2 变量</h1> <h1 id="2">2 变量</h1>
<h2 id="2.1">2.1 定义</h2> <h2 id="2.1">2.1 定义</h2>
<h2 id="2.2">2.2 赋值</h2> <h2 id="2.2">2.2 赋值</h2>
<p>{变量名} {锚点名}</p> <p><a href="#锚点名">锚点名</a></p>
<p>提纲的编号已经自动配置为了锚点,可直接使用{2}</p> <p>提纲的编号已经自动配置为了锚点,可直接使用{2}</p>
<h2 id="2.3">2.3 添加锚点</h2> <h2 id="2.3">2.3 添加锚点</h2>
<p><span id="锚点名"></span></p> <p><span id="锚点名"></span></p>