Compare commits

..

No commits in common. "bbc729f1c359d59a303fb53d43bf53a92db8d8d4" and "5ab56b2e8b1622f3fc952804631ac6e5ce179356" have entirely different histories.

4 changed files with 38 additions and 120 deletions

View File

@ -2,6 +2,7 @@
核心代码
"""
import re
import xml
from typing import *
@ -11,7 +12,6 @@ from markdown.blockprocessors import BlockProcessor
from markdown.extensions import Extension, meta, toc, wikilinks, legacy_attrs
from markdown.inlinepatterns import InlineProcessor
from markdown.preprocessors import Preprocessor
from markdown.treeprocessors import Treeprocessor
from pymdownx.arithmatex import ArithmatexExtension
from pymdownx.blocks import BlocksExtension
@ -25,7 +25,7 @@ from pymdownx.emoji import EmojiExtension
from pymdownx.extra import ExtraExtension
from pymdownx.fancylists import FancyListExtension
from pymdownx.highlight import HighlightExtension
from pymdownx.inlinehilite import InlineHiliteExtension, InlineHilitePattern
from pymdownx.inlinehilite import InlineHiliteExtension
from pymdownx.keys import KeysExtension
from pymdownx.mark import MarkExtension
from pymdownx.progressbar import ProgressBarExtension
@ -40,8 +40,6 @@ from pymdownx.pathconverter import PathConverterExtension
import kbdextension
import markdown_gfm_admonition
from xml.etree.ElementTree import ElementTree, Element, fromstring
from .Define import Variable
@ -110,7 +108,7 @@ class ID(InlineProcessor):
需要对HTML标签设置ID实现的样式
"""
def __init__(self, pattern: str, tag: str, property_: str, value: str | bool = None):
def __init__(self, pattern: str, tag: str, property_: str, value: Union[str, bool] = None):
"""
初始化
:param pattern: 正则表达式
@ -170,9 +168,8 @@ 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模块的匹配对象
@ -186,35 +183,12 @@ class Anchor(InlineProcessor):
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):
"""
{行内链接}
"""
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模块的匹配对象
@ -255,48 +229,8 @@ class AnchorExtension(Extension):
:param md: 转换器
"""
md.registerExtension(self) # 注册扩展
md.inlinePatterns.register(Anchor(r'\{#([^{}#]+)}'), 'anchor', 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>'
# md.inlinePatterns.register(Anchor(r'\{{#([^{}#]+)}}'), 'anchor', 0) # 定义锚点
md.inlinePatterns.register(LinkLine(r'\{{([^{}#]+)}}'), 'line_link', 0) # 添加页内链接
Extensions = {
@ -313,8 +247,8 @@ Extensions = {
{
'name': 'mermaid',
'class': 'mermaid',
'format': fence_div_format,
},
'format': fence_div_format
}
]
},
}),
@ -327,6 +261,7 @@ Extensions = {
'标签': TabExtension(),
'批评': CriticExtension(),
'代码高亮': HighlightExtension(),
'行内高亮': InlineHiliteExtension(),
'按键风格': KeysExtension(),
'高亮': MarkExtension(),
'进度条': ProgressBarExtension(),
@ -345,11 +280,11 @@ Extensions = {
# 自定义
'基本风格': 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: 输入文本
@ -358,15 +293,5 @@ def main(text: str, variable: Variable = None) -> tuple[str, Variable]:
"""
if variable is None:
variable = {}
md = Markdown(extensions=list(Extensions.values()) + [
InlineHiliteExtension(
custom_inline=[
{
'name': '*',
'class': 'block',
'format': InlineCode(variable=variable),
},
]
),
])
md = Markdown(extensions=list(Extensions.values()))
return md.convert(text), md.Meta

View File

@ -1,4 +1,4 @@
from typing import *
Variable = dict[str, str | tuple[str], list[str]] | None
Variable = Dict[str, Union[str, Tuple[str], List[str]]] | None

View File

@ -59,11 +59,7 @@
<li><a href="#2.2">2.2 赋值</a></li>
</ul>
</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="#3">3 锚点</a></li>
<li><a href="#4">4 代码块</a><ul>
<li><a href="#4.1">4.1 单行</a><ul>
<li><a href="#4.1.1">4.1.1 LaTex</a></li>
@ -186,10 +182,6 @@
<h2 id="2.2">2.2 赋值</h2>
<p>直接在文本中使用 <abbr title="长的文本">缩写</abbr> 即可</p>
<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>
<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>
@ -207,7 +199,7 @@
<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>
<h3 id="4.1.4">4.1.4 突出</h3>
<p>`{突出内容}`</p>
<p><code>{突出内容}</code></p>
<h2 id="4.2">4.2 多行</h2>
<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>
@ -217,7 +209,10 @@
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</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"> 9</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 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>
@ -225,6 +220,9 @@
<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">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>
<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>
@ -314,7 +312,7 @@
<p>7.1.3. 错误示范</p>
<p>不能出现两个及以上连续的点,例如:</p>
<p>7..1...4 错误示范</p>
<p>提纲号会被自动配置为锚点,可直接使用{-7}{-7.1}</p>
<p>提纲号会被自动配置为锚点,可直接使用<a href="#7">7</a><a href="#7.1">7.1</a></p>
<h1 id="8">8 注释</h1>
<!-- 这是注释 -->
@ -355,7 +353,7 @@
<li>c</li>
</ul>
</dd>
<dt>强调值</dt>
<dt><code>{强调变量}</code></dt>
<dd>
<ul>
<li>a</li>

View File

@ -90,14 +90,6 @@ liuhanbo333@icloud.com
3 锚点
3.1 定义
`{#锚点}`
3.2 链接
`{-锚点}`
4 代码块
4.1 `单行`
@ -146,6 +138,9 @@ B:
- a
- b
- c
{[强调变量]}:
- a
- b
```
4.2.2 Python
@ -245,7 +240,7 @@ def main():
7..1...4 错误示范
提纲号会被自动配置为锚点,可直接使用{-7}{-7.1}
提纲号会被自动配置为锚点,可直接使用{{7}}{{7.1}}
8 注释