From 0fef493d04ce617477c3837f2fc54c0975af5b45 Mon Sep 17 00:00:00 2001 From: crossdark Date: Sun, 6 Oct 2024 22:53:00 +0800 Subject: [PATCH] =?UTF-8?q?1.6.2=E9=94=9A=E7=82=B9=E5=A4=B4=E7=96=BC?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CrossDown/Core.py | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/CrossDown/Core.py b/CrossDown/Core.py index defa090..bc00314 100644 --- a/CrossDown/Core.py +++ b/CrossDown/Core.py @@ -89,13 +89,13 @@ class ID(InlineProcessor): 需要对HTML标签设置ID实现的样式 """ - def __init__(self, pattern: str, tag: str, property_: str, value: Union[str, bool, int] = None): + def __init__(self, pattern: str, tag: str, property_: str, value: Union[str, bool] = None): """ 初始化 :param pattern: 正则表达式 :param tag: html标签 :param property_: html标签属性名称 - :param value: html标签属性的值 + :param value: html标签属性的值 不设置时为第二个匹配组,设置为整数时则为指定的匹配组,设置为字符串则为原始字符串 """ super().__init__(pattern) self.tag = tag @@ -126,26 +126,6 @@ class Emoji(InlineProcessor): return emoji.emojize(match.group(0)), match.start(), match.end() -class Syllabus_(InlineProcessor): - """ - 需要对HTML标签设置ID实现的样式 - """ - - def __init__(self, pattern: str): - """ - 初始化 - :param pattern: 正则表达式 - """ - super().__init__(pattern) - - def handleMatch(self, match, match_line): - tag = xml.etree.ElementTree.Element(f'h{len(match.group(1).split("."))}') # 创建标签 - tag.text = match.group(1) + ' ' + match.group(3) # 设置标签内容 - tag.set('id', match.group(1)) # 设置标签属性 - - return tag, match.start(), match.end() - - class Syllabus(BlockProcessor): # 定义提纲的正则表达式 syllabus_re = r'(\d+(\.\d+)*)\s+(.*)' @@ -244,10 +224,13 @@ class Anchor(Extension): def extendMarkdown(self, md): md.registerExtension(self) # 注册扩展 md.inlinePatterns.register(ID( - r'\{#([^{}#]+)}', tag='span', property_='id'), 'hide', 0 + r'\{#([^{}#]+)}', tag='span', property_='id', value=1), 'hide', 0 + ) # 定义锚点 + md.inlinePatterns.register(ID( + r'\{([^{}#]+)}', tag='span', property_='id', value=1), 'hide', 0 ) # 定义锚点 def main(text: str) -> Tuple[str, Dict[str, List[str]]]: - md = Markdown(extensions=[Basic(), Box(), Anchor()] + list(Extensions.values()), safe_mode=False) + md = Markdown(extensions=[Basic(), Box()] + list(Extensions.values()), safe_mode=False) return md.convert(text), md.Meta