forked from CrossDark/CrossDown
1.1.0锚点有点问题
This commit is contained in:
parent
7c9d21b324
commit
d255f8e2aa
@ -26,9 +26,9 @@ Extensions = {
|
|||||||
"WikiLinks": "markdown.extensions.wikilinks",
|
"WikiLinks": "markdown.extensions.wikilinks",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try: # 检测当前平台是否支持扩展语法
|
try: # 检测当前平台是否支持扩展语法
|
||||||
import Extra
|
import Extra
|
||||||
|
|
||||||
Extensions += Extra.EXTRA
|
Extensions += Extra.EXTRA
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
EXTRA_ABLE = False
|
EXTRA_ABLE = False
|
||||||
@ -43,7 +43,7 @@ class Style(Preprocessor):
|
|||||||
def underline(text):
|
def underline(text):
|
||||||
"""
|
"""
|
||||||
~下划线~
|
~下划线~
|
||||||
:return:
|
:return: text
|
||||||
"""
|
"""
|
||||||
return re.sub(r'~([^~\n]+)~', r'<u>\1</u>', text)
|
return re.sub(r'~([^~\n]+)~', r'<u>\1</u>', text)
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ class Style(Preprocessor):
|
|||||||
def strikethrough(text):
|
def strikethrough(text):
|
||||||
"""
|
"""
|
||||||
~~删除线~~
|
~~删除线~~
|
||||||
:return:
|
:return: text
|
||||||
"""
|
"""
|
||||||
return re.sub(r'~~([^~\n]+)~~', r'<s>\1</s>', text)
|
return re.sub(r'~~([^~\n]+)~~', r'<s>\1</s>', text)
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ class Style(Preprocessor):
|
|||||||
def highlight(text):
|
def highlight(text):
|
||||||
"""
|
"""
|
||||||
==高亮==
|
==高亮==
|
||||||
:return:
|
:return: text
|
||||||
"""
|
"""
|
||||||
return re.sub(r'==([^=\n]+)==', r'<mark>\1</mark>', text)
|
return re.sub(r'==([^=\n]+)==', r'<mark>\1</mark>', text)
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ class Style(Preprocessor):
|
|||||||
def up(text):
|
def up(text):
|
||||||
"""
|
"""
|
||||||
[在文本的正上方添加一行小文本]^(主要用于标拼音)
|
[在文本的正上方添加一行小文本]^(主要用于标拼音)
|
||||||
:return:
|
:return: text
|
||||||
"""
|
"""
|
||||||
return re.sub(r'\[(.*?)]\^\((.*?)\)', r'<ruby>\1<rt>\2</rt></ruby>', text)
|
return re.sub(r'\[(.*?)]\^\((.*?)\)', r'<ruby>\1<rt>\2</rt></ruby>', text)
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ class Style(Preprocessor):
|
|||||||
def hide(text):
|
def hide(text):
|
||||||
"""
|
"""
|
||||||
[在指定的文本里面隐藏一段文本]-(只有鼠标放在上面才会显示隐藏文本)
|
[在指定的文本里面隐藏一段文本]-(只有鼠标放在上面才会显示隐藏文本)
|
||||||
:return:
|
:return: text
|
||||||
"""
|
"""
|
||||||
return re.sub(r'\[(.*?)]-\((.*?)\)', r'<span title="\2">\1</span>', text)
|
return re.sub(r'\[(.*?)]-\((.*?)\)', r'<span title="\2">\1</span>', text)
|
||||||
|
|
||||||
@ -117,7 +117,8 @@ class Value(Preprocessor):
|
|||||||
if match
|
if match
|
||||||
for key, value in [match.groups()]
|
for key, value in [match.groups()]
|
||||||
} # 识别变量定义
|
} # 识别变量定义
|
||||||
print(values)
|
anchors = re.findall(r'\{#([^{}#]+)}', '\n'.join(lines)) # 识别锚点定义
|
||||||
|
print(anchors)
|
||||||
for index, line in enumerate(lines):
|
for index, line in enumerate(lines):
|
||||||
if any(value in line for value in values): # 匹配到了变量
|
if any(value in line for value in values): # 匹配到了变量
|
||||||
for key, value in values.items():
|
for key, value in values.items():
|
||||||
@ -125,10 +126,15 @@ class Value(Preprocessor):
|
|||||||
lines.remove(line) # 删除
|
lines.remove(line) # 删除
|
||||||
else: # 应该被赋值
|
else: # 应该被赋值
|
||||||
lines[index] = re.sub('{' + key + '}', value, line) # 对变量赋值
|
lines[index] = re.sub('{' + key + '}', value, line) # 对变量赋值
|
||||||
|
elif any(anchor in line for anchor in anchors): # 匹配到了锚点
|
||||||
|
if re.search('\{#', line): # 是锚点定义
|
||||||
|
lines[index] = re.sub(r'\{#(.+)}', r'<span id="\1"></span>', line) # 定义锚点
|
||||||
|
else: # 是页内链接
|
||||||
|
pass
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
|
||||||
class Header(Treeprocessor):
|
class Tag(Treeprocessor):
|
||||||
def run(self, root):
|
def run(self, root):
|
||||||
"""
|
"""
|
||||||
通过修改AST来给标题添加锚点
|
通过修改AST来给标题添加锚点
|
||||||
@ -136,9 +142,15 @@ class Header(Treeprocessor):
|
|||||||
for header in root.iter():
|
for header in root.iter():
|
||||||
if header.tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'): # 查找标题
|
if header.tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'): # 查找标题
|
||||||
header.set('id', header.text.split(' ')[0]) # 给标题添加锚点
|
header.set('id', header.text.split(' ')[0]) # 给标题添加锚点
|
||||||
|
if header.text is not None: # 不是空行
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Basic(Extension):
|
class Basic(Extension):
|
||||||
|
"""
|
||||||
|
基本扩展
|
||||||
|
"""
|
||||||
|
|
||||||
def extendMarkdown(self, md):
|
def extendMarkdown(self, md):
|
||||||
md.registerExtension(self) # 注册扩展
|
md.registerExtension(self) # 注册扩展
|
||||||
md.preprocessors.register(Style(md), 'style', 0)
|
md.preprocessors.register(Style(md), 'style', 0)
|
||||||
@ -146,15 +158,23 @@ class Basic(Extension):
|
|||||||
|
|
||||||
|
|
||||||
class More(Extension):
|
class More(Extension):
|
||||||
|
"""
|
||||||
|
高级扩展
|
||||||
|
"""
|
||||||
|
|
||||||
def extendMarkdown(self, md):
|
def extendMarkdown(self, md):
|
||||||
md.preprocessors.register(Value(md), 'values', 0)
|
md.preprocessors.register(Value(md), 'values', 0)
|
||||||
|
|
||||||
|
|
||||||
class Decorate(Extension):
|
class Decorate(Extension):
|
||||||
|
"""
|
||||||
|
修饰扩展,最后处理
|
||||||
|
"""
|
||||||
|
|
||||||
def extendMarkdown(self, md):
|
def extendMarkdown(self, md):
|
||||||
md.treeprocessors.register(Header(md), 'header', 0)
|
md.treeprocessors.register(Tag(md), 'header', 0)
|
||||||
|
|
||||||
|
|
||||||
def main(text: str) -> Tuple[str, Dict[str, List[str]]]:
|
def main(text: str) -> Tuple[str, Dict[str, List[str]]]:
|
||||||
md = Markdown(extensions=[Basic(), More()] + list(Extensions.values()) + [Decorate()])
|
md = Markdown(extensions=[Basic(), More()] + list(Extensions.values()) + [Decorate()], safe_mode=False)
|
||||||
return md.convert(text), md.Meta
|
return md.convert(text), md.Meta
|
||||||
|
@ -77,6 +77,7 @@
|
|||||||
<li><a href="#31">3.1 单行</a><ul>
|
<li><a href="#31">3.1 单行</a><ul>
|
||||||
<li><a href="#311-latex">3.1.1 LaTex</a></li>
|
<li><a href="#311-latex">3.1.1 LaTex</a></li>
|
||||||
<li><a href="#312">3.1.2 函数</a></li>
|
<li><a href="#312">3.1.2 函数</a></li>
|
||||||
|
<li><a href="#313">3.1.3 强调</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
<li><a href="#32">3.2 多行</a><ul>
|
<li><a href="#32">3.2 多行</a><ul>
|
||||||
@ -155,7 +156,7 @@
|
|||||||
<p>值 {锚点名}</p>
|
<p>值 {锚点名}</p>
|
||||||
<p>提纲的编号已经自动配置为了锚点,可直接使用{2}</p>
|
<p>提纲的编号已经自动配置为了锚点,可直接使用{2}</p>
|
||||||
<h2 id="2.3">2.3 添加锚点</h2>
|
<h2 id="2.3">2.3 添加锚点</h2>
|
||||||
<p>{#锚点名}</p>
|
<p><span id="锚点名"></span></p>
|
||||||
<h1 id="3">3 代码块</h1>
|
<h1 id="3">3 代码块</h1>
|
||||||
<h2 id="3.1">3.1 <code>单行</code></h2>
|
<h2 id="3.1">3.1 <code>单行</code></h2>
|
||||||
<h3 id="3.1.1">3.1.1 LaTex</h3>
|
<h3 id="3.1.1">3.1.1 LaTex</h3>
|
||||||
@ -165,6 +166,8 @@
|
|||||||
<p><code>¥y=x*2+1¥</code> // 不定义范围</p>
|
<p><code>¥y=x*2+1¥</code> // 不定义范围</p>
|
||||||
<p><code>¥y=x**2¥€-50,50€</code> // 定义了x范围</p>
|
<p><code>¥y=x**2¥€-50,50€</code> // 定义了x范围</p>
|
||||||
<p><code>¥y=x**3¥€-50,50|-100,100€</code> // 定义了y范围</p>
|
<p><code>¥y=x**3¥€-50,50|-100,100€</code> // 定义了y范围</p>
|
||||||
|
<h3 id="3.1.3">3.1.3 强调</h3>
|
||||||
|
<p><code>{强调文本}</code></p>
|
||||||
<h2 id="3.2">3.2 多行</h2>
|
<h2 id="3.2">3.2 多行</h2>
|
||||||
<h3 id="3.2.1">3.2.1 YAML</h3>
|
<h3 id="3.2.1">3.2.1 YAML</h3>
|
||||||
<p><code>A:
|
<p><code>A:
|
||||||
@ -276,8 +279,8 @@
|
|||||||
<p>:grinning_face_with_big_eyes:</p>
|
<p>:grinning_face_with_big_eyes:</p>
|
||||||
<h1 id="12">12 扩展语法</h1>
|
<h1 id="12">12 扩展语法</h1>
|
||||||
<h2 id="12.1">12.1 警告</h2>
|
<h2 id="12.1">12.1 警告</h2>
|
||||||
<div class="admonition danger">
|
<div class="admonition 危险">
|
||||||
<p class="admonition-title">Don’t try this at home</p>
|
<p class="admonition-title">危险</p>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user