mddoco 2.2.0__tar.gz → 2.2.2__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.
- {mddoco-2.2.0/src/mddoco.egg-info → mddoco-2.2.2}/PKG-INFO +1 -1
- {mddoco-2.2.0 → mddoco-2.2.2}/pyproject.toml +1 -1
- mddoco-2.2.2/src/mddoco/__init__.py +1 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/cli.py +2 -2
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/converter.py +11 -8
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/renderer.py +5 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/_site_base.html +2 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/academic.html +5 -3
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/dark.html +4 -2
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/default.html +4 -2
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/paged-professional.html +3 -7
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/professional.html +5 -10
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/vanilla.html +2 -0
- mddoco-2.2.2/src/mddoco/toc.py +63 -0
- {mddoco-2.2.0 → mddoco-2.2.2/src/mddoco.egg-info}/PKG-INFO +1 -1
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco.egg-info/SOURCES.txt +2 -1
- {mddoco-2.2.0 → mddoco-2.2.2}/tests/test_preprocessor.py +1 -1
- mddoco-2.2.2/tests/test_toc.py +65 -0
- mddoco-2.2.0/src/mddoco/__init__.py +0 -1
- mddoco-2.2.0/src/mddoco/toc.py +0 -15
- {mddoco-2.2.0 → mddoco-2.2.2}/LICENSE +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/README.md +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/setup.cfg +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/graph.py +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/mermaid.py +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/pdf.py +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/preprocessor.py +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/scanner.py +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/academic-wide.html +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/dark-wide.html +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/default-wide.html +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/professional-wide.html +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/site-dark.html +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/site-professional.html +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/site-terminal.html +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/themes/site.html +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco/writer.py +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco.egg-info/dependency_links.txt +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco.egg-info/entry_points.txt +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco.egg-info/requires.txt +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/src/mddoco.egg-info/top_level.txt +0 -0
- {mddoco-2.2.0 → mddoco-2.2.2}/tests/test_themes.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "2.2.2"
|
|
@@ -8,7 +8,7 @@ from mddoco.pdf import html_to_pdf
|
|
|
8
8
|
from mddoco.preprocessor import load_context
|
|
9
9
|
from mddoco.renderer import check_theme_format, render_html
|
|
10
10
|
from mddoco.scanner import find_markdown_files
|
|
11
|
-
from mddoco.toc import
|
|
11
|
+
from mddoco.toc import build_toc
|
|
12
12
|
from mddoco.writer import write_output
|
|
13
13
|
|
|
14
14
|
|
|
@@ -82,7 +82,7 @@ def main(
|
|
|
82
82
|
except Exception as exc:
|
|
83
83
|
raise click.ClickException(str(exc)) from exc
|
|
84
84
|
sections = [(p, html) for p, html, _ in raw]
|
|
85
|
-
toc_html =
|
|
85
|
+
toc_html = build_toc([e for _, _, entries in raw for e in entries]) if toc else None
|
|
86
86
|
has_mermaid = any('<div class="mermaid">' in html for _, html in sections)
|
|
87
87
|
has_code = any("<pre><code" in html for _, html in sections)
|
|
88
88
|
|
|
@@ -5,6 +5,7 @@ import markdown
|
|
|
5
5
|
from mddoco.graph import process_graph
|
|
6
6
|
from mddoco.mermaid import process_mermaid
|
|
7
7
|
from mddoco.preprocessor import render_j2
|
|
8
|
+
from mddoco.toc import TocEntry, flatten_tokens
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
def convert_file(
|
|
@@ -12,12 +13,12 @@ def convert_file(
|
|
|
12
13
|
toc: bool = False,
|
|
13
14
|
toc_depth: int = 3,
|
|
14
15
|
context: dict | None = None,
|
|
15
|
-
) -> tuple[str,
|
|
16
|
+
) -> tuple[str, list[TocEntry]]:
|
|
16
17
|
"""Convert a markdown (or .md.j2) file to an HTML fragment.
|
|
17
18
|
|
|
18
19
|
.md.j2 files are rendered with Jinja2 first using the supplied context,
|
|
19
20
|
then the resulting Markdown is converted as normal.
|
|
20
|
-
Returns (html_body,
|
|
21
|
+
Returns (html_body, toc_entries). toc_entries is empty when toc=False.
|
|
21
22
|
"""
|
|
22
23
|
if path.suffix.lower() == ".j2":
|
|
23
24
|
text = render_j2(path, context or {})
|
|
@@ -36,8 +37,8 @@ def convert_file(
|
|
|
36
37
|
html, _ = process_mermaid(html)
|
|
37
38
|
html, _ = process_graph(html)
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
return html,
|
|
40
|
+
toc_entries = flatten_tokens(md.toc_tokens) if toc else []
|
|
41
|
+
return html, toc_entries
|
|
41
42
|
|
|
42
43
|
|
|
43
44
|
def convert_files(
|
|
@@ -45,10 +46,12 @@ def convert_files(
|
|
|
45
46
|
toc: bool = False,
|
|
46
47
|
toc_depth: int = 3,
|
|
47
48
|
context: dict | None = None,
|
|
48
|
-
) -> list[tuple[Path, str,
|
|
49
|
-
"""Convert markdown files, returning (path, html_body,
|
|
49
|
+
) -> list[tuple[Path, str, list[TocEntry]]]:
|
|
50
|
+
"""Convert markdown files, returning (path, html_body, toc_entries) tuples."""
|
|
50
51
|
results = []
|
|
51
52
|
for p in paths:
|
|
52
|
-
html,
|
|
53
|
-
|
|
53
|
+
html, toc_entries = convert_file(
|
|
54
|
+
p, toc=toc, toc_depth=toc_depth, context=context
|
|
55
|
+
)
|
|
56
|
+
results.append((p, html, toc_entries))
|
|
54
57
|
return results
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
from datetime import datetime
|
|
1
2
|
from importlib.resources import files
|
|
2
3
|
from pathlib import Path
|
|
3
4
|
|
|
4
5
|
from jinja2 import Environment, FileSystemLoader, Template, TemplateNotFound
|
|
5
6
|
|
|
7
|
+
from mddoco import __version__
|
|
8
|
+
|
|
6
9
|
|
|
7
10
|
def load_theme(theme: str) -> Template:
|
|
8
11
|
"""Load a theme by built-in name or by path to an HTML template file."""
|
|
@@ -54,4 +57,6 @@ def render_html(
|
|
|
54
57
|
sections=sections,
|
|
55
58
|
has_mermaid=has_mermaid,
|
|
56
59
|
has_code=has_code,
|
|
60
|
+
generator=f"mddoco {__version__}",
|
|
61
|
+
generated=datetime.now().astimezone().isoformat(timespec="seconds"),
|
|
57
62
|
)
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
in the theme_styles block. Screen-only, so PDF output is refused. -#}
|
|
9
9
|
<meta charset="UTF-8">
|
|
10
10
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
11
|
+
{% if generator %}<meta name="generator" content="{{ generator }}">{% endif %}
|
|
12
|
+
{% if generated %}<meta name="date" content="{{ generated }}">{% endif %}
|
|
11
13
|
<title>{{ title or "Documentation" }}</title>
|
|
12
14
|
<style>
|
|
13
15
|
:root {
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
{% if generator %}<meta name="generator" content="{{ generator }}">{% endif %}
|
|
7
|
+
{% if generated %}<meta name="date" content="{{ generated }}">{% endif %}
|
|
6
8
|
<title>{% if title %}{{ title }}{% else %}Document{% endif %}</title>
|
|
7
9
|
<style>
|
|
8
10
|
*, *::before, *::after { box-sizing: border-box; }
|
|
@@ -123,9 +125,8 @@
|
|
|
123
125
|
.toc-block {
|
|
124
126
|
border: 1px solid #ccc;
|
|
125
127
|
padding: 1rem 1.5rem;
|
|
126
|
-
margin: 1.5rem
|
|
127
|
-
display:
|
|
128
|
-
min-width: 260px;
|
|
128
|
+
margin: 1.5rem 0 2.5rem;
|
|
129
|
+
display: block;
|
|
129
130
|
font-size: 0.95em;
|
|
130
131
|
}
|
|
131
132
|
.toc-block ul {
|
|
@@ -135,6 +136,7 @@
|
|
|
135
136
|
}
|
|
136
137
|
.toc-block ul ul { padding-left: 1em; }
|
|
137
138
|
.toc-block li { margin: 0.2em 0; }
|
|
139
|
+
.toc-block > ul > li > a { font-weight: 700; }
|
|
138
140
|
.toc-block a { color: #111; font-size: 0.9em; text-decoration: none; }
|
|
139
141
|
.toc-block a:hover { text-decoration: underline; }
|
|
140
142
|
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
{% if generator %}<meta name="generator" content="{{ generator }}">{% endif %}
|
|
7
|
+
{% if generated %}<meta name="date" content="{{ generated }}">{% endif %}
|
|
6
8
|
<title>{% if title %}{{ title }}{% else %}Document{% endif %}</title>
|
|
7
9
|
<style>
|
|
8
10
|
*, *::before, *::after { box-sizing: border-box; }
|
|
@@ -127,8 +129,7 @@
|
|
|
127
129
|
border-radius: 6px;
|
|
128
130
|
padding: 1rem 1.5rem;
|
|
129
131
|
margin: 1.5rem 0 2.5rem;
|
|
130
|
-
display:
|
|
131
|
-
min-width: 260px;
|
|
132
|
+
display: block;
|
|
132
133
|
}
|
|
133
134
|
.toc-block ul {
|
|
134
135
|
margin: 0;
|
|
@@ -137,6 +138,7 @@
|
|
|
137
138
|
}
|
|
138
139
|
.toc-block ul ul { padding-left: 1em; }
|
|
139
140
|
.toc-block li { margin: 0.2em 0; }
|
|
141
|
+
.toc-block > ul > li > a { font-weight: 700; }
|
|
140
142
|
.toc-block a { color: var(--accent); font-size: 0.9em; }
|
|
141
143
|
|
|
142
144
|
/* Mermaid diagrams */
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
{% if generator %}<meta name="generator" content="{{ generator }}">{% endif %}
|
|
7
|
+
{% if generated %}<meta name="date" content="{{ generated }}">{% endif %}
|
|
6
8
|
<title>{% if title %}{{ title }}{% else %}Document{% endif %}</title>
|
|
7
9
|
<style>
|
|
8
10
|
*, *::before, *::after { box-sizing: border-box; }
|
|
@@ -108,8 +110,7 @@
|
|
|
108
110
|
border-radius: 6px;
|
|
109
111
|
padding: 1rem 1.5rem;
|
|
110
112
|
margin: 1.5rem 0 2.5rem;
|
|
111
|
-
display:
|
|
112
|
-
min-width: 260px;
|
|
113
|
+
display: block;
|
|
113
114
|
}
|
|
114
115
|
.toc-block ul {
|
|
115
116
|
margin: 0;
|
|
@@ -118,6 +119,7 @@
|
|
|
118
119
|
}
|
|
119
120
|
.toc-block ul ul { padding-left: 1em; }
|
|
120
121
|
.toc-block li { margin: 0.2em 0; }
|
|
122
|
+
.toc-block > ul > li > a { font-weight: 700; }
|
|
121
123
|
.toc-block a { color: #0366d6; font-size: 0.9em; }
|
|
122
124
|
|
|
123
125
|
/* Mermaid diagrams */
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
{% if generator %}<meta name="generator" content="{{ generator }}">{% endif %}
|
|
7
|
+
{% if generated %}<meta name="date" content="{{ generated }}">{% endif %}
|
|
6
8
|
<title>{% if title %}{{ title }}{% else %}Document{% endif %}</title>
|
|
7
9
|
<style>
|
|
8
10
|
/* ── Paged Media: headers, footers, page size ───────────────── */
|
|
@@ -78,11 +80,6 @@
|
|
|
78
80
|
/* Captured into the right-side running header on subsequent pages */
|
|
79
81
|
string-set: doc-title content(text);
|
|
80
82
|
}
|
|
81
|
-
.doc-title-page .subtitle {
|
|
82
|
-
font-size: 10pt;
|
|
83
|
-
color: var(--muted);
|
|
84
|
-
margin: 0;
|
|
85
|
-
}
|
|
86
83
|
|
|
87
84
|
/* ── Headings ───────────────────────────────────────────────── */
|
|
88
85
|
/* Section h1s update the left-side running header */
|
|
@@ -201,7 +198,6 @@
|
|
|
201
198
|
padding: 0.9rem 1.3rem;
|
|
202
199
|
margin: 1.5rem 0 2.5rem;
|
|
203
200
|
display: block;
|
|
204
|
-
max-width: 340px;
|
|
205
201
|
}
|
|
206
202
|
|
|
207
203
|
/* Ensure page boxes render with a white background on screen */
|
|
@@ -211,6 +207,7 @@
|
|
|
211
207
|
.toc-block ul { margin: 0; padding-left: 1.1em; list-style: none; }
|
|
212
208
|
.toc-block ul ul { padding-left: 1em; }
|
|
213
209
|
.toc-block li { margin: 0.2em 0; }
|
|
210
|
+
.toc-block > ul > li > a { font-weight: 700; }
|
|
214
211
|
.toc-block a { color: var(--primary); font-size: 9pt; }
|
|
215
212
|
.toc-block a:hover { color: var(--accent); }
|
|
216
213
|
|
|
@@ -258,7 +255,6 @@
|
|
|
258
255
|
{% if title %}
|
|
259
256
|
<div class="doc-title-page">
|
|
260
257
|
<h1>{{ title }}</h1>
|
|
261
|
-
<p class="subtitle">Generated by mddoco</p>
|
|
262
258
|
</div>
|
|
263
259
|
{% endif %}
|
|
264
260
|
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
{% if generator %}<meta name="generator" content="{{ generator }}">{% endif %}
|
|
7
|
+
{% if generated %}<meta name="date" content="{{ generated }}">{% endif %}
|
|
6
8
|
<title>{% if title %}{{ title }}{% else %}Document{% endif %}</title>
|
|
7
9
|
<style>
|
|
8
10
|
*, *::before, *::after { box-sizing: border-box; }
|
|
@@ -51,12 +53,6 @@
|
|
|
51
53
|
color: #fff;
|
|
52
54
|
border: none;
|
|
53
55
|
}
|
|
54
|
-
.doc-header .doc-subtitle {
|
|
55
|
-
margin: 0.4rem 0 0;
|
|
56
|
-
font-size: 0.9rem;
|
|
57
|
-
opacity: 0.7;
|
|
58
|
-
letter-spacing: 0.03em;
|
|
59
|
-
}
|
|
60
56
|
|
|
61
57
|
.content {
|
|
62
58
|
padding: 2rem 3rem 3rem;
|
|
@@ -167,8 +163,7 @@
|
|
|
167
163
|
border-radius: 0 4px 4px 0;
|
|
168
164
|
padding: 1rem 1.5rem;
|
|
169
165
|
margin: 1.5rem 0 2.5rem;
|
|
170
|
-
display:
|
|
171
|
-
min-width: 280px;
|
|
166
|
+
display: block;
|
|
172
167
|
}
|
|
173
168
|
.toc-block ul {
|
|
174
169
|
margin: 0;
|
|
@@ -177,6 +172,7 @@
|
|
|
177
172
|
}
|
|
178
173
|
.toc-block ul ul { padding-left: 1em; }
|
|
179
174
|
.toc-block li { margin: 0.2em 0; }
|
|
175
|
+
.toc-block > ul > li > a { font-weight: 700; }
|
|
180
176
|
.toc-block a { color: var(--primary); font-size: 0.9em; }
|
|
181
177
|
.toc-block a:hover { color: var(--accent); }
|
|
182
178
|
|
|
@@ -193,7 +189,7 @@
|
|
|
193
189
|
{% block variant_styles %}{% endblock %}
|
|
194
190
|
</style>
|
|
195
191
|
{% if has_code %}
|
|
196
|
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@highlightjs/cdn-assets@11.12.0/styles/github.min.css" integrity="sha384-
|
|
192
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@highlightjs/cdn-assets@11.12.0/styles/github-dark.min.css" integrity="sha384-wH75j6z1lH97ZOpMOInqhgKzFkAInZPPSPlZpYKYTOqsaizPvhQZmAtLcPKXpLyH" crossorigin="anonymous">
|
|
197
193
|
<script src="https://cdn.jsdelivr.net/npm/@highlightjs/cdn-assets@11.12.0/highlight.min.js" integrity="sha384-wjfDDhOPPdjtva8vWBhWeVprSpmxisEu5aYT3q1JyACqXpdKpo3PWZTMVq24MBix" crossorigin="anonymous"></script>
|
|
198
194
|
<script>hljs.highlightAll();</script>
|
|
199
195
|
{% endif %}
|
|
@@ -208,7 +204,6 @@
|
|
|
208
204
|
{% if title %}
|
|
209
205
|
<header class="doc-header">
|
|
210
206
|
<h1 class="doc-title">{{ title }}</h1>
|
|
211
|
-
<p class="doc-subtitle">Generated by mddoco</p>
|
|
212
207
|
</header>
|
|
213
208
|
{% endif %}
|
|
214
209
|
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
{% if generator %}<meta name="generator" content="{{ generator }}">{% endif %}
|
|
7
|
+
{% if generated %}<meta name="date" content="{{ generated }}">{% endif %}
|
|
6
8
|
<title>{% if title %}{{ title }}{% else %}Document{% endif %}</title>
|
|
7
9
|
<style>
|
|
8
10
|
table { border-collapse: collapse; }
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@dataclass
|
|
5
|
+
class TocEntry:
|
|
6
|
+
"""One heading in the table of contents, at its absolute heading level."""
|
|
7
|
+
|
|
8
|
+
level: int
|
|
9
|
+
anchor: str
|
|
10
|
+
label: str
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class _Node:
|
|
15
|
+
entry: TocEntry | None
|
|
16
|
+
children: list["_Node"] = field(default_factory=list)
|
|
17
|
+
|
|
18
|
+
def to_html(self) -> str:
|
|
19
|
+
"""Render this node's link (if any) followed by its children as a <ul>."""
|
|
20
|
+
link = (
|
|
21
|
+
f'<a href="#{self.entry.anchor}">{self.entry.label}</a>'
|
|
22
|
+
if self.entry
|
|
23
|
+
else ""
|
|
24
|
+
)
|
|
25
|
+
if not self.children:
|
|
26
|
+
return link
|
|
27
|
+
items = "\n".join(f"<li>{child.to_html()}</li>" for child in self.children)
|
|
28
|
+
return f"{link}<ul>\n{items}\n</ul>"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def flatten_tokens(tokens: list[dict]) -> list[TocEntry]:
|
|
32
|
+
"""Flatten Python-Markdown ``toc_tokens`` into entries in document order.
|
|
33
|
+
|
|
34
|
+
``label`` is the token's ``name``, which Python-Markdown has already
|
|
35
|
+
HTML-escaped, so it is safe to place in markup as-is.
|
|
36
|
+
"""
|
|
37
|
+
entries: list[TocEntry] = []
|
|
38
|
+
for token in tokens:
|
|
39
|
+
entries.append(TocEntry(token["level"], token["id"], token["name"]))
|
|
40
|
+
entries.extend(flatten_tokens(token["children"]))
|
|
41
|
+
return entries
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def build_toc(entries: list[TocEntry]) -> str:
|
|
45
|
+
"""Render entries from all files as one nested <ul>, nested by heading level.
|
|
46
|
+
|
|
47
|
+
Nesting follows the absolute heading level, so an H2 always sits one
|
|
48
|
+
level below the top whatever file it came from. An H2 with no H1 before
|
|
49
|
+
it is placed in an unlabelled top-level item rather than promoted.
|
|
50
|
+
"""
|
|
51
|
+
root = _Node(None)
|
|
52
|
+
stack: list[_Node] = [] # stack[d] is the open node at heading level d + 1
|
|
53
|
+
for entry in entries:
|
|
54
|
+
del stack[entry.level - 1 :]
|
|
55
|
+
while len(stack) < entry.level - 1:
|
|
56
|
+
placeholder = _Node(None)
|
|
57
|
+
(stack[-1] if stack else root).children.append(placeholder)
|
|
58
|
+
stack.append(placeholder)
|
|
59
|
+
node = _Node(entry)
|
|
60
|
+
(stack[-1] if stack else root).children.append(node)
|
|
61
|
+
stack.append(node)
|
|
62
|
+
|
|
63
|
+
return root.to_html()
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from mddoco.converter import convert_file
|
|
4
|
+
from mddoco.toc import TocEntry, build_toc
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _toc_for(tmp_path: Path, *documents: str, toc_depth: int = 3) -> str:
|
|
8
|
+
entries = []
|
|
9
|
+
for i, text in enumerate(documents):
|
|
10
|
+
path = tmp_path / f"{i:02d}.md"
|
|
11
|
+
path.write_text(text, encoding="utf-8")
|
|
12
|
+
_, file_entries = convert_file(path, toc=True, toc_depth=toc_depth)
|
|
13
|
+
entries.extend(file_entries)
|
|
14
|
+
return build_toc(entries)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_h1_with_subheadings_nests_under_it(tmp_path):
|
|
18
|
+
toc = _toc_for(tmp_path, "# One\n\n## Two\n\n### Three\n")
|
|
19
|
+
assert toc == (
|
|
20
|
+
'<ul>\n<li><a href="#one">One</a><ul>\n'
|
|
21
|
+
'<li><a href="#two">Two</a><ul>\n'
|
|
22
|
+
'<li><a href="#three">Three</a></li>\n</ul></li>\n'
|
|
23
|
+
"</ul></li>\n</ul>"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_file_without_h1_nests_its_h2_under_previous_h1(tmp_path):
|
|
28
|
+
toc = _toc_for(tmp_path, "# Intro\n", "## Detail\n")
|
|
29
|
+
assert toc == (
|
|
30
|
+
'<ul>\n<li><a href="#intro">Intro</a><ul>\n'
|
|
31
|
+
'<li><a href="#detail">Detail</a></li>\n'
|
|
32
|
+
"</ul></li>\n</ul>"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_leading_h2_is_not_promoted_to_top_level(tmp_path):
|
|
37
|
+
toc = _toc_for(tmp_path, "## Orphan\n\n# Real\n")
|
|
38
|
+
assert toc == (
|
|
39
|
+
'<ul>\n<li><ul>\n<li><a href="#orphan">Orphan</a></li>\n</ul></li>\n'
|
|
40
|
+
'<li><a href="#real">Real</a></li>\n</ul>'
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_toc_depth_limits_entries(tmp_path):
|
|
45
|
+
toc = _toc_for(tmp_path, "# One\n\n## Two\n\n### Three\n", toc_depth=2)
|
|
46
|
+
assert "#two" in toc
|
|
47
|
+
assert "#three" not in toc
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_heading_text_is_escaped(tmp_path):
|
|
51
|
+
toc = _toc_for(tmp_path, "# Fish & chips < 5\n")
|
|
52
|
+
assert "Fish & chips < 5" in toc
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_skipped_level_gets_unlabelled_parent():
|
|
56
|
+
toc = build_toc([TocEntry(1, "a", "A"), TocEntry(3, "c", "C")])
|
|
57
|
+
assert toc == (
|
|
58
|
+
'<ul>\n<li><a href="#a">A</a><ul>\n'
|
|
59
|
+
'<li><ul>\n<li><a href="#c">C</a></li>\n</ul></li>\n'
|
|
60
|
+
"</ul></li>\n</ul>"
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_no_entries_gives_empty_string():
|
|
65
|
+
assert build_toc([]) == ""
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "2.2.0"
|
mddoco-2.2.0/src/mddoco/toc.py
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import re
|
|
2
|
-
|
|
3
|
-
_INNER_UL = re.compile(r"<ul>(.*)</ul>", re.DOTALL)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def combine_toc(fragments: list[str]) -> str:
|
|
7
|
-
"""Merge per-file TOC HTML fragments into a single <ul> block."""
|
|
8
|
-
items: list[str] = []
|
|
9
|
-
for fragment in fragments:
|
|
10
|
-
m = _INNER_UL.search(fragment)
|
|
11
|
-
if m:
|
|
12
|
-
items.append(m.group(1).strip())
|
|
13
|
-
if not items:
|
|
14
|
-
return ""
|
|
15
|
-
return "<ul>\n" + "\n".join(items) + "\n</ul>"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|