diff --git a/CrossDown/Core.py b/CrossDown/Core.py index cf04512..d27ca74 100644 --- a/CrossDown/Core.py +++ b/CrossDown/Core.py @@ -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): diff --git a/README.html b/README.html index d3cb82c..7052124 100644 --- a/README.html +++ b/README.html @@ -151,9 +151,8 @@

链接文本

2 变量

2.1 定义

-

{变量名} = 值

2.2 赋值

-

{变量名} {锚点名}

+

值 {锚点名}

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

2.3 添加锚点

{#锚点名}