Compare commits
2 Commits
bbc729f1c3
...
fefa0ad1b5
Author | SHA1 | Date | |
---|---|---|---|
fefa0ad1b5 | |||
dd9f7d2efb |
@ -6,12 +6,11 @@ import re
|
|||||||
import xml
|
import xml
|
||||||
from typing import *
|
from typing import *
|
||||||
|
|
||||||
|
import markdown.core
|
||||||
from markdown import Markdown
|
from markdown import Markdown
|
||||||
from markdown.blockprocessors import BlockProcessor
|
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.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 +24,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, InlineHilitePattern
|
from pymdownx.inlinehilite import InlineHiliteExtension
|
||||||
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,9 +39,10 @@ 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 xml.etree.ElementTree import ElementTree
|
||||||
|
|
||||||
from .Define import Variable
|
|
||||||
|
Variable = dict[str, str | tuple[str], list[str]] | None
|
||||||
|
|
||||||
|
|
||||||
class Simple(InlineProcessor):
|
class Simple(InlineProcessor):
|
||||||
@ -166,68 +166,6 @@ class Syllabus(BlockProcessor):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
class Anchor(InlineProcessor):
|
|
||||||
"""
|
|
||||||
{#定义锚点}
|
|
||||||
"""
|
|
||||||
|
|
||||||
def handleMatch(self, m: Match[str], data: str) -> (tuple[xml.etree.ElementTree.Element, int, int] |
|
|
||||||
tuple[None, None, None]):
|
|
||||||
"""
|
|
||||||
处理匹配
|
|
||||||
:param m: re模块的匹配对象
|
|
||||||
:param data: 被匹配的原始文本
|
|
||||||
:return: 标签 匹配开始 匹配结束
|
|
||||||
"""
|
|
||||||
tag = xml.etree.ElementTree.Element('span') # 创建标签
|
|
||||||
tag.text = m.group(1)
|
|
||||||
tag.set('id', m.group(1)) # 设置id
|
|
||||||
|
|
||||||
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]):
|
|
||||||
"""
|
|
||||||
处理匹配
|
|
||||||
:param m: re模块的匹配对象
|
|
||||||
:param data: 被匹配的原始文本
|
|
||||||
:return: 标签 匹配开始 匹配结束
|
|
||||||
"""
|
|
||||||
tag = xml.etree.ElementTree.Element('a') # 创建标签
|
|
||||||
tag.set('href', '#' + m.group(1)) # 设置id
|
|
||||||
tag.text = m.group(1)
|
|
||||||
|
|
||||||
return tag, m.start(), m.end()
|
|
||||||
|
|
||||||
|
|
||||||
class BasicExtension(Extension):
|
class BasicExtension(Extension):
|
||||||
"""
|
"""
|
||||||
渲染基本样式
|
渲染基本样式
|
||||||
@ -248,42 +186,16 @@ class BasicExtension(Extension):
|
|||||||
md.parser.blockprocessors.register(Syllabus(md.parser), 'syllabus', 182) # 渲染提纲
|
md.parser.blockprocessors.register(Syllabus(md.parser), 'syllabus', 182) # 渲染提纲
|
||||||
|
|
||||||
|
|
||||||
class AnchorExtension(Extension):
|
|
||||||
def extendMarkdown(self, md: Markdown):
|
|
||||||
"""
|
|
||||||
添加扩展
|
|
||||||
: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:
|
class InlineCode:
|
||||||
def __init__(self, variable: Variable):
|
def __init__(self, variable: Variable):
|
||||||
self.variable = variable
|
self.variable = variable
|
||||||
|
|
||||||
def __call__(self, source, language, css_class, md): # 自定义的单行代码格式化器
|
def __call__(self, source: str, language: str, css_class: str, md: markdown.core.Markdown): # 自定义的单行代码格式化器
|
||||||
if language != '': # 调用默认格式化函数
|
if language != '': # 调用默认格式化函数
|
||||||
return md.inlinePatterns['backtick'].highlight_code(src=source, language=language, classname=css_class,
|
return md.inlinePatterns['backtick'].highlight_code(
|
||||||
md=md)
|
src=source, language=language, classname=css_class,
|
||||||
|
md=md
|
||||||
|
)
|
||||||
match tuple(source):
|
match tuple(source):
|
||||||
case '{', '#', *archers, '}': # 匹配到{#锚点}
|
case '{', '#', *archers, '}': # 匹配到{#锚点}
|
||||||
archer = ''.join(archers)
|
archer = ''.join(archers)
|
||||||
@ -295,6 +207,8 @@ class InlineCode:
|
|||||||
variable = ''.join(variables)
|
variable = ''.join(variables)
|
||||||
if variable in self.variable:
|
if variable in self.variable:
|
||||||
return self.variable[variable]
|
return self.variable[variable]
|
||||||
|
else:
|
||||||
|
return variable
|
||||||
case _:
|
case _:
|
||||||
return f'<code>{source}</code>'
|
return f'<code>{source}</code>'
|
||||||
|
|
||||||
@ -345,7 +259,6 @@ Extensions = {
|
|||||||
|
|
||||||
# 自定义
|
# 自定义
|
||||||
'基本风格': BasicExtension(),
|
'基本风格': BasicExtension(),
|
||||||
# '锚点': AnchorExtension(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
from typing import *
|
|
||||||
|
|
||||||
|
|
||||||
Variable = dict[str, str | tuple[str], list[str]] | None
|
|
@ -1,12 +1,14 @@
|
|||||||
from typing import *
|
from typing import *
|
||||||
import pickle
|
import pickle
|
||||||
from .Core import main
|
from .Core import main, Variable, Syllabus
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'main', # 主函数
|
'main', # 主函数
|
||||||
'indent', # 添加空格
|
'indent', # 添加空格
|
||||||
'HEAD', # HTML头部引用
|
'HEAD', # HTML头部引用
|
||||||
'Meta', # 元数据处理器
|
'Meta', # 元数据处理器
|
||||||
|
'Syllabus', # 提纲扩展
|
||||||
|
'Variable', # 变量类型提示
|
||||||
]
|
]
|
||||||
__version__ = '0.11.2'
|
__version__ = '0.11.2'
|
||||||
__author__ = 'CrossDark'
|
__author__ = 'CrossDark'
|
||||||
|
@ -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>`{突出内容}`</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>
|
||||||
|
Loading…
Reference in New Issue
Block a user