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"?> <?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <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="jdk" jdkName="Python 3.11" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>

View File

@ -164,6 +164,7 @@ class Value:
赋值: {变量或锚点名} 赋值: {变量或锚点名}
锚点: {#锚点名} 锚点: {#锚点名}
""" """
def __init__(self, text: str): def __init__(self, text: str):
self.text = text self.text = text
self.values = { self.values = {
@ -225,7 +226,8 @@ class CodeBlock:
if head in ('', 'yaml'): if head in ('', 'yaml'):
self.codes[index] = f'<pre><code class="language-yaml">{code}</code></pre>' self.codes[index] = f'<pre><code class="language-yaml">{code}</code></pre>'
elif head in ('shell', 'python'): 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',): elif head in ('mermaid',):
self.codes[index] = f'<div class="{head}">{re.sub(f"({head})", "", code)}</div>' self.codes[index] = f'<div class="{head}">{re.sub(f"({head})", "", code)}</div>'
elif re.match(r'\$[^$]*\$', code): # 是LaTex代码(单行) elif re.match(r'\$[^$]*\$', code): # 是LaTex代码(单行)
@ -257,7 +259,7 @@ class Escape: # TODO 还有点问题
self.text = text self.text = text
self.escapes = { self.escapes = {
i: f'\0\1\2{i}\2\1\0' for i in re.findall(r'(\\.)', text) i: f'\0\1\2{i}\2\1\0' for i in re.findall(r'(\\.)', text)
} # 找出要转义的字符 } # 找出要转义的字符
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
""" """
@ -297,11 +299,13 @@ class Cite:
""" """
> 渲染引用 --[引用来源] > 渲染引用 --[引用来源]
""" """
def __init__(self, text): def __init__(self, text):
self.text = text self.text = text
def __call__(self, *args, **kwargs) -> str: 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) # 渲染没有来源的引用 self.text = re.sub('> (.*?)\n', r'<blockquote>\1</blockquote>\n', self.text) # 渲染没有来源的引用
return self.text return self.text
@ -312,15 +316,19 @@ class Syllabus:
2 找到符合若干个数字++数字且首尾都是数字的行 2 找到符合若干个数字++数字且首尾都是数字的行
每个提纲编号全文只能出现一次 每个提纲编号全文只能出现一次
""" """
def __init__(self, text: str): def __init__(self, text: str):
self.text = text 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): def __call__(self, *args, **kwargs):
for num, txt in self.syllabus.items(): return '\n'.join([
self.text = re.sub(f'{".".join(num)} {re.escape(txt)}', f'{"#" * len(num)}{".".join(num)} {txt}{{#' + '.'.join(num) + f'}}\n', self.text) # 按照层级为提纲添加不同等级的标题并创建锚点 (lambda match, origen:
return self.text 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: class Basic:

View File

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