1.0.5变量搞定

This commit is contained in:
跨越晨昏 2024-10-02 20:19:55 +08:00
parent 2c796e7146
commit 7c9d21b324
2 changed files with 17 additions and 9 deletions

View File

@ -110,13 +110,22 @@ class Syllabus(Preprocessor):
class Value(Preprocessor):
def run(self, lines: List[str]) -> List[str]:
values = { # 从text中提取所有变量并转换成字典
key: value for key, value in [
(lambda match: match.groups() if match is not None else ('\1', '\2')) # 未定义变量的行统一返回('', '')
(re.match(r'\{([^{}#]+)} ?= ?(.+?)(?=\n|$)', line)) for line in lines
]
}
return [re.sub(line, values[line], line) if any(value in line for value in values) in values.keys() else line for line in lines] # TODO
values = {
key: value.strip() # 去除值两侧的空白字符
for line in lines
for match in [re.match(r'\{([^{}#]+)} ?= ?(.+?)(?=\n|$)', line)]
if match
for key, value in [match.groups()]
} # 识别变量定义
print(values)
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) # 对变量赋值
return lines
class Header(Treeprocessor):

View File

@ -151,9 +151,8 @@
<p><a href="https://crossdark.com">链接文本</a></p>
<h1 id="2">2 变量</h1>
<h2 id="2.1">2.1 定义</h2>
<p>{变量名} = 值</p>
<h2 id="2.2">2.2 赋值</h2>
<p>{变量名} {锚点名}</p>
<p> {锚点名}</p>
<p>提纲的编号已经自动配置为了锚点,可直接使用{2}</p>
<h2 id="2.3">2.3 添加锚点</h2>
<p>{#锚点名}</p>