From b22a4f319ed1f3b0568d10b7c80abde9500ea56e Mon Sep 17 00:00:00 2001 From: crossdark Date: Wed, 9 Oct 2024 22:25:56 +0800 Subject: [PATCH] =?UTF-8?q?1.7=20latex=E5=A5=BD=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CrossDown/Core.py | 23 +++++++++++++++++++++-- README.html | 4 ++-- README.md | 4 ++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CrossDown/Core.py b/CrossDown/Core.py index 58b6fb4..2eabd9d 100644 --- a/CrossDown/Core.py +++ b/CrossDown/Core.py @@ -193,6 +193,19 @@ class LinkLine(InlineProcessor): return tag, match.start(), match.end() +class CodeLine(Treeprocessor): + def run(self, root): + for elem in root.iter('p'): # 在所有段落中查找单行代码 + if elem.findall('code'): # 找到单行代码 + for code in elem: + if re.match(r'\$[^$]*\$', code.text): # 渲染Latex + if isinstance(elem.text, str): + elem.text += fr'\({code.text[1:-1]}\){code.tail}' + else: + elem.text = fr'\({code.text}\)' + elem.remove(code) + + class Basic(Extension): """ 渲染基本样式 @@ -239,12 +252,18 @@ class Box(Extension): class Anchor(Extension): - def extendMarkdown(self, md): + def extendMarkdown(self, md: Markdown): md.registerExtension(self) # 注册扩展 md.inlinePatterns.register(_Anchor(r'\{#([^{}#]+)}'), 'anchor', 0) # 定义锚点 md.inlinePatterns.register(LinkLine(r'\{([^{}#]+)}'), 'line_link', 0) # 添加页内链接 +class Code(Extension): + def extendMarkdown(self, md: Markdown) -> None: + md.registerExtension(self) # 注册扩展 + md.treeprocessors.register(CodeLine(), 'code_block', 0) # 渲染多行代码块 + + def main(text: str) -> Tuple[str, Dict[str, List[str]]]: - md = Markdown(extensions=[Basic(), Box(), Anchor()] + list(Extensions.values())) + md = Markdown(extensions=[Basic(), Box(), Anchor()] + list(Extensions.values()) + [Code()]) return md.convert(text), md.Meta diff --git a/README.html b/README.html index 3131b4e..ea43ae0 100644 --- a/README.html +++ b/README.html @@ -172,8 +172,8 @@

4 代码块

4.1 单行

4.1.1 LaTex

-

$CO_2$

-

$H_2O$

+

这是\(CO_2\)二氧化碳

+

这是\(H_2O\)水

4.1.2 函数

¥y=x*2+1¥ // 不定义范围

¥y=x**2¥€-50,50€ // 定义了x范围

diff --git a/README.md b/README.md index 219abea..b795604 100644 --- a/README.md +++ b/README.md @@ -94,9 +94,9 @@ ___ 4.1.1 LaTex -`$CO_2$` +这是`$CO_2$`二氧化碳 -`$H_2O$` +这是`$H_2O$`水 4.1.2 函数