3.4.2 自定义InlineHilite扩展成功
Some checks are pending
Publish to PyPI on main branch update / build_and_publish (push) Waiting to run

This commit is contained in:
跨越晨昏 2024-11-21 19:03:57 +08:00
parent b4d4fff952
commit bbc729f1c3
3 changed files with 43 additions and 34 deletions

View File

@ -2,7 +2,6 @@
核心代码 核心代码
""" """
import re import re
import xml import xml
from typing import * from typing import *
@ -111,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: 正则表达式
@ -171,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模块的匹配对象
@ -212,8 +212,9 @@ 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模块的匹配对象
@ -275,20 +276,27 @@ class CodeExtension(Extension):
md.treeprocessors.register(CodeLine(variable=self.variable), 'code_line', 100) md.treeprocessors.register(CodeLine(variable=self.variable), 'code_line', 100)
def inline_formatter(source, language, css_class, md): # 自定义的单行代码格式化器 class InlineCode:
if language != '': # 调用默认格式化函数 def __init__(self, variable: Variable):
return md.inlinePatterns['backtick'].highlight_code(src=source, language=language, classname=css_class, md=md) self.variable = variable
match tuple(source):
case '{', '#', *archers, '}': # 匹配到{#锚点} def __call__(self, source, language, css_class, md): # 自定义的单行代码格式化器
archer = ''.join(archers) if language != '': # 调用默认格式化函数
return f'<span id="{archer}">{archer}</span>' return md.inlinePatterns['backtick'].highlight_code(src=source, language=language, classname=css_class,
case '{', '-', *inline_links, '}': # 匹配到{-行内链接} md=md)
inline_link = ''.join(inline_links) match tuple(source):
return f'<a href=#{inline_link}>{inline_link}</a>' case '{', '#', *archers, '}': # 匹配到{#锚点}
case '{', *variable, '}': # 匹配到{变量} archer = ''.join(archers)
return ''.join(variable) return f'<span id="{archer}">{archer}</span>'
case _: case '{', '-', *inline_links, '}': # 匹配到{-行内链接}
return f'<code>{source}</code>' 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 = {
@ -319,15 +327,6 @@ Extensions = {
'标签': TabExtension(), '标签': TabExtension(),
'批评': CriticExtension(), '批评': CriticExtension(),
'代码高亮': HighlightExtension(), '代码高亮': HighlightExtension(),
'行内高亮': InlineHiliteExtension(
custom_inline=[
{
'name': '*',
'class': 'block',
'format': inline_formatter,
},
]
),
'按键风格': KeysExtension(), '按键风格': KeysExtension(),
'高亮': MarkExtension(), '高亮': MarkExtension(),
'进度条': ProgressBarExtension(), '进度条': ProgressBarExtension(),
@ -350,7 +349,7 @@ Extensions = {
} }
def main(text: str, variable: Variable = None) -> Tuple[str, Variable]: def main(text: str, variable: Variable = None) -> tuple[str, Variable]:
""" """
主函数 主函数
:param text: 输入文本 :param text: 输入文本
@ -359,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

View File

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

View File

@ -207,7 +207,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><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>
@ -355,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>