1.7 latex好了

This commit is contained in:
跨越晨昏 2024-10-09 22:25:56 +08:00
parent dfbf9371a5
commit b22a4f319e
3 changed files with 25 additions and 6 deletions

View File

@ -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

View File

@ -172,8 +172,8 @@
<h1 id="4">4 代码块</h1>
<h2 id="4.1">4.1 <code>单行</code></h2>
<h3 id="4.1.1">4.1.1 LaTex</h3>
<p><code>$CO_2$</code></p>
<p><code>$H_2O$</code></p>
<p>这是\(CO_2\)二氧化碳</p>
<p>这是\(H_2O\)水</p>
<h3 id="4.1.2">4.1.2 函数</h3>
<p><code>¥y=x*2+1¥</code> // 不定义范围</p>
<p><code>¥y=x**2¥€-50,50€</code> // 定义了x范围</p>

View File

@ -94,9 +94,9 @@ ___
4.1.1 LaTex
`$CO_2$`
这是`$CO_2$`二氧化碳
`$H_2O$`
这是`$H_2O$`水
4.1.2 函数