1.8.0 强调块支持变量赋值

This commit is contained in:
跨越晨昏 2024-10-13 22:13:10 +08:00
parent 38910cbe8b
commit ff42b7683c
4 changed files with 47 additions and 15 deletions

View File

@ -12,7 +12,7 @@ import xml
import emoji import emoji
Extensions = { Extensions = {
"Extra": extra.ExtraExtension(), # 基本扩展 "Extra": extra.ExtraExtension(fenced_code={'lang_prefix': ''}), # 基本扩展
"Admonition": admonition.AdmonitionExtension(), "Admonition": admonition.AdmonitionExtension(),
"Meta-Data": meta.MetaExtension(), "Meta-Data": meta.MetaExtension(),
"Sane Lists": sane_lists.SaneListExtension(), "Sane Lists": sane_lists.SaneListExtension(),
@ -20,6 +20,7 @@ Extensions = {
"WikiLinks": wikilinks.WikiLinkExtension(), "WikiLinks": wikilinks.WikiLinkExtension(),
} }
try: # 检测当前平台是否支持扩展语法 try: # 检测当前平台是否支持扩展语法
from .Extra import * from .Extra import *
EXTRA_ABLE = True EXTRA_ABLE = True
@ -183,6 +184,10 @@ class LinkLine(InlineProcessor):
class CodeLine(Treeprocessor): class CodeLine(Treeprocessor):
def __init__(self, variable: Dict):
super().__init__()
self.variable = variable
def run(self, root): def run(self, root):
for elem in root.iter('p'): # 在所有段落中查找单行代码 for elem in root.iter('p'): # 在所有段落中查找单行代码
if elem.findall('code'): # 找到单行代码 if elem.findall('code'): # 找到单行代码
@ -212,7 +217,11 @@ class CodeLine(Treeprocessor):
elif re.match(r'\{[^$]*}', code.text): # 是强调 elif re.match(r'\{[^$]*}', code.text): # 是强调
code.tag = 'span' code.tag = 'span'
code.set('class', 'block') code.set('class', 'block')
code.text = code.text[1:-1] key = code.text[1:-1] # 去掉两边的{}
if key in self.variable:
code.text = self.variable[key]
else:
code.text = key
class CodeBlock(Treeprocessor): class CodeBlock(Treeprocessor):
@ -291,12 +300,16 @@ class Anchor(Extension):
class Code(Extension): class Code(Extension):
def __init__(self, variable: Dict):
super().__init__()
self.variable = variable
def extendMarkdown(self, md: Markdown) -> None: def extendMarkdown(self, md: Markdown) -> None:
md.registerExtension(self) # 注册扩展 md.registerExtension(self) # 注册扩展
md.treeprocessors.register(CodeLine(), 'code_line', 0) # 渲染单行代码块 md.treeprocessors.register(CodeLine(variable=self.variable), 'code_line', 0) # 渲染单行代码块
md.treeprocessors.register(CodeBlock(), 'code_block', 1) # 渲染多行代码块 # md.treeprocessors.register(CodeBlock(), 'code_block', 1) # 渲染多行代码块
def main(text: str) -> Tuple[str, Dict[str, List[str]]]: def main(text: str) -> Tuple[str, Dict[str, List[str]]]:
md = Markdown(extensions=[Basic(), Box(), Anchor()] + list(Extensions.values()) + [Code()]) md = Markdown(extensions=[Basic(), Box(), Anchor()] + list(Extensions.values()) + [Code({'a': 'b', '强调变量': '强调值'})])
return md.convert(text), md.Meta return md.convert(text), md.Meta

View File

@ -20,7 +20,18 @@ HEAD = (
'<link href="https://cdn.jsdelivr.net/npm/prismjs/themes/prism.css" rel="stylesheet" />', '<link href="https://cdn.jsdelivr.net/npm/prismjs/themes/prism.css" rel="stylesheet" />',
'<script src="https://cdn.jsdelivr.net/npm/prismjs/prism.js"></script>', '<script src="https://cdn.jsdelivr.net/npm/prismjs/prism.js"></script>',
'<script src="https://cdn.jsdelivr.net/npm/prismjs/components/prism-yaml.min.js"></script>', '<script src="https://cdn.jsdelivr.net/npm/prismjs/components/prism-yaml.min.js"></script>',
# mermaid
'<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>', '<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>',
'<script>',
' mermaid.initialize({startOnLoad:true});',
'</script>',
# Highlight.js
'<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/styles/default.min.css">',
'<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/highlight.min.js"></script>',
'<script>hljs.highlightAll();</script>',
'<style>', '<style>',
' .block {', ' .block {',
' background-color: grey; /* 灰色背景 */', ' background-color: grey; /* 灰色背景 */',
@ -30,9 +41,7 @@ HEAD = (
) )
BODY = ( BODY = (
'<script>', '',
' mermaid.initialize({startOnLoad:true});',
'</script>',
) )

File diff suppressed because one or more lines are too long

View File

@ -97,6 +97,8 @@ ___
这是`$H_2O$`水 这是`$H_2O$`水
`$\lg\left(\frac{目标生物的理智值}{稳定折磨型工具人的理智值}\right)$`
4.1.2 函数 4.1.2 函数
`¥y=x*2+1¥` // 不定义范围 `¥y=x*2+1¥` // 不定义范围
@ -109,6 +111,8 @@ ___
`{强调文本}` `{强调文本}`
`{强调变量}`
4.2 多行 4.2 多行
4.2.1 YAML 4.2.1 YAML