Compare commits

..

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

4 changed files with 105 additions and 16 deletions

View File

@ -6,11 +6,12 @@ import re
import xml
from typing import *
import markdown.core
from markdown import Markdown
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
@ -24,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
from pymdownx.inlinehilite import InlineHiliteExtension, InlineHilitePattern
from pymdownx.keys import KeysExtension
from pymdownx.mark import MarkExtension
from pymdownx.progressbar import ProgressBarExtension
@ -39,10 +40,9 @@ from pymdownx.pathconverter import PathConverterExtension
import kbdextension
import markdown_gfm_admonition
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import ElementTree, Element, fromstring
Variable = dict[str, str | tuple[str], list[str]] | None
from .Define import Variable
class Simple(InlineProcessor):
@ -166,6 +166,68 @@ class Syllabus(BlockProcessor):
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):
"""
渲染基本样式
@ -186,16 +248,42 @@ class BasicExtension(Extension):
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:
def __init__(self, variable: Variable):
self.variable = variable
def __call__(self, source: str, language: str, css_class: str, md: markdown.core.Markdown): # 自定义的单行代码格式化器
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
)
return md.inlinePatterns['backtick'].highlight_code(src=source, language=language, classname=css_class,
md=md)
match tuple(source):
case '{', '#', *archers, '}': # 匹配到{#锚点}
archer = ''.join(archers)
@ -207,8 +295,6 @@ class InlineCode:
variable = ''.join(variables)
if variable in self.variable:
return self.variable[variable]
else:
return variable
case _:
return f'<code>{source}</code>'
@ -259,6 +345,7 @@ Extensions = {
# 自定义
'基本风格': BasicExtension(),
# '锚点': AnchorExtension(),
}

4
CrossDown/Define.py Normal file
View File

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

View File

@ -1,14 +1,12 @@
from typing import *
import pickle
from .Core import main, Variable, Syllabus
from .Core import main
__all__ = [
'main', # 主函数
'indent', # 添加空格
'HEAD', # HTML头部引用
'Meta', # 元数据处理器
'Syllabus', # 提纲扩展
'Variable', # 变量类型提示
]
__version__ = '0.11.2'
__author__ = 'CrossDark'

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><kbd>Enter</kbd></p>
<h3 id="4.1.4">4.1.4 突出</h3>
<p>突出内容</p>
<p>`{突出内容}`</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>