0.8.3提纲会匹配以点结尾的提纲号

This commit is contained in:
跨越晨昏 2024-09-21 20:45:45 +08:00
parent 3becbd9f47
commit d57651aee1
3 changed files with 110 additions and 79 deletions

View File

@ -1,7 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/CrossDown.egg-info" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/dist" />
</content>
<orderEntry type="jdk" jdkName="Python 3.11" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

View File

@ -164,6 +164,7 @@ class Value:
赋值: {变量或锚点名}
锚点: {#锚点名}
"""
def __init__(self, text: str):
self.text = text
self.values = {
@ -225,7 +226,8 @@ class CodeBlock:
if head in ('', 'yaml'):
self.codes[index] = f'<pre><code class="language-yaml">{code}</code></pre>'
elif head in ('shell', 'python'):
self.codes[index] = f'<pre><code class="language-{head}">{re.sub(f"({head})", "", code)}</code></pre>'
self.codes[
index] = f'<pre><code class="language-{head}">{re.sub(f"({head})", "", code)}</code></pre>'
elif head in ('mermaid',):
self.codes[index] = f'<div class="{head}">{re.sub(f"({head})", "", code)}</div>'
elif re.match(r'\$[^$]*\$', code): # 是LaTex代码(单行)
@ -297,11 +299,13 @@ class Cite:
"""
> 渲染引用 --[引用来源]
"""
def __init__(self, text):
self.text = text
def __call__(self, *args, **kwargs) -> str:
self.text = re.sub('> (.*?) --\[(.*?)]\n', r'<blockquote>\1<footer><cite>\2</cite></footer></blockquote>', self.text) # 渲染有来源的引用
self.text = re.sub('> (.*?) --\[(.*?)]\n', r'<blockquote>\1<footer><cite>\2</cite></footer></blockquote>',
self.text) # 渲染有来源的引用
self.text = re.sub('> (.*?)\n', r'<blockquote>\1</blockquote>\n', self.text) # 渲染没有来源的引用
return self.text
@ -312,15 +316,19 @@ class Syllabus:
2 找到符合若干个数字++数字且首尾都是数字的行
每个提纲编号全文只能出现一次
"""
def __init__(self, text: str):
self.text = text
self.syllabus = {tuple(syllabus[0].split('.')): syllabus[1] for syllabus in [re.match(r'([\.|\d]+) ([^ ]+?)\n', i) for i in self.text.split('\n')] if syllabus is not None} # 找出提纲
print(self.syllabus)
def __call__(self, *args, **kwargs):
for num, txt in self.syllabus.items():
self.text = re.sub(f'{".".join(num)} {re.escape(txt)}', f'{"#" * len(num)}{".".join(num)} {txt}{{#' + '.'.join(num) + f'}}\n', self.text) # 按照层级为提纲添加不同等级的标题并创建锚点
return self.text
return '\n'.join([
(lambda match, origen:
re.sub(f'^({match.groups()[0]})',
fr'{"#" * len(match.groups()[0].split("."))} \1{{#' + match.groups()[0] + '}', origen)
if match is not None else origen) # 对于不是提纲的行,直接返回原始字符
(re.match(r'^([\d.]+) ', line), line) # 匹配提纲号
for line in self.text.splitlines() # 分割并遍历文本的每一行
])
class Basic:

View File

@ -25,52 +25,52 @@
</script>
<h1>CrossDown</h1>
<p>自制的markdown添加了一些自定义的语法
效果请见<a href="https://github.com/CrossDark/CrossDown/blob/main/README.html">README.html</a>
1 基本语法
1.1 标题</p>
效果请见<a href="https://github.com/CrossDark/CrossDown/blob/main/README.html">README.html</a></p>
<h1>1<span id="1"></span> 基本语法</h1>
<h2>1.1<span id="1.1"></span> 标题</h2>
<h1>一级标题</h1>
<h2>二级标题</h2>
<h3>三级标题</h3>
<h4>四级标题</h4>
<h5>五级标题</h5>
<h6>六级标题</h6>
<p>1.2 样式
1.2.1 <em>斜体</em>
1.2.2 <strong>粗体</strong>
1.2.3 <strong><em>粗斜体</em></strong>
1.2.4 <u>下划线</u>
1.2.5 <s>删除线</s>
1.2.6 <mark>高亮</mark>
1.2.7 <ruby>在文本的正上方添加一行小文本<rt>主要用于标拼音</rt></ruby>
1.2.8 <span title="只有鼠标放在上面才会显示隐藏文本">在指定的文本里面隐藏一段文本</span>
1.2.9 分割线</p>
<h2>1.2<span id="1.2"></span> 样式</h2>
<h3>1.2.1<span id="1.2.1"></span> <em>斜体</em></h3>
<h3>1.2.2<span id="1.2.2"></span> <strong>粗体</strong></h3>
<h3>1.2.3<span id="1.2.3"></span> <strong><em>粗斜体</em></strong></h3>
<h3>1.2.4<span id="1.2.4"></span> <u>下划线</u></h3>
<h3>1.2.5<span id="1.2.5"></span> <s>删除线</s></h3>
<h3>1.2.6<span id="1.2.6"></span> <mark>高亮</mark></h3>
<h3>1.2.7<span id="1.2.7"></span> <ruby>在文本的正上方添加一行小文本<rt>主要用于标拼音</rt></ruby></h3>
<h3>1.2.8<span id="1.2.8"></span> <span title="只有鼠标放在上面才会显示隐藏文本">在指定的文本里面隐藏一段文本</span></h3>
<h3>1.2.9<span id="1.2.9"></span> 分割线</h3>
<hr />
<hr />
<hr />
<p>1.3 链接
1.3.1 普通链接
<a href="链接地址">链接文本</a>
<a href="https://crossdark.com">CrossDark</a>
1.3.2 图片
<img alt="链接图片" src="链接地址" />
<img alt="sea" src="https://crossdark.com/wp-content/uploads/2024/05/1715259682-sea.jpg" />
1.3.3 变量链接
<a href="https://crossdark.com">链接文本</a></p>
<p>2 变量
2.1 定义</p>
<p>2.2 赋值
<a href="#锚点名">锚点名</a>
提纲的编号已经自动配置为了锚点,可直接使用{2}
2.3 添加锚点
<span id="锚点名"></span>
3 代码块
3.1 <span class="block">单行</span>
3.1.1 LaTex
<p>\(CO_2\)</p>
<p>\(H_2O\)</p>
3.2 多行
3.2.1 YAML
<pre><code class="language-yaml">
<h2>1.3<span id="1.3"></span> 链接</h2>
<h3>1.3.1<span id="1.3.1"></span> 普通链接</h3>
<p><a href="链接地址">链接文本</a>
<a href="https://crossdark.com">CrossDark</a></p>
<h3>1.3.2<span id="1.3.2"></span> 图片</h3>
<p><img alt="链接图片" src="链接地址" />
<img alt="sea" src="https://crossdark.com/wp-content/uploads/2024/05/1715259682-sea.jpg" /></p>
<h3>1.3.3<span id="1.3.3"></span> 变量链接</h3>
<p><a href="https://crossdark.com">链接文本</a></p>
<h1>2<span id="2"></span> 变量</h1>
<h2>2.1<span id="2.1"></span> 定义</h2>
<h2>2.2<span id="2.2"></span> 赋值</h2>
<p><a href="#锚点名">锚点名</a>
提纲的编号已经自动配置为了锚点,可直接使用<a href="#2">2</a></p>
<h2>2.3<span id="2.3"></span> 添加锚点</h2>
<p><span id="锚点名"></span></p>
<h1>3<span id="3"></span> 代码块</h1>
<h2>3.1<span id="3.1"></span> <span class="block">单行</span></h2>
<h3>3.1.1<span id="3.1.1"></span> LaTex</h3>
<p><p>\(CO_2\)</p>
<p>\(H_2O\)</p></p>
<h2>3.2<span id="3.2"></span> 多行</h2>
<h3>3.2.1<span id="3.2.1"></span> YAML</h3>
<p><pre><code class="language-yaml">
A:
1. a
2. b
@ -79,24 +79,24 @@
- a
- b
- c
</code></pre>
3.2.2 Python
<pre><code class="language-python">
</code></pre></p>
<h3>3.2.2<span id="3.2.2"></span> Python</h3>
<p><pre><code class="language-python">
print('CrossDown')
</code></pre>
3.2.3 Mermaid
<div class="mermaid">
</code></pre></p>
<h3>3.2.3<span id="3.2.3"></span> Mermaid</h3>
<p><div class="mermaid">
graph LR
A-->B
A-->C
B-->D
C-->D
</div>
4 转义
\
</div></p>
<h1>4<span id="4"></span> 转义</h1>
<p>\
\a
*
5 引用</p>
*</p>
<h1>5<span id="5"></span> 引用</h1>
<blockquote>
<p>一级引用</p>
<blockquote>
@ -116,27 +116,46 @@
</blockquote>
<p>引文内添加<em>斜体</em><strong>粗体</strong><u>下划线</u><s>删除线</s><mark>高亮</mark></p>
</blockquote>
<p>6 提纲</p>
<p>7 注释
7.1 强注释</p>
<p>7.2 弱注释
只有在
<span class="block">// 代码中的注释弱不会被移除</span>
8 列表
8.1 有序列表
1. a
2. b
3. c
4. d</p>
<p>8.2 无序列表
- A
- B
- C
- D</p>
<p>9 表格
| 表头1 | 表头2 | 表头3 |<br />
| :--: | :--: | :--: |<br />
| 单元格1 | 单元格2 | 单元格3 |<br />
| 单元格4 | 单元格5 | 单元格6 |</p>
<h1>6<span id="6"></span> 提纲</h1>
<h1>7<span id="7"></span> 注释</h1>
<h2>7.1<span id="7.1"></span> 强注释</h2>
<h2>7.2<span id="7.2"></span> 弱注释</h2>
<p>只有在
<span class="block">// 代码中的注释弱不会被移除</span></p>
<h1>8<span id="8"></span> 列表</h1>
<h2>8.1<span id="8.1"></span> 有序列表</h2>
<h2>1.<span id="1."></span> a</h2>
<h2>2.<span id="2."></span> b</h2>
<h2>3.<span id="3."></span> c</h2>
<h2>4.<span id="4."></span> d</h2>
<h2>8.2<span id="8.2"></span> 无序列表</h2>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
<h1>9<span id="9"></span> 表格</h1>
<table>
<thead>
<tr>
<th style="text-align: center;">表头1</th>
<th style="text-align: center;">表头2</th>
<th style="text-align: center;">表头3</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;">单元格1</td>
<td style="text-align: center;">单元格2</td>
<td style="text-align: center;">单元格3</td>
</tr>
<tr>
<td style="text-align: center;">单元格4</td>
<td style="text-align: center;">单元格5</td>
<td style="text-align: center;">单元格6</td>
</tr>
</tbody>
</table>
</body>
</html>