Compare commits
3 Commits
5ab56b2e8b
...
bbc729f1c3
Author | SHA1 | Date | |
---|---|---|---|
bbc729f1c3 | |||
b4d4fff952 | |||
dd01c2b57e |
@ -2,7 +2,6 @@
|
|||||||
核心代码
|
核心代码
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import xml
|
import xml
|
||||||
from typing import *
|
from typing import *
|
||||||
@ -12,6 +11,7 @@ from markdown.blockprocessors import BlockProcessor
|
|||||||
from markdown.extensions import Extension, meta, toc, wikilinks, legacy_attrs
|
from markdown.extensions import Extension, meta, toc, wikilinks, legacy_attrs
|
||||||
from markdown.inlinepatterns import InlineProcessor
|
from markdown.inlinepatterns import InlineProcessor
|
||||||
from markdown.preprocessors import Preprocessor
|
from markdown.preprocessors import Preprocessor
|
||||||
|
from markdown.treeprocessors import Treeprocessor
|
||||||
|
|
||||||
from pymdownx.arithmatex import ArithmatexExtension
|
from pymdownx.arithmatex import ArithmatexExtension
|
||||||
from pymdownx.blocks import BlocksExtension
|
from pymdownx.blocks import BlocksExtension
|
||||||
@ -25,7 +25,7 @@ from pymdownx.emoji import EmojiExtension
|
|||||||
from pymdownx.extra import ExtraExtension
|
from pymdownx.extra import ExtraExtension
|
||||||
from pymdownx.fancylists import FancyListExtension
|
from pymdownx.fancylists import FancyListExtension
|
||||||
from pymdownx.highlight import HighlightExtension
|
from pymdownx.highlight import HighlightExtension
|
||||||
from pymdownx.inlinehilite import InlineHiliteExtension
|
from pymdownx.inlinehilite import InlineHiliteExtension, InlineHilitePattern
|
||||||
from pymdownx.keys import KeysExtension
|
from pymdownx.keys import KeysExtension
|
||||||
from pymdownx.mark import MarkExtension
|
from pymdownx.mark import MarkExtension
|
||||||
from pymdownx.progressbar import ProgressBarExtension
|
from pymdownx.progressbar import ProgressBarExtension
|
||||||
@ -40,6 +40,8 @@ from pymdownx.pathconverter import PathConverterExtension
|
|||||||
import kbdextension
|
import kbdextension
|
||||||
import markdown_gfm_admonition
|
import markdown_gfm_admonition
|
||||||
|
|
||||||
|
from xml.etree.ElementTree import ElementTree, Element, fromstring
|
||||||
|
|
||||||
from .Define import Variable
|
from .Define import Variable
|
||||||
|
|
||||||
|
|
||||||
@ -108,7 +110,7 @@ class ID(InlineProcessor):
|
|||||||
需要对HTML标签设置ID实现的样式
|
需要对HTML标签设置ID实现的样式
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, pattern: str, tag: str, property_: str, value: Union[str, bool] = None):
|
def __init__(self, pattern: str, tag: str, property_: str, value: str | bool = None):
|
||||||
"""
|
"""
|
||||||
初始化
|
初始化
|
||||||
:param pattern: 正则表达式
|
:param pattern: 正则表达式
|
||||||
@ -168,8 +170,9 @@ class Anchor(InlineProcessor):
|
|||||||
"""
|
"""
|
||||||
{#定义锚点}
|
{#定义锚点}
|
||||||
"""
|
"""
|
||||||
def handleMatch(self, m: Match[str], data: str) -> (Tuple[xml.etree.ElementTree.Element, int, int] |
|
|
||||||
Tuple[None, None, None]):
|
def handleMatch(self, m: Match[str], data: str) -> (tuple[xml.etree.ElementTree.Element, int, int] |
|
||||||
|
tuple[None, None, None]):
|
||||||
"""
|
"""
|
||||||
处理匹配
|
处理匹配
|
||||||
:param m: re模块的匹配对象
|
:param m: re模块的匹配对象
|
||||||
@ -183,12 +186,35 @@ class Anchor(InlineProcessor):
|
|||||||
return tag, m.start(), m.end()
|
return tag, m.start(), m.end()
|
||||||
|
|
||||||
|
|
||||||
|
class CodeLine(Treeprocessor):
|
||||||
|
"""
|
||||||
|
渲染单行代码
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, variable: Variable):
|
||||||
|
"""
|
||||||
|
初始化
|
||||||
|
:param variable: 变量字典
|
||||||
|
"""
|
||||||
|
super().__init__()
|
||||||
|
self.variable = variable
|
||||||
|
|
||||||
|
def run(self, root: xml.etree.ElementTree.Element):
|
||||||
|
"""
|
||||||
|
渲染
|
||||||
|
:param root: Element树
|
||||||
|
"""
|
||||||
|
for code in root.findall('.//code'): # 在所有段落中查找单行代码
|
||||||
|
print(code.text)
|
||||||
|
|
||||||
|
|
||||||
class LinkLine(InlineProcessor):
|
class LinkLine(InlineProcessor):
|
||||||
"""
|
"""
|
||||||
{行内链接}
|
{行内链接}
|
||||||
"""
|
"""
|
||||||
def handleMatch(self, m: Match[str], data: str) -> (Tuple[xml.etree.ElementTree.Element, int, int] |
|
|
||||||
Tuple[None, None, None]):
|
def handleMatch(self, m: Match[str], data: str) -> (tuple[xml.etree.ElementTree.Element, int, int] |
|
||||||
|
tuple[None, None, None]):
|
||||||
"""
|
"""
|
||||||
处理匹配
|
处理匹配
|
||||||
:param m: re模块的匹配对象
|
:param m: re模块的匹配对象
|
||||||
@ -229,8 +255,48 @@ class AnchorExtension(Extension):
|
|||||||
:param md: 转换器
|
:param md: 转换器
|
||||||
"""
|
"""
|
||||||
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 CodeExtension(Extension):
|
||||||
|
def __init__(self, variable: Variable):
|
||||||
|
"""
|
||||||
|
初始化
|
||||||
|
:param variable: 变量字典
|
||||||
|
"""
|
||||||
|
super().__init__()
|
||||||
|
self.variable = variable
|
||||||
|
|
||||||
|
def extendMarkdown(self, md: Markdown):
|
||||||
|
"""
|
||||||
|
添加扩展
|
||||||
|
:param md: 转换器
|
||||||
|
"""
|
||||||
|
md.treeprocessors.register(CodeLine(variable=self.variable), 'code_line', 100)
|
||||||
|
|
||||||
|
|
||||||
|
class InlineCode:
|
||||||
|
def __init__(self, variable: Variable):
|
||||||
|
self.variable = variable
|
||||||
|
|
||||||
|
def __call__(self, source, language, css_class, md): # 自定义的单行代码格式化器
|
||||||
|
if language != '': # 调用默认格式化函数
|
||||||
|
return md.inlinePatterns['backtick'].highlight_code(src=source, language=language, classname=css_class,
|
||||||
|
md=md)
|
||||||
|
match tuple(source):
|
||||||
|
case '{', '#', *archers, '}': # 匹配到{#锚点}
|
||||||
|
archer = ''.join(archers)
|
||||||
|
return f'<span id="{archer}">{archer}</span>'
|
||||||
|
case '{', '-', *inline_links, '}': # 匹配到{-行内链接}
|
||||||
|
inline_link = ''.join(inline_links)
|
||||||
|
return f'<a href=#{inline_link}>{inline_link}</a>'
|
||||||
|
case '{', *variables, '}': # 匹配到{变量}
|
||||||
|
variable = ''.join(variables)
|
||||||
|
if variable in self.variable:
|
||||||
|
return self.variable[variable]
|
||||||
|
case _:
|
||||||
|
return f'<code>{source}</code>'
|
||||||
|
|
||||||
|
|
||||||
Extensions = {
|
Extensions = {
|
||||||
@ -247,8 +313,8 @@ Extensions = {
|
|||||||
{
|
{
|
||||||
'name': 'mermaid',
|
'name': 'mermaid',
|
||||||
'class': 'mermaid',
|
'class': 'mermaid',
|
||||||
'format': fence_div_format
|
'format': fence_div_format,
|
||||||
}
|
},
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@ -261,7 +327,6 @@ Extensions = {
|
|||||||
'标签': TabExtension(),
|
'标签': TabExtension(),
|
||||||
'批评': CriticExtension(),
|
'批评': CriticExtension(),
|
||||||
'代码高亮': HighlightExtension(),
|
'代码高亮': HighlightExtension(),
|
||||||
'行内高亮': InlineHiliteExtension(),
|
|
||||||
'按键风格': KeysExtension(),
|
'按键风格': KeysExtension(),
|
||||||
'高亮': MarkExtension(),
|
'高亮': MarkExtension(),
|
||||||
'进度条': ProgressBarExtension(),
|
'进度条': ProgressBarExtension(),
|
||||||
@ -280,11 +345,11 @@ Extensions = {
|
|||||||
|
|
||||||
# 自定义
|
# 自定义
|
||||||
'基本风格': BasicExtension(),
|
'基本风格': BasicExtension(),
|
||||||
'锚点': AnchorExtension(),
|
# '锚点': AnchorExtension(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def main(text: str, variable: Variable = None) -> Tuple[str, Variable]:
|
def main(text: str, variable: Variable = None) -> tuple[str, Variable]:
|
||||||
"""
|
"""
|
||||||
主函数
|
主函数
|
||||||
:param text: 输入文本
|
:param text: 输入文本
|
||||||
@ -293,5 +358,15 @@ def main(text: str, variable: Variable = None) -> Tuple[str, Variable]:
|
|||||||
"""
|
"""
|
||||||
if variable is None:
|
if variable is None:
|
||||||
variable = {}
|
variable = {}
|
||||||
md = Markdown(extensions=list(Extensions.values()))
|
md = Markdown(extensions=list(Extensions.values()) + [
|
||||||
|
InlineHiliteExtension(
|
||||||
|
custom_inline=[
|
||||||
|
{
|
||||||
|
'name': '*',
|
||||||
|
'class': 'block',
|
||||||
|
'format': InlineCode(variable=variable),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
),
|
||||||
|
])
|
||||||
return md.convert(text), md.Meta
|
return md.convert(text), md.Meta
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from typing import *
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
Variable = Dict[str, Union[str, Tuple[str], List[str]]] | None
|
Variable = dict[str, str | tuple[str], list[str]] | None
|
||||||
|
38
README.html
38
README.html
@ -59,7 +59,11 @@
|
|||||||
<li><a href="#2.2">2.2 赋值</a></li>
|
<li><a href="#2.2">2.2 赋值</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
<li><a href="#3">3 锚点</a></li>
|
<li><a href="#3">3 锚点</a><ul>
|
||||||
|
<li><a href="#3.1">3.1 定义</a></li>
|
||||||
|
<li><a href="#3.2">3.2 链接</a></li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
<li><a href="#4">4 代码块</a><ul>
|
<li><a href="#4">4 代码块</a><ul>
|
||||||
<li><a href="#4.1">4.1 单行</a><ul>
|
<li><a href="#4.1">4.1 单行</a><ul>
|
||||||
<li><a href="#4.1.1">4.1.1 LaTex</a></li>
|
<li><a href="#4.1.1">4.1.1 LaTex</a></li>
|
||||||
@ -182,6 +186,10 @@
|
|||||||
<h2 id="2.2">2.2 赋值</h2>
|
<h2 id="2.2">2.2 赋值</h2>
|
||||||
<p>直接在文本中使用 <abbr title="长的文本">缩写</abbr> 即可</p>
|
<p>直接在文本中使用 <abbr title="长的文本">缩写</abbr> 即可</p>
|
||||||
<h1 id="3">3 锚点</h1>
|
<h1 id="3">3 锚点</h1>
|
||||||
|
<h2 id="3.1">3.1 定义</h2>
|
||||||
|
<p><span id="锚点">锚点</span></p>
|
||||||
|
<h2 id="3.2">3.2 链接</h2>
|
||||||
|
<p><a href=#锚点>锚点</a></p>
|
||||||
<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>
|
||||||
<p>Here is some code: <code class="highlight"><span class="kn">import</span> <span class="nn">pymdownx</span><span class="p">;</span> <span class="n">pymdownx</span><span class="o">.</span><span class="n">__version__</span></code>.</p>
|
<p>Here is some code: <code class="highlight"><span class="kn">import</span> <span class="nn">pymdownx</span><span class="p">;</span> <span class="n">pymdownx</span><span class="o">.</span><span class="n">__version__</span></code>.</p>
|
||||||
@ -199,20 +207,17 @@
|
|||||||
<p><span class="keys"><kbd class="key-control">Ctrl</kbd><span>+</span><kbd class="key-alt">Alt</kbd><span>+</span><kbd class="key-delete">Del</kbd></span></p>
|
<p><span class="keys"><kbd class="key-control">Ctrl</kbd><span>+</span><kbd class="key-alt">Alt</kbd><span>+</span><kbd class="key-delete">Del</kbd></span></p>
|
||||||
<p><kbd>Enter</kbd></p>
|
<p><kbd>Enter</kbd></p>
|
||||||
<h3 id="4.1.4">4.1.4 突出</h3>
|
<h3 id="4.1.4">4.1.4 突出</h3>
|
||||||
<p><code>{突出内容}</code></p>
|
<p>`{突出内容}`</p>
|
||||||
<h2 id="4.2">4.2 多行</h2>
|
<h2 id="4.2">4.2 多行</h2>
|
||||||
<h3 id="4.2.1">4.2.1 YAML</h3>
|
<h3 id="4.2.1">4.2.1 YAML</h3>
|
||||||
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 2</span>
|
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">2</span>
|
||||||
<span class="normal"> 3</span>
|
<span class="normal">3</span>
|
||||||
<span class="normal"> 4</span>
|
<span class="normal">4</span>
|
||||||
<span class="normal"> 5</span>
|
<span class="normal">5</span>
|
||||||
<span class="normal"> 6</span>
|
<span class="normal">6</span>
|
||||||
<span class="normal"> 7</span>
|
<span class="normal">7</span>
|
||||||
<span class="normal"> 8</span>
|
<span class="normal">8</span>
|
||||||
<span class="normal"> 9</span>
|
<span class="normal">9</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="hll"><span class="nt">A</span><span class="p">:</span>
|
||||||
<span class="normal">10</span>
|
|
||||||
<span class="normal">11</span>
|
|
||||||
<span class="normal">12</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="hll"><span class="nt">A</span><span class="p">:</span>
|
|
||||||
</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">1. a</span>
|
</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">1. a</span>
|
||||||
<span class="hll"><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">2. b</span>
|
<span class="hll"><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">2. b</span>
|
||||||
</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">3. c</span>
|
</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">3. c</span>
|
||||||
@ -220,9 +225,6 @@
|
|||||||
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">a</span>
|
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">a</span>
|
||||||
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">b</span>
|
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">b</span>
|
||||||
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">c</span>
|
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">c</span>
|
||||||
<span class="p p-Indicator">{[</span><span class="nv">强调变量</span><span class="p p-Indicator">]}:</span>
|
|
||||||
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">a</span>
|
|
||||||
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">b</span>
|
|
||||||
</code></pre></div></td></tr></table></div>
|
</code></pre></div></td></tr></table></div>
|
||||||
<h3 id="4.2.2">4.2.2 Python</h3>
|
<h3 id="4.2.2">4.2.2 Python</h3>
|
||||||
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
|
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
|
||||||
@ -312,7 +314,7 @@
|
|||||||
<p>7.1.3. 错误示范</p>
|
<p>7.1.3. 错误示范</p>
|
||||||
<p>不能出现两个及以上连续的点,例如:</p>
|
<p>不能出现两个及以上连续的点,例如:</p>
|
||||||
<p>7..1...4 错误示范</p>
|
<p>7..1...4 错误示范</p>
|
||||||
<p>提纲号会被自动配置为锚点,可直接使用<a href="#7">7</a><a href="#7.1">7.1</a></p>
|
<p>提纲号会被自动配置为锚点,可直接使用{-7}{-7.1}</p>
|
||||||
<h1 id="8">8 注释</h1>
|
<h1 id="8">8 注释</h1>
|
||||||
<!-- 这是注释 -->
|
<!-- 这是注释 -->
|
||||||
|
|
||||||
@ -353,7 +355,7 @@
|
|||||||
<li>c</li>
|
<li>c</li>
|
||||||
</ul>
|
</ul>
|
||||||
</dd>
|
</dd>
|
||||||
<dt><code>{强调变量}</code></dt>
|
<dt>强调值</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<ul>
|
<ul>
|
||||||
<li>a</li>
|
<li>a</li>
|
||||||
|
13
README.md
13
README.md
@ -90,6 +90,14 @@ liuhanbo333@icloud.com
|
|||||||
|
|
||||||
3 锚点
|
3 锚点
|
||||||
|
|
||||||
|
3.1 定义
|
||||||
|
|
||||||
|
`{#锚点}`
|
||||||
|
|
||||||
|
3.2 链接
|
||||||
|
|
||||||
|
`{-锚点}`
|
||||||
|
|
||||||
4 代码块
|
4 代码块
|
||||||
|
|
||||||
4.1 `单行`
|
4.1 `单行`
|
||||||
@ -138,9 +146,6 @@ B:
|
|||||||
- a
|
- a
|
||||||
- b
|
- b
|
||||||
- c
|
- c
|
||||||
{[强调变量]}:
|
|
||||||
- a
|
|
||||||
- b
|
|
||||||
```
|
```
|
||||||
|
|
||||||
4.2.2 Python
|
4.2.2 Python
|
||||||
@ -240,7 +245,7 @@ def main():
|
|||||||
|
|
||||||
7..1...4 错误示范
|
7..1...4 错误示范
|
||||||
|
|
||||||
提纲号会被自动配置为锚点,可直接使用{{7}}{{7.1}}
|
提纲号会被自动配置为锚点,可直接使用{-7}{-7.1}
|
||||||
|
|
||||||
8 注释
|
8 注释
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user