bellpress 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bellpress-0.1.0/.gitignore +19 -0
- bellpress-0.1.0/PKG-INFO +11 -0
- bellpress-0.1.0/README.md +173 -0
- bellpress-0.1.0/i18n/en.toml +37 -0
- bellpress-0.1.0/i18n/zh.toml +42 -0
- bellpress-0.1.0/pyproject.toml +57 -0
- bellpress-0.1.0/sitegen/__init__.py +5 -0
- bellpress-0.1.0/sitegen/blocks/__init__.py +152 -0
- bellpress-0.1.0/sitegen/blocks/algo.py +235 -0
- bellpress-0.1.0/sitegen/blocks/callout.py +136 -0
- bellpress-0.1.0/sitegen/blocks/deck.py +229 -0
- bellpress-0.1.0/sitegen/blocks/diagram.py +267 -0
- bellpress-0.1.0/sitegen/blocks/figure.py +52 -0
- bellpress-0.1.0/sitegen/blocks/layout.py +114 -0
- bellpress-0.1.0/sitegen/blocks/subfig.py +116 -0
- bellpress-0.1.0/sitegen/build.py +604 -0
- bellpress-0.1.0/sitegen/cli.py +121 -0
- bellpress-0.1.0/sitegen/content.py +211 -0
- bellpress-0.1.0/sitegen/i18n.py +91 -0
- bellpress-0.1.0/sitegen/markdown.py +269 -0
- bellpress-0.1.0/sitegen/mathml.py +36 -0
- bellpress-0.1.0/sitegen/refs.py +63 -0
- bellpress-0.1.0/sitegen/scaffold.py +83 -0
- bellpress-0.1.0/sitegen/skill_resources/sitegen-markdown/SKILL.md +229 -0
- bellpress-0.1.0/sitegen/skills.py +28 -0
- bellpress-0.1.0/sitegen/slides.py +152 -0
- bellpress-0.1.0/static/main.js +17 -0
- bellpress-0.1.0/static/math/OFL.txt +92 -0
- bellpress-0.1.0/static/math/STIX2Math.woff2 +0 -0
- bellpress-0.1.0/static/reveal/LICENSE +19 -0
- bellpress-0.1.0/static/reveal/embed.js +101 -0
- bellpress-0.1.0/static/reveal/notes.js +1 -0
- bellpress-0.1.0/static/reveal/reset.css +30 -0
- bellpress-0.1.0/static/reveal/reveal.css +8 -0
- bellpress-0.1.0/static/reveal/reveal.js +9 -0
- bellpress-0.1.0/static/reveal/slides.css +282 -0
- bellpress-0.1.0/static/search.js +93 -0
- bellpress-0.1.0/static/style.css +757 -0
- bellpress-0.1.0/static/tokens.css +68 -0
- bellpress-0.1.0/templates/404.html +9 -0
- bellpress-0.1.0/templates/archive.html +21 -0
- bellpress-0.1.0/templates/base.html +60 -0
- bellpress-0.1.0/templates/decks.html +16 -0
- bellpress-0.1.0/templates/feed.xml +21 -0
- bellpress-0.1.0/templates/index.html +27 -0
- bellpress-0.1.0/templates/page.html +22 -0
- bellpress-0.1.0/templates/post.html +43 -0
- bellpress-0.1.0/templates/search.html +20 -0
- bellpress-0.1.0/templates/sitemap.xml +9 -0
- bellpress-0.1.0/templates/slides.html +41 -0
- bellpress-0.1.0/templates/tag.html +17 -0
- bellpress-0.1.0/templates/tags.html +14 -0
- bellpress-0.1.0/tests/test_init.py +106 -0
- bellpress-0.1.0/tests/test_release.py +223 -0
bellpress-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: bellpress
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Markdown -> static site generator with an extensible block-syntax registry.
|
|
5
|
+
Requires-Python: >=3.11
|
|
6
|
+
Requires-Dist: jinja2>=3.1
|
|
7
|
+
Requires-Dist: latex2mathml>=3.77
|
|
8
|
+
Requires-Dist: markdown-it-py>=3.0
|
|
9
|
+
Requires-Dist: mdit-py-plugins>=0.4
|
|
10
|
+
Requires-Dist: pygments>=2.16
|
|
11
|
+
Requires-Dist: pyyaml>=6.0
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# BellPress
|
|
2
|
+
|
|
3
|
+
将 Markdown 构建为可部署的静态站点:多语言、文章归档、标签、前端搜索,以及科研风格的 reveal.js 幻灯片。
|
|
4
|
+
|
|
5
|
+
## 安装与使用
|
|
6
|
+
|
|
7
|
+
发布到 PyPI 后:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pip install bellpress
|
|
11
|
+
bellpress init # 当前目录;已有目标文件不会覆盖
|
|
12
|
+
# 或 bellpress init my-site,然后进入 my-site
|
|
13
|
+
bellpress build
|
|
14
|
+
bellpress serve --port 8000
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
源码安装:
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
uv pip install -e .
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
主命令为 `bellpress`,同时保留兼容命令 `sitegen` 和 `site-gen`。输出在 `dist/`,可部署到任意静态托管;搜索和幻灯片不需要后端。默认模板、公开静态资源和语言文件随安装包提供,项目内同名文件优先覆盖。
|
|
24
|
+
|
|
25
|
+
## 创建内容
|
|
26
|
+
|
|
27
|
+
```text
|
|
28
|
+
content/zh/index.md
|
|
29
|
+
content/zh/pages/about.md
|
|
30
|
+
content/zh/posts/2026-11-21-example.md
|
|
31
|
+
content/zh/slides/talk.md
|
|
32
|
+
content/en/posts/2026-11-21-example.md
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
文章示例:
|
|
36
|
+
|
|
37
|
+
```markdown
|
|
38
|
+
---
|
|
39
|
+
title: 我的第一篇文章
|
|
40
|
+
date: 2026-11-21
|
|
41
|
+
tags: [Python, 工具]
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## 一个问题
|
|
45
|
+
|
|
46
|
+
从最小实验开始解释。
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
不同语言下相同 slug 互为译文。日期按 front matter、文件名前缀、mtime 的顺序选择;建议明确填写日期。
|
|
50
|
+
|
|
51
|
+
可选的 `site.toml`:
|
|
52
|
+
|
|
53
|
+
```toml
|
|
54
|
+
title = "牧牛的铃铛"
|
|
55
|
+
default_lang = "zh"
|
|
56
|
+
|
|
57
|
+
[fonts]
|
|
58
|
+
body = "Seaford"
|
|
59
|
+
body_fallback = ["Songti SC", "SimSun", "serif"]
|
|
60
|
+
code = "Courier"
|
|
61
|
+
code_fallback = ["monospace"]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
字体是项目配置,不是硬编码。Seaford 等专有字体**不随 PyPI 包发布**;项目自行配置 Web 字体时需确认嵌入与分发授权。
|
|
65
|
+
|
|
66
|
+
## 关于页头像
|
|
67
|
+
|
|
68
|
+
在 `content/zh/pages/about.md` 的 front matter 中配置:
|
|
69
|
+
|
|
70
|
+
```yaml
|
|
71
|
+
title: 关于
|
|
72
|
+
author: 胡畅
|
|
73
|
+
avatar: avatar.jpg
|
|
74
|
+
institute: 单位名称
|
|
75
|
+
bio: 关注 CPython、虚拟机与工具链
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
把头像放在这份 Markdown 旁边。模板显示圆形头像、姓名、单位和简介;不配置 `avatar` 就不显示头像区。`sitegen init` 提供几何图形占位,可自行换成真实头像。
|
|
79
|
+
|
|
80
|
+
## 给 AI 安装语法 skill
|
|
81
|
+
|
|
82
|
+
```sh
|
|
83
|
+
sitegen install-skill
|
|
84
|
+
# 默认:.agents/skills/sitegen-markdown/SKILL.md
|
|
85
|
+
|
|
86
|
+
sitegen install-skill --target .zettcode/skills
|
|
87
|
+
sitegen install-skill --target ~/.claude/skills
|
|
88
|
+
# 显式覆盖已有不同版本:
|
|
89
|
+
sitegen install-skill --force
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`--target` 是 skills 的父目录。相同内容可重复安装;不同内容默认不覆盖。安装不要求项目存在 `site.toml`。支持 skill 的 AI 工具可读取该文件学习本站语法,不会自动修改其全局设置。
|
|
93
|
+
|
|
94
|
+
## Markdown 扩展
|
|
95
|
+
|
|
96
|
+
- 标准标题、列表、引用、表格、删除线、脚注。
|
|
97
|
+
- `$...$` / `$$...$$`:构建时转换为 MathML。
|
|
98
|
+
- `algo`:LaTeX algorithmic 风格的算法块。
|
|
99
|
+
- `diagram`:节点、箭头、标签组成的矢量图。
|
|
100
|
+
- `svg`:内联 SVG;`figures`:带 (a)/(b) 小注的副图。
|
|
101
|
+
- `{{fig:name}}`、`{{alg:name}}`:带编号的交叉引用。
|
|
102
|
+
- GitHub 提示块及 `::: note/tip/important/warning/caution`。
|
|
103
|
+
- `:::: cols ratio=2:1` 与 `::: col`:双栏或多栏图文。
|
|
104
|
+
- 代码高亮、文件名标题与复制按钮。
|
|
105
|
+
- `slides`:嵌入 Markdown 或 PDF 幻灯片。
|
|
106
|
+
|
|
107
|
+
完整细节以安装后的 `sitegen-markdown/SKILL.md` 为准。Mermaid 当前只有源码占位,不包含渲染器。
|
|
108
|
+
|
|
109
|
+
## 幻灯片
|
|
110
|
+
|
|
111
|
+
`content/zh/slides/talk.md`:
|
|
112
|
+
|
|
113
|
+
```markdown
|
|
114
|
+
---
|
|
115
|
+
title: 从名字到对象
|
|
116
|
+
subtitle: Python 技术分享
|
|
117
|
+
author: 胡畅
|
|
118
|
+
institute: 单位名称
|
|
119
|
+
date: 2026-11-21
|
|
120
|
+
logo: logo.svg
|
|
121
|
+
cover_images: [topic.jpg, portrait.jpg]
|
|
122
|
+
cover_layout: right
|
|
123
|
+
cover_align: left
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## 第一章
|
|
127
|
+
|
|
128
|
+
这一页只讲一个问题。
|
|
129
|
+
|
|
130
|
+
--
|
|
131
|
+
|
|
132
|
+
## 这一章的细节
|
|
133
|
+
|
|
134
|
+
向下翻页。
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## 第二章
|
|
139
|
+
|
|
140
|
+
向右切换章节。
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
封面图片区可选 `left/right/top/bottom`,最多三张图片。`Note:` 独占一行后是讲者备注。项目图片按源文件相对路径引用。
|
|
144
|
+
|
|
145
|
+
嵌入文章:
|
|
146
|
+
|
|
147
|
+
````markdown
|
|
148
|
+
```slides src=../slides/talk.md title="Python 演示"
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
```slides src=../slides/talk.pdf title="PDF 演示"
|
|
152
|
+
```
|
|
153
|
+
````
|
|
154
|
+
|
|
155
|
+
PDF 在构建时转为逐页 SVG/图片并提取文字层,需要 Poppler 的 `pdftocairo`、`pdfinfo`,选字还需要 `pdftotext`:
|
|
156
|
+
|
|
157
|
+
```sh
|
|
158
|
+
brew install poppler
|
|
159
|
+
# Debian/Ubuntu: sudo apt install poppler-utils
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
源文件不存在时构建报错。PDF 的已有位图不会转成矢量图,扫描件没有文本时不能选字。部署只需生成结果,不要求访客安装 Poppler。
|
|
163
|
+
|
|
164
|
+
## 扩展与验证
|
|
165
|
+
|
|
166
|
+
在 `sitegen/blocks/` 注册 `@block("name")` 可扩展围栏语法。发布包只包含生成器、公开模板/资源、技能及测试,不含本站文章、私人 PDF 或专有字体。
|
|
167
|
+
|
|
168
|
+
```sh
|
|
169
|
+
python -m unittest discover -s tests
|
|
170
|
+
uv build
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
GitHub 仓库:`Chang-LeHung/site-gen`(私有)。PyPI 安装包会公开;两者不是同一个可见性设置。
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[ui]
|
|
2
|
+
skip = "Skip to content"
|
|
3
|
+
home = "Home"
|
|
4
|
+
archive = "Archive"
|
|
5
|
+
tags = "Tags"
|
|
6
|
+
about = "About"
|
|
7
|
+
rss = "RSS"
|
|
8
|
+
recent = "Recent"
|
|
9
|
+
all_posts = "All {n} posts"
|
|
10
|
+
browse_tags = "Browse by tag"
|
|
11
|
+
toc = "Contents"
|
|
12
|
+
min_read = "{n} min read"
|
|
13
|
+
newer = "Newer"
|
|
14
|
+
older = "Older"
|
|
15
|
+
tag_page = "Tag: {name}"
|
|
16
|
+
n_posts = "{n} posts"
|
|
17
|
+
count = "Posts"
|
|
18
|
+
all_tags = "All tags"
|
|
19
|
+
not_found = "This page does not exist."
|
|
20
|
+
back_home = "Back to home"
|
|
21
|
+
see_archive = "See the archive"
|
|
22
|
+
footer = "Written in Markdown, built to static HTML"
|
|
23
|
+
language = "Language"
|
|
24
|
+
slides = "Slides"
|
|
25
|
+
present = "Present"
|
|
26
|
+
back = "Back"
|
|
27
|
+
search = "Search"
|
|
28
|
+
search_placeholder = "Words from a title, tag or body"
|
|
29
|
+
search_hint = "Type to search; results appear as you type."
|
|
30
|
+
search_empty = "Nothing matched."
|
|
31
|
+
search_count = "{n} results"
|
|
32
|
+
|
|
33
|
+
[labels]
|
|
34
|
+
figure = "Figure"
|
|
35
|
+
algorithm = "Algorithm"
|
|
36
|
+
table = "Table"
|
|
37
|
+
equation = "Equation"
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[ui]
|
|
2
|
+
skip = "跳到正文"
|
|
3
|
+
home = "首页"
|
|
4
|
+
archive = "归档"
|
|
5
|
+
tags = "标签"
|
|
6
|
+
about = "关于"
|
|
7
|
+
rss = "RSS"
|
|
8
|
+
recent = "最近更新"
|
|
9
|
+
all_posts = "全部 {n} 篇文章"
|
|
10
|
+
browse_tags = "按标签浏览"
|
|
11
|
+
toc = "目录"
|
|
12
|
+
min_read = "{n} 分钟"
|
|
13
|
+
newer = "较新"
|
|
14
|
+
older = "较旧"
|
|
15
|
+
tag_page = "标签:{name}"
|
|
16
|
+
n_posts = "{n} 篇"
|
|
17
|
+
count = "篇数"
|
|
18
|
+
all_tags = "全部标签"
|
|
19
|
+
not_found = "没有这个页面。"
|
|
20
|
+
back_home = "回到首页"
|
|
21
|
+
see_archive = "看归档"
|
|
22
|
+
footer = "用 Markdown 写成,构建为静态页面"
|
|
23
|
+
language = "语言"
|
|
24
|
+
slides = "幻灯片"
|
|
25
|
+
present = "演示"
|
|
26
|
+
back = "返回"
|
|
27
|
+
search = "搜索"
|
|
28
|
+
search_placeholder = "标题、标签或正文里的词"
|
|
29
|
+
search_hint = "输入关键词,结果即时出现。"
|
|
30
|
+
search_empty = "没有匹配的内容。"
|
|
31
|
+
search_count = "{n} 条结果"
|
|
32
|
+
|
|
33
|
+
[labels]
|
|
34
|
+
figure = "图"
|
|
35
|
+
algorithm = "算法"
|
|
36
|
+
table = "表"
|
|
37
|
+
equation = "式"
|
|
38
|
+
note = "注意"
|
|
39
|
+
tip = "提示"
|
|
40
|
+
important = "重要"
|
|
41
|
+
warning = "警告"
|
|
42
|
+
caution = "小心"
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "bellpress"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Markdown -> static site generator with an extensible block-syntax registry."
|
|
5
|
+
requires-python = ">=3.11"
|
|
6
|
+
dependencies = [
|
|
7
|
+
"markdown-it-py>=3.0",
|
|
8
|
+
"mdit-py-plugins>=0.4",
|
|
9
|
+
"pyyaml>=6.0",
|
|
10
|
+
"jinja2>=3.1",
|
|
11
|
+
"pygments>=2.16",
|
|
12
|
+
"latex2mathml>=3.77",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.scripts]
|
|
16
|
+
bellpress = "sitegen.cli:main"
|
|
17
|
+
sitegen = "sitegen.cli:main"
|
|
18
|
+
site-gen = "sitegen.cli:main"
|
|
19
|
+
|
|
20
|
+
[build-system]
|
|
21
|
+
requires = ["hatchling"]
|
|
22
|
+
build-backend = "hatchling.build"
|
|
23
|
+
|
|
24
|
+
[tool.hatch.build]
|
|
25
|
+
exclude = ["**/__pycache__", "**/*.pyc", "/static/fonts", "/content", "**/*.pdf", "**/*.PDF"]
|
|
26
|
+
|
|
27
|
+
[tool.hatch.build.targets.wheel]
|
|
28
|
+
packages = ["sitegen"]
|
|
29
|
+
|
|
30
|
+
[tool.hatch.build.targets.wheel.force-include]
|
|
31
|
+
"templates" = "sitegen/resources/templates"
|
|
32
|
+
# Enumerate public assets: force-include bypasses normal exclude rules.
|
|
33
|
+
"static/tokens.css" = "sitegen/resources/static/tokens.css"
|
|
34
|
+
"static/style.css" = "sitegen/resources/static/style.css"
|
|
35
|
+
"static/main.js" = "sitegen/resources/static/main.js"
|
|
36
|
+
"static/search.js" = "sitegen/resources/static/search.js"
|
|
37
|
+
"static/reveal" = "sitegen/resources/static/reveal"
|
|
38
|
+
"static/math" = "sitegen/resources/static/math"
|
|
39
|
+
"i18n" = "sitegen/resources/i18n"
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.sdist]
|
|
42
|
+
include = [
|
|
43
|
+
# Hatch also includes this VCS metadata file automatically.
|
|
44
|
+
"/.gitignore",
|
|
45
|
+
"/pyproject.toml",
|
|
46
|
+
"/README.md",
|
|
47
|
+
"/sitegen",
|
|
48
|
+
"/templates",
|
|
49
|
+
"/static/tokens.css",
|
|
50
|
+
"/static/style.css",
|
|
51
|
+
"/static/main.js",
|
|
52
|
+
"/static/search.js",
|
|
53
|
+
"/static/reveal",
|
|
54
|
+
"/static/math",
|
|
55
|
+
"/i18n",
|
|
56
|
+
"/tests",
|
|
57
|
+
]
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"""Fenced-block extension point.
|
|
2
|
+
|
|
3
|
+
Every ```-fence whose info string names a registered language is handed to the
|
|
4
|
+
corresponding renderer instead of the default highlighter. That single seam is
|
|
5
|
+
all you need to teach the generator a new syntax:
|
|
6
|
+
|
|
7
|
+
from sitegen.blocks import block
|
|
8
|
+
|
|
9
|
+
@block("mermaid")
|
|
10
|
+
def render_mermaid(code, ctx):
|
|
11
|
+
return f'<pre class="mermaid">{escape(code)}</pre>'
|
|
12
|
+
|
|
13
|
+
Registration happens on import, so ``sitegen.blocks`` imports every module
|
|
14
|
+
listed at the bottom of this file.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import re
|
|
20
|
+
from dataclasses import dataclass, field
|
|
21
|
+
from typing import Any, Callable
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"BlockContext",
|
|
25
|
+
"Numbered",
|
|
26
|
+
"REGISTRY",
|
|
27
|
+
"block",
|
|
28
|
+
"parse_info",
|
|
29
|
+
"render",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True, slots=True)
|
|
34
|
+
class Numbered:
|
|
35
|
+
"""What a numbered block needs: its counter value and its anchor id."""
|
|
36
|
+
|
|
37
|
+
number: int
|
|
38
|
+
anchor: str
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(slots=True)
|
|
42
|
+
class BlockContext:
|
|
43
|
+
"""Everything a renderer needs beyond the fence body itself."""
|
|
44
|
+
|
|
45
|
+
language: str = ""
|
|
46
|
+
attrs: dict[str, str] = field(default_factory=dict)
|
|
47
|
+
doc: Any = None
|
|
48
|
+
"""The document being rendered, or ``None``."""
|
|
49
|
+
counters: dict[str, int] = field(default_factory=dict)
|
|
50
|
+
"""Counters shared by every block of one document (resets per document)."""
|
|
51
|
+
refs: Any = None
|
|
52
|
+
"""The build's :class:`sitegen.refs.RefRegistry`, or ``None``."""
|
|
53
|
+
doc_slug: str = ""
|
|
54
|
+
lang: str = ""
|
|
55
|
+
labels: dict[str, str] = field(default_factory=dict)
|
|
56
|
+
"""Localised captions: ``{"figure": "图", "algorithm": "算法"}``."""
|
|
57
|
+
|
|
58
|
+
# -- attributes -----------------------------------------------------
|
|
59
|
+
def attr(self, name: str, default: str = "") -> str:
|
|
60
|
+
return self.attrs.get(name, default)
|
|
61
|
+
|
|
62
|
+
def label(self, kind: str) -> str:
|
|
63
|
+
"""Caption word for a numbered block, in the page's language."""
|
|
64
|
+
return self.labels.get(kind) or {
|
|
65
|
+
"figure": "Figure",
|
|
66
|
+
"algorithm": "Algorithm",
|
|
67
|
+
"table": "Table",
|
|
68
|
+
}.get(kind, kind.title())
|
|
69
|
+
|
|
70
|
+
def flag(self, name: str) -> bool:
|
|
71
|
+
"""A valueless attribute (``line-numbers``) or ``name=true``."""
|
|
72
|
+
return self.attrs.get(name, "false").lower() not in ("false", "0", "no", "")
|
|
73
|
+
|
|
74
|
+
# -- numbering ------------------------------------------------------
|
|
75
|
+
def next_number(self, key: str) -> int:
|
|
76
|
+
self.counters[key] = self.counters.get(key, 0) + 1
|
|
77
|
+
return self.counters[key]
|
|
78
|
+
|
|
79
|
+
def number(self, kind: str, title: str = "") -> Numbered:
|
|
80
|
+
"""Number a figure/algorithm and register it for `{{label}}` lookups."""
|
|
81
|
+
number = self.next_number(kind)
|
|
82
|
+
key = self.attr("label").strip()
|
|
83
|
+
anchor = key.replace(":", "-") if key else f"{kind}-{number}"
|
|
84
|
+
if key and self.refs is not None:
|
|
85
|
+
from ..refs import RefEntry
|
|
86
|
+
|
|
87
|
+
self.refs.add(
|
|
88
|
+
RefEntry(
|
|
89
|
+
key=key,
|
|
90
|
+
kind=kind,
|
|
91
|
+
number=number,
|
|
92
|
+
doc_slug=self.doc_slug,
|
|
93
|
+
anchor=anchor,
|
|
94
|
+
title=title,
|
|
95
|
+
doc_url=getattr(self.doc, "url", ""),
|
|
96
|
+
lang=self.lang,
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
return Numbered(number, anchor)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
Renderer = Callable[[str, BlockContext], str]
|
|
103
|
+
|
|
104
|
+
REGISTRY: dict[str, Renderer] = {}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def block(language: str) -> Callable[[Renderer], Renderer]:
|
|
108
|
+
"""Register ``fn`` as the renderer for ```language`` fences."""
|
|
109
|
+
|
|
110
|
+
def register(fn: Renderer) -> Renderer:
|
|
111
|
+
REGISTRY[language] = fn
|
|
112
|
+
return fn
|
|
113
|
+
|
|
114
|
+
return register
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
_PAIR = re.compile(r"""([\w-]+)=(?:"([^"]*)"|'([^']*)'|(\S+))""")
|
|
118
|
+
_BARE = re.compile(r"([\w-]+)")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def parse_info(info: str) -> tuple[str, dict[str, str]]:
|
|
122
|
+
"""Split a fence info string into ``(language, attributes)``.
|
|
123
|
+
|
|
124
|
+
Accepts ``algo title="二分查找"``, ``{algo}`` and ``{.algo}``; both
|
|
125
|
+
``k="v with space"`` and bare flags (``line-numbers``) are understood.
|
|
126
|
+
"""
|
|
127
|
+
info = info.strip()
|
|
128
|
+
if not info:
|
|
129
|
+
return "", {}
|
|
130
|
+
head, _, rest = info.partition(" ")
|
|
131
|
+
language = head.strip().strip("{}").lstrip(".").strip("}")
|
|
132
|
+
attrs: dict[str, str] = {}
|
|
133
|
+
for match in _PAIR.finditer(rest):
|
|
134
|
+
value = match.group(2) or match.group(3) or match.group(4) or ""
|
|
135
|
+
attrs[match.group(1)] = value
|
|
136
|
+
for match in _BARE.finditer(rest):
|
|
137
|
+
attrs.setdefault(match.group(1), "")
|
|
138
|
+
return language, attrs
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def render(code: str, info: str, ctx: BlockContext) -> str | None:
|
|
142
|
+
"""Render ``code`` with the registered renderer for ``info``, or ``None``."""
|
|
143
|
+
language, attrs = parse_info(info)
|
|
144
|
+
fn = REGISTRY.get(language)
|
|
145
|
+
if fn is None:
|
|
146
|
+
return None
|
|
147
|
+
ctx.language, ctx.attrs = language, attrs
|
|
148
|
+
return fn(code, ctx)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
# Importing these populates REGISTRY. Add your own module here.
|
|
152
|
+
from . import algo, callout, deck, diagram, figure, layout, subfig # noqa: E402,F401
|