3 使用pymdownx
This commit is contained in:
parent
dc70e8c996
commit
2017c80724
@ -1,6 +1,5 @@
|
|||||||
from re import Match
|
|
||||||
|
|
||||||
from markdown.extensions import Extension, extra, admonition, meta, sane_lists, toc, wikilinks, codehilite, legacy_attrs
|
from markdown.extensions import Extension, extra, admonition, meta, sane_lists, toc, wikilinks, codehilite, legacy_attrs
|
||||||
|
from pymdownx.arithmatex import ArithmatexExtension
|
||||||
|
|
||||||
from pygments.formatters import HtmlFormatter
|
from pygments.formatters import HtmlFormatter
|
||||||
|
|
||||||
@ -48,14 +47,18 @@ class HighlightHtmlFormatter(HtmlFormatter):
|
|||||||
|
|
||||||
|
|
||||||
Extensions = {
|
Extensions = {
|
||||||
|
# 自带
|
||||||
'基本扩展': extra.ExtraExtension(fenced_code={'lang_prefix': ''}),
|
'基本扩展': extra.ExtraExtension(fenced_code={'lang_prefix': ''}),
|
||||||
'警告扩展': admonition.AdmonitionExtension(),
|
'警告扩展': admonition.AdmonitionExtension(),
|
||||||
'元数据': meta.MetaExtension(),
|
'元数据': meta.MetaExtension(),
|
||||||
'能列表': sane_lists.SaneListExtension(),
|
'能列表': sane_lists.SaneListExtension(),
|
||||||
'目录': toc.TocExtension(),
|
'目录': toc.TocExtension(),
|
||||||
'内部链接': wikilinks.WikiLinkExtension(),
|
'内部链接': wikilinks.WikiLinkExtension(),
|
||||||
'代码高亮': codehilite.CodeHiliteExtension(guess_lang=False, pygments_formatter=HighlightHtmlFormatter),
|
# '代码高亮': codehilite.CodeHiliteExtension(guess_lang=False, pygments_formatter=HighlightHtmlFormatter),
|
||||||
'属性设置': legacy_attrs.LegacyAttrExtension()
|
'属性设置': legacy_attrs.LegacyAttrExtension(),
|
||||||
|
|
||||||
|
# pymdownx
|
||||||
|
'超级数学': ArithmatexExtension(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -68,7 +71,6 @@ class PreProcess(Preprocessor):
|
|||||||
def run(self, lines: List[str]) -> List[str]:
|
def run(self, lines: List[str]) -> List[str]:
|
||||||
new_lines = []
|
new_lines = []
|
||||||
for line in lines: # 逐行遍历
|
for line in lines: # 逐行遍历
|
||||||
print(line)
|
|
||||||
for value in re.findall(r'\{\[(.+?)]}', line): # 找到变量
|
for value in re.findall(r'\{\[(.+?)]}', line): # 找到变量
|
||||||
if value in self.variable: # 变量已定义
|
if value in self.variable: # 变量已定义
|
||||||
line = re.sub(fr'\{{\[{value}]}}', self.variable[value], line) # 替换变量为值
|
line = re.sub(fr'\{{\[{value}]}}', self.variable[value], line) # 替换变量为值
|
||||||
@ -351,6 +353,15 @@ class CodeLine(Treeprocessor):
|
|||||||
code.text = key
|
code.text = key
|
||||||
|
|
||||||
|
|
||||||
|
class CodeBlock(Treeprocessor):
|
||||||
|
"""
|
||||||
|
渲染单行代码
|
||||||
|
"""
|
||||||
|
def run(self, root: xml.etree.ElementTree.Element):
|
||||||
|
for code in root:
|
||||||
|
print(code.text)
|
||||||
|
|
||||||
|
|
||||||
class Pre(Extension):
|
class Pre(Extension):
|
||||||
"""预处理"""
|
"""预处理"""
|
||||||
def __init__(self, variable: Variable):
|
def __init__(self, variable: Variable):
|
||||||
@ -465,6 +476,7 @@ class Code(Extension):
|
|||||||
"""
|
"""
|
||||||
md.registerExtension(self) # 注册扩展
|
md.registerExtension(self) # 注册扩展
|
||||||
md.treeprocessors.register(CodeLine(variable=self.variable), 'code_line', 0) # 渲染单行代码块
|
md.treeprocessors.register(CodeLine(variable=self.variable), 'code_line', 0) # 渲染单行代码块
|
||||||
|
md.treeprocessors.register(CodeBlock(), 'code_block', 1000) # 渲染多行代码块
|
||||||
|
|
||||||
|
|
||||||
def main(text: str, variable: Variable = None) -> Tuple[str, Variable]:
|
def main(text: str, variable: Variable = None) -> Tuple[str, Variable]:
|
||||||
@ -476,5 +488,5 @@ def main(text: str, variable: Variable = None) -> Tuple[str, Variable]:
|
|||||||
"""
|
"""
|
||||||
if variable is None:
|
if variable is None:
|
||||||
variable = {}
|
variable = {}
|
||||||
md = Markdown(extensions=[Pre(variable=variable), Basic(), Box(), Anchor()] + list(Extensions.values()) + [Code(variable=variable)])
|
md = Markdown(extensions=[Pre(variable=variable), Basic(), Anchor()] + list(Extensions.values()) + [Code(variable=variable)])
|
||||||
return md.convert(text), md.Meta
|
return md.convert(text), md.Meta
|
||||||
|
37
README.html
37
README.html
@ -76,6 +76,7 @@
|
|||||||
<li><a href="#4.2.4">4.2.4 shell</a></li>
|
<li><a href="#4.2.4">4.2.4 shell</a></li>
|
||||||
<li><a href="#4.2.5">4.2.5 latex</a></li>
|
<li><a href="#4.2.5">4.2.5 latex</a></li>
|
||||||
<li><a href="#4.2.6">4.2.6 HTML</a></li>
|
<li><a href="#4.2.6">4.2.6 HTML</a></li>
|
||||||
|
<li><a href="#4.2.7">4.2.7 未知语言</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
@ -123,7 +124,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<h1 id="crossdown">CrossDown</h1>
|
<h1 id="crossdown">CrossDown</h1>
|
||||||
<p>自制的markdown,添加了一些自定义的语法
|
<p>自制的markdown,添加了一些自定义的语法
|
||||||
效果请见<a href="https://github.com/CrossDark/CrossDown/blob/main/README.html">README.html</a></p>
|
效果请见<README.html></p>
|
||||||
<h1 id="1">1 基本语法</h1>
|
<h1 id="1">1 基本语法</h1>
|
||||||
<h2 id="1.1">1.1 标题</h2>
|
<h2 id="1.1">1.1 标题</h2>
|
||||||
<h1 id="_1">一级标题</h1>
|
<h1 id="_1">一级标题</h1>
|
||||||
@ -212,36 +213,42 @@
|
|||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
|
|
||||||
<h3 id="4.2.5">4.2.5 latex</h3>
|
<h3 id="4.2.5">4.2.5 latex</h3>
|
||||||
<div class="codehilite"><pre><span></span><code class="latex"><span class="k">\documentclass</span><span class="nb">{</span>article<span class="nb">}</span>
|
<div class="codehilite"><pre><span></span><code class="rd">\documentclass{article}
|
||||||
<span class="k">\title</span><span class="nb">{</span>我的第一个LaTeX文档<span class="nb">}</span>
|
\title{我的第一个LaTeX文档}
|
||||||
<span class="k">\author</span><span class="nb">{</span>作者姓名<span class="nb">}</span>
|
\author{作者姓名}
|
||||||
<span class="k">\date</span><span class="nb">{</span><span class="k">\today</span><span class="nb">}</span>
|
\date{\today}
|
||||||
|
|
||||||
<span class="k">\begin</span><span class="nb">{</span>document<span class="nb">}</span>
|
\begin{document}
|
||||||
|
|
||||||
<span class="k">\maketitle</span>
|
\maketitle
|
||||||
|
|
||||||
这是我的第一个LaTeX文档。它包含了一个标题、作者信息、日期和一个简单的正文部分。
|
这是我的第一个LaTeX文档。它包含了一个标题、作者信息、日期和一个简单的正文部分。
|
||||||
|
|
||||||
<span class="k">\section</span><span class="nb">{</span>引言<span class="nb">}</span>
|
\section{引言}
|
||||||
|
|
||||||
这是引言部分,可以简要介绍文档的主题和目的。
|
这是引言部分,可以简要介绍文档的主题和目的。
|
||||||
|
|
||||||
<span class="k">\section</span><span class="nb">{</span>正文<span class="nb">}</span>
|
\section{正文}
|
||||||
|
|
||||||
这是正文部分,可以详细阐述你的观点或研究内容。你可以根据需要添加更多的章节和子章节。
|
这是正文部分,可以详细阐述你的观点或研究内容。你可以根据需要添加更多的章节和子章节。
|
||||||
|
|
||||||
<span class="k">\subsection</span><span class="nb">{</span>子章节示例<span class="nb">}</span>
|
\subsection{子章节示例}
|
||||||
|
|
||||||
这是一个子章节的示例,用于展示如何在LaTeX文档中创建层次结构。
|
这是一个子章节的示例,用于展示如何在LaTeX文档中创建层次结构。
|
||||||
|
|
||||||
<span class="k">\end</span><span class="nb">{</span>document<span class="nb">}</span>
|
\end{document}
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
|
|
||||||
<h3 id="4.2.6">4.2.6 HTML</h3>
|
<h3 id="4.2.6">4.2.6 HTML</h3>
|
||||||
<div class="codehilite"><pre><span></span><code class="html">*斜体***粗体**~下划线~~~删除线~~==高亮==
|
<div class="codehilite"><pre><span></span><code class="html">*斜体***粗体**~下划线~~~删除线~~==高亮==
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
|
|
||||||
|
<h3 id="4.2.7">4.2.7 未知语言</h3>
|
||||||
|
<div class="codehilite"><pre><span></span><code class="text">#! usr/bin/python3.11
|
||||||
|
def main():
|
||||||
|
print('CrossDown')
|
||||||
|
</code></pre></div>
|
||||||
|
|
||||||
<h1 id="5">5 转义</h1>
|
<h1 id="5">5 转义</h1>
|
||||||
<p>\ </p>
|
<p>\ </p>
|
||||||
<p>\a </p>
|
<p>\a </p>
|
||||||
@ -382,10 +389,10 @@
|
|||||||
<p>否则所有定义将被集中在文章末尾</p>
|
<p>否则所有定义将被集中在文章末尾</p>
|
||||||
<h1 id="14">14 外框</h1>
|
<h1 id="14">14 外框</h1>
|
||||||
<h2 id="14.1">14.1 警告</h2>
|
<h2 id="14.1">14.1 警告</h2>
|
||||||
<p>这是一个<div style="display: inline-block; border: 1px solid red;">警告</div>……</p>
|
<p>这是一个!!!警告!!!……</p>
|
||||||
<div style="display: inline-block; border: 1px solid red;">
|
<p>!!!
|
||||||
<p>这是一条警告</p>
|
这是一条警告
|
||||||
</div>
|
!!!</p>
|
||||||
<h2 id="14.2">14.2 提醒</h2>
|
<h2 id="14.2">14.2 提醒</h2>
|
||||||
<p>这是一个!!提醒!!……</p>
|
<p>这是一个!!提醒!!……</p>
|
||||||
<p>!!
|
<p>!!
|
||||||
|
10
README.md
10
README.md
@ -158,7 +158,7 @@ ls
|
|||||||
|
|
||||||
4.2.5 latex
|
4.2.5 latex
|
||||||
|
|
||||||
```latex
|
```latex-rd
|
||||||
\documentclass{article}
|
\documentclass{article}
|
||||||
\title{我的第一个LaTeX文档}
|
\title{我的第一个LaTeX文档}
|
||||||
\author{作者姓名}
|
\author{作者姓名}
|
||||||
@ -191,6 +191,14 @@ ls
|
|||||||
*斜体***粗体**~下划线~~~删除线~~==高亮==
|
*斜体***粗体**~下划线~~~删除线~~==高亮==
|
||||||
```
|
```
|
||||||
|
|
||||||
|
4.2.7 未知语言
|
||||||
|
|
||||||
|
```
|
||||||
|
#! usr/bin/python3.11
|
||||||
|
def main():
|
||||||
|
print('CrossDown')
|
||||||
|
```
|
||||||
|
|
||||||
5 转义
|
5 转义
|
||||||
|
|
||||||
\\
|
\\
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
Markdown>=3.7
|
Markdown>=3.7
|
||||||
matplotlib>=3.9.2
|
matplotlib~=3.8.2
|
||||||
numpy>=2.1.1
|
numpy~=1.26.2
|
||||||
pygments>=2.18.0
|
pygments>=2.18.0
|
||||||
|
CrossDown~=2.2.0
|
||||||
|
setuptools~=70.1.1
|
||||||
|
emoji~=2.13.2
|
Loading…
Reference in New Issue
Block a user