From ff42b7683c62532a92486073fb551f48fcf367ce Mon Sep 17 00:00:00 2001 From: crossdark Date: Sun, 13 Oct 2024 22:13:10 +0800 Subject: [PATCH] =?UTF-8?q?1.8.0=20=E5=BC=BA=E8=B0=83=E5=9D=97=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8F=98=E9=87=8F=E8=B5=8B=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CrossDown/Core.py | 23 ++++++++++++++++++----- CrossDown/__init__.py | 15 ++++++++++++--- README.html | 20 +++++++++++++------- README.md | 4 ++++ 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/CrossDown/Core.py b/CrossDown/Core.py index e78d45a..2d9dc3e 100644 --- a/CrossDown/Core.py +++ b/CrossDown/Core.py @@ -12,7 +12,7 @@ import xml import emoji Extensions = { - "Extra": extra.ExtraExtension(), # 基本扩展 + "Extra": extra.ExtraExtension(fenced_code={'lang_prefix': ''}), # 基本扩展 "Admonition": admonition.AdmonitionExtension(), "Meta-Data": meta.MetaExtension(), "Sane Lists": sane_lists.SaneListExtension(), @@ -20,6 +20,7 @@ Extensions = { "WikiLinks": wikilinks.WikiLinkExtension(), } + try: # 检测当前平台是否支持扩展语法 from .Extra import * EXTRA_ABLE = True @@ -183,6 +184,10 @@ class LinkLine(InlineProcessor): class CodeLine(Treeprocessor): + def __init__(self, variable: Dict): + super().__init__() + self.variable = variable + def run(self, root): for elem in root.iter('p'): # 在所有段落中查找单行代码 if elem.findall('code'): # 找到单行代码 @@ -212,7 +217,11 @@ class CodeLine(Treeprocessor): elif re.match(r'\{[^$]*}', code.text): # 是强调 code.tag = 'span' code.set('class', 'block') - code.text = code.text[1:-1] + key = code.text[1:-1] # 去掉两边的{} + if key in self.variable: + code.text = self.variable[key] + else: + code.text = key class CodeBlock(Treeprocessor): @@ -291,12 +300,16 @@ class Anchor(Extension): class Code(Extension): + def __init__(self, variable: Dict): + super().__init__() + self.variable = variable + def extendMarkdown(self, md: Markdown) -> None: md.registerExtension(self) # 注册扩展 - md.treeprocessors.register(CodeLine(), 'code_line', 0) # 渲染单行代码块 - md.treeprocessors.register(CodeBlock(), 'code_block', 1) # 渲染多行代码块 + md.treeprocessors.register(CodeLine(variable=self.variable), 'code_line', 0) # 渲染单行代码块 + # md.treeprocessors.register(CodeBlock(), 'code_block', 1) # 渲染多行代码块 def main(text: str) -> Tuple[str, Dict[str, List[str]]]: - md = Markdown(extensions=[Basic(), Box(), Anchor()] + list(Extensions.values()) + [Code()]) + md = Markdown(extensions=[Basic(), Box(), Anchor()] + list(Extensions.values()) + [Code({'a': 'b', '强调变量': '强调值'})]) return md.convert(text), md.Meta diff --git a/CrossDown/__init__.py b/CrossDown/__init__.py index 6e2ee5a..4af49aa 100644 --- a/CrossDown/__init__.py +++ b/CrossDown/__init__.py @@ -20,7 +20,18 @@ HEAD = ( '', '', '', + + # mermaid '', + '', + + # Highlight.js + '', + '', + '', + '