3.4 emm自定义InlineHilite扩展有点问题

This commit is contained in:
跨越晨昏 2024-11-16 18:10:07 +08:00
parent 5ab56b2e8b
commit dd01c2b57e
3 changed files with 94 additions and 26 deletions

View File

@ -12,6 +12,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 +26,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 +41,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
@ -183,6 +186,28 @@ 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):
""" """
{行内链接} {行内链接}
@ -229,8 +254,36 @@ 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)
def variable_formatter(source, language, css_class, md):
if language != '':
return InlineHilitePattern.highlight_code(src=source, language=language, classname=css_class, md=md)
match tuple(source):
case '{', '#', *archers, '}': # 匹配到{#锚点}
archer = str(archers.items())
return f'<span id="{archer}">{archer}</span>'
case _:
return f'<code>{source}</code>' # Or string
Extensions = { Extensions = {
@ -247,8 +300,8 @@ Extensions = {
{ {
'name': 'mermaid', 'name': 'mermaid',
'class': 'mermaid', 'class': 'mermaid',
'format': fence_div_format 'format': fence_div_format,
} },
] ]
}, },
}), }),
@ -261,7 +314,15 @@ Extensions = {
'标签': TabExtension(), '标签': TabExtension(),
'批评': CriticExtension(), '批评': CriticExtension(),
'代码高亮': HighlightExtension(), '代码高亮': HighlightExtension(),
'行内高亮': InlineHiliteExtension(), '行内高亮': InlineHiliteExtension(
custom_inline=[
{
'name': '*',
'class': 'block',
'format': variable_formatter,
},
]
),
'按键风格': KeysExtension(), '按键风格': KeysExtension(),
'高亮': MarkExtension(), '高亮': MarkExtension(),
'进度条': ProgressBarExtension(), '进度条': ProgressBarExtension(),

View File

@ -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,9 +186,13 @@
<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><code>{-锚点}</code></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: `#!py3 import pymdownx; pymdownx.<strong>version</strong>`.</p>
<p>The mock shebang will be treated like text here: <code>#!js var test = 0;</code>.</p> <p>The mock shebang will be treated like text here: <code>#!js var test = 0;</code>.</p>
<h3 id="4.1.1">4.1.1 LaTex</h3> <h3 id="4.1.1">4.1.1 LaTex</h3>
<p>这是<span class="arithmatex"><span class="MathJax_Preview">CO_2</span><script type="math/tex">CO_2</script></span>二氧化碳,或者可以写成这样CO<sub>2</sub></p> <p>这是<span class="arithmatex"><span class="MathJax_Preview">CO_2</span><script type="math/tex">CO_2</script></span>二氧化碳,或者可以写成这样CO<sub>2</sub></p>
@ -202,17 +210,14 @@
<p><code>{突出内容}</code></p> <p><code>{突出内容}</code></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>

View File

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