forked from CrossDark/CrossDown
1.7 latex好了
This commit is contained in:
parent
dfbf9371a5
commit
b22a4f319e
@ -193,6 +193,19 @@ class LinkLine(InlineProcessor):
|
|||||||
return tag, match.start(), match.end()
|
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):
|
class Basic(Extension):
|
||||||
"""
|
"""
|
||||||
渲染基本样式
|
渲染基本样式
|
||||||
@ -239,12 +252,18 @@ class Box(Extension):
|
|||||||
|
|
||||||
|
|
||||||
class Anchor(Extension):
|
class Anchor(Extension):
|
||||||
def extendMarkdown(self, md):
|
def extendMarkdown(self, md: Markdown):
|
||||||
md.registerExtension(self) # 注册扩展
|
md.registerExtension(self) # 注册扩展
|
||||||
md.inlinePatterns.register(_Anchor(r'\{#([^{}#]+)}'), 'anchor', 0) # 定义锚点
|
md.inlinePatterns.register(_Anchor(r'\{#([^{}#]+)}'), 'anchor', 0) # 定义锚点
|
||||||
md.inlinePatterns.register(LinkLine(r'\{([^{}#]+)}'), 'line_link', 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]]]:
|
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
|
return md.convert(text), md.Meta
|
||||||
|
@ -172,8 +172,8 @@
|
|||||||
<h1 id="4">4 代码块</h1>
|
<h1 id="4">4 代码块</h1>
|
||||||
<h2 id="4.1">4.1 <code>单行</code></h2>
|
<h2 id="4.1">4.1 <code>单行</code></h2>
|
||||||
<h3 id="4.1.1">4.1.1 LaTex</h3>
|
<h3 id="4.1.1">4.1.1 LaTex</h3>
|
||||||
<p><code>$CO_2$</code></p>
|
<p>这是\(CO_2\)二氧化碳</p>
|
||||||
<p><code>$H_2O$</code></p>
|
<p>这是\(H_2O\)水</p>
|
||||||
<h3 id="4.1.2">4.1.2 函数</h3>
|
<h3 id="4.1.2">4.1.2 函数</h3>
|
||||||
<p><code>¥y=x*2+1¥</code> // 不定义范围</p>
|
<p><code>¥y=x*2+1¥</code> // 不定义范围</p>
|
||||||
<p><code>¥y=x**2¥€-50,50€</code> // 定义了x范围</p>
|
<p><code>¥y=x**2¥€-50,50€</code> // 定义了x范围</p>
|
||||||
|
Loading…
Reference in New Issue
Block a user