mddoco 2.0.3__tar.gz → 2.2.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.
- {mddoco-2.0.3 → mddoco-2.2.0}/PKG-INFO +56 -3
- mddoco-2.0.3/src/mddoco.egg-info/PKG-INFO → mddoco-2.2.0/README.md +51 -20
- {mddoco-2.0.3 → mddoco-2.2.0}/pyproject.toml +9 -1
- mddoco-2.2.0/src/mddoco/__init__.py +1 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/cli.py +8 -1
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/pdf.py +20 -1
- mddoco-2.2.0/src/mddoco/renderer.py +57 -0
- mddoco-2.2.0/src/mddoco/themes/_site_base.html +382 -0
- mddoco-2.2.0/src/mddoco/themes/academic-wide.html +7 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/themes/academic.html +9 -1
- mddoco-2.2.0/src/mddoco/themes/dark-wide.html +7 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/themes/dark.html +14 -1
- mddoco-2.2.0/src/mddoco/themes/default-wide.html +7 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/themes/default.html +14 -1
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/themes/paged-professional.html +34 -11
- mddoco-2.2.0/src/mddoco/themes/professional-wide.html +14 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/themes/professional.html +15 -1
- mddoco-2.2.0/src/mddoco/themes/site-dark.html +37 -0
- mddoco-2.2.0/src/mddoco/themes/site-professional.html +67 -0
- mddoco-2.2.0/src/mddoco/themes/site-terminal.html +65 -0
- mddoco-2.2.0/src/mddoco/themes/site.html +8 -0
- mddoco-2.2.0/src/mddoco/themes/vanilla.html +40 -0
- mddoco-2.0.3/README.md → mddoco-2.2.0/src/mddoco.egg-info/PKG-INFO +309 -238
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco.egg-info/SOURCES.txt +7 -1
- {mddoco-2.0.3 → mddoco-2.2.0}/tests/test_preprocessor.py +1 -1
- mddoco-2.2.0/tests/test_themes.py +63 -0
- mddoco-2.0.3/src/mddoco/__init__.py +0 -1
- mddoco-2.0.3/src/mddoco/renderer.py +0 -33
- mddoco-2.0.3/src/mddoco/themes/academic-wide.html +0 -185
- mddoco-2.0.3/src/mddoco/themes/dark-wide.html +0 -177
- mddoco-2.0.3/src/mddoco/themes/default-wide.html +0 -158
- mddoco-2.0.3/src/mddoco/themes/professional-wide.html +0 -222
- mddoco-2.0.3/src/mddoco/themes/vanilla.html +0 -33
- {mddoco-2.0.3 → mddoco-2.2.0}/LICENSE +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/setup.cfg +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/converter.py +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/graph.py +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/mermaid.py +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/preprocessor.py +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/scanner.py +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/toc.py +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco/writer.py +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco.egg-info/dependency_links.txt +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco.egg-info/entry_points.txt +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco.egg-info/requires.txt +0 -0
- {mddoco-2.0.3 → mddoco-2.2.0}/src/mddoco.egg-info/top_level.txt +0 -0
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mddoco
|
|
3
|
-
Version: 2.0
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: Markdown to HTML/PDF document converter
|
|
5
|
+
Author-email: Phil Massyn <phil.massyn@icloud.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/massyn/mddoco
|
|
7
|
+
Project-URL: Repository, https://github.com/massyn/mddoco
|
|
8
|
+
Project-URL: Issues, https://github.com/massyn/mddoco/issues
|
|
5
9
|
Requires-Python: >=3.10
|
|
6
10
|
Description-Content-Type: text/markdown
|
|
7
11
|
License-File: LICENSE
|
|
@@ -20,6 +24,8 @@ Dynamic: license-file
|
|
|
20
24
|
|
|
21
25
|
A CLI tool that converts markdown files to a single HTML (or future PDF) document.
|
|
22
26
|
|
|
27
|
+
[](https://github.com/massyn/mddoco/actions/workflows/ci.yml)
|
|
28
|
+
 
|
|
23
29
|
## Installation
|
|
24
30
|
|
|
25
31
|
```bash
|
|
@@ -85,6 +91,25 @@ mddoco ./docs --title "My Project" --toc --output ./out
|
|
|
85
91
|
|
|
86
92
|
All matched markdown files are combined into a single HTML document in the output directory. The filename is derived from the input folder or file name.
|
|
87
93
|
|
|
94
|
+
## Examples
|
|
95
|
+
|
|
96
|
+
The [`examples/`](examples/) directory contains five runnable input folders, each
|
|
97
|
+
covering a different feature:
|
|
98
|
+
|
|
99
|
+
| Folder | Demonstrates |
|
|
100
|
+
|--------|--------------|
|
|
101
|
+
| [`examples/01_basic`](examples/01_basic) | Plain Markdown, file ordering, underscore-excluded files |
|
|
102
|
+
| [`examples/02_rich_content`](examples/02_rich_content) | Mermaid diagrams, syntax highlighting, `graph` charts |
|
|
103
|
+
| [`examples/03_themed_report`](examples/03_themed_report) | Themes and a table of contents |
|
|
104
|
+
| [`examples/04_jinja_data`](examples/04_jinja_data) | Jinja2 templates with JSON and CSV data files |
|
|
105
|
+
| [`examples/05_security_findings`](examples/05_security_findings) | CSV data grouped in a Jinja2 template and fed into a `graph` chart |
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
mddoco examples/03_themed_report --theme professional --title "Q3 Platform Review" --toc --output ./out
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
See [`examples/README.md`](examples/README.md) for the full list of commands.
|
|
112
|
+
|
|
88
113
|
## Jinja2 templates
|
|
89
114
|
|
|
90
115
|
Files ending in `.md.j2` are Jinja2 templates that produce Markdown. They are sorted alongside regular `.md` files (numeric prefixes such as `01_`, `02_` determine order) and converted through the same pipeline.
|
|
@@ -157,7 +182,7 @@ Use `{% import %}` or `{% from ... import %}` to make macros from another file c
|
|
|
157
182
|
|
|
158
183
|
## Themes
|
|
159
184
|
|
|
160
|
-
Themes are
|
|
185
|
+
Themes are Jinja2 HTML files with embedded CSS. Pass a theme name with `--theme NAME`. The `-wide` and `site-*` themes extend another theme (`default.html`, `_site_base.html` and so on), so to customise one of those, copy the theme it extends as well.
|
|
161
186
|
|
|
162
187
|
| Theme | Description |
|
|
163
188
|
|---|---|
|
|
@@ -171,14 +196,42 @@ Themes are self-contained Jinja2 HTML files with embedded CSS. Pass a theme name
|
|
|
171
196
|
| `academic-wide` | Same as `academic` but full viewport width |
|
|
172
197
|
| `vanilla` | Minimal HTML with no styling — useful for testing or custom CSS |
|
|
173
198
|
| `paged-professional` | Corporate style with Paged.js pagination for print-ready PDFs |
|
|
199
|
+
| `site` | Website layout: side nav with one page per H1, minimalist styling (HTML only) |
|
|
200
|
+
| `site-professional` | Same as `site` with a navy sidebar and card layout (HTML only) |
|
|
201
|
+
| `site-dark` | Same as `site` on a dark background (HTML only) |
|
|
202
|
+
| `site-terminal` | Same as `site` as a green-phosphor terminal, just for fun (HTML only) |
|
|
174
203
|
|
|
175
204
|
All themes support:
|
|
176
205
|
|
|
177
206
|
- Optional document title
|
|
178
|
-
- Optional table of contents
|
|
207
|
+
- Optional table of contents (the `site-*` themes use their side nav instead)
|
|
179
208
|
- [Mermaid.js](https://mermaid.js.org) diagram support (loaded only when diagrams are present)
|
|
180
209
|
- Graph diagram blocks
|
|
181
210
|
|
|
211
|
+
### Site themes
|
|
212
|
+
|
|
213
|
+
The `site-*` themes turn the output into a small website while keeping it a single HTML file: no web server needed, and it still works offline or as an email attachment.
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
mddoco ./docs --theme site --title "Team Handbook" --output ./out
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
- Every H1 starts a new page, and each page is listed in the side nav. Anything before the first H1 becomes an introduction page.
|
|
220
|
+
- The current page's H2 headings are listed under it in the nav.
|
|
221
|
+
- Only one page shows at a time, with previous/next links at the bottom. Each page has its own address (`handbook.html#/onboarding`, or `#/onboarding/first-week` for a heading), so the back button, bookmarks and shared links work.
|
|
222
|
+
- On narrow screens the nav moves behind a menu button.
|
|
223
|
+
- They are screen-only: `--format pdf` is refused. Printing from the browser still prints every page in sequence.
|
|
224
|
+
|
|
225
|
+
### Screen-only custom themes
|
|
226
|
+
|
|
227
|
+
A custom theme can refuse PDF output by declaring this at the top level of its template:
|
|
228
|
+
|
|
229
|
+
```jinja
|
|
230
|
+
{% set pdf_supported = false %}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
`--format pdf` with that theme then exits with an error instead of producing a PDF.
|
|
234
|
+
|
|
182
235
|
### Graph diagrams
|
|
183
236
|
|
|
184
237
|
Use ` ```graph ` fenced blocks with a JSON payload. The only required field is `data`.
|
|
@@ -1,25 +1,9 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: mddoco
|
|
3
|
-
Version: 2.0.3
|
|
4
|
-
Summary: Markdown to HTML/PDF document converter
|
|
5
|
-
Requires-Python: >=3.10
|
|
6
|
-
Description-Content-Type: text/markdown
|
|
7
|
-
License-File: LICENSE
|
|
8
|
-
Requires-Dist: click>=8.0
|
|
9
|
-
Requires-Dist: markdown>=3.5
|
|
10
|
-
Requires-Dist: jinja2>=3.1
|
|
11
|
-
Requires-Dist: playwright>=1.40
|
|
12
|
-
Requires-Dist: matplotlib>=3.7
|
|
13
|
-
Provides-Extra: dev
|
|
14
|
-
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
15
|
-
Requires-Dist: black>=24.0; extra == "dev"
|
|
16
|
-
Requires-Dist: ruff<0.17,>=0.16; extra == "dev"
|
|
17
|
-
Dynamic: license-file
|
|
18
|
-
|
|
19
1
|
# mddoco
|
|
20
2
|
|
|
21
3
|
A CLI tool that converts markdown files to a single HTML (or future PDF) document.
|
|
22
4
|
|
|
5
|
+
[](https://github.com/massyn/mddoco/actions/workflows/ci.yml)
|
|
6
|
+
 
|
|
23
7
|
## Installation
|
|
24
8
|
|
|
25
9
|
```bash
|
|
@@ -85,6 +69,25 @@ mddoco ./docs --title "My Project" --toc --output ./out
|
|
|
85
69
|
|
|
86
70
|
All matched markdown files are combined into a single HTML document in the output directory. The filename is derived from the input folder or file name.
|
|
87
71
|
|
|
72
|
+
## Examples
|
|
73
|
+
|
|
74
|
+
The [`examples/`](examples/) directory contains five runnable input folders, each
|
|
75
|
+
covering a different feature:
|
|
76
|
+
|
|
77
|
+
| Folder | Demonstrates |
|
|
78
|
+
|--------|--------------|
|
|
79
|
+
| [`examples/01_basic`](examples/01_basic) | Plain Markdown, file ordering, underscore-excluded files |
|
|
80
|
+
| [`examples/02_rich_content`](examples/02_rich_content) | Mermaid diagrams, syntax highlighting, `graph` charts |
|
|
81
|
+
| [`examples/03_themed_report`](examples/03_themed_report) | Themes and a table of contents |
|
|
82
|
+
| [`examples/04_jinja_data`](examples/04_jinja_data) | Jinja2 templates with JSON and CSV data files |
|
|
83
|
+
| [`examples/05_security_findings`](examples/05_security_findings) | CSV data grouped in a Jinja2 template and fed into a `graph` chart |
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
mddoco examples/03_themed_report --theme professional --title "Q3 Platform Review" --toc --output ./out
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
See [`examples/README.md`](examples/README.md) for the full list of commands.
|
|
90
|
+
|
|
88
91
|
## Jinja2 templates
|
|
89
92
|
|
|
90
93
|
Files ending in `.md.j2` are Jinja2 templates that produce Markdown. They are sorted alongside regular `.md` files (numeric prefixes such as `01_`, `02_` determine order) and converted through the same pipeline.
|
|
@@ -157,7 +160,7 @@ Use `{% import %}` or `{% from ... import %}` to make macros from another file c
|
|
|
157
160
|
|
|
158
161
|
## Themes
|
|
159
162
|
|
|
160
|
-
Themes are
|
|
163
|
+
Themes are Jinja2 HTML files with embedded CSS. Pass a theme name with `--theme NAME`. The `-wide` and `site-*` themes extend another theme (`default.html`, `_site_base.html` and so on), so to customise one of those, copy the theme it extends as well.
|
|
161
164
|
|
|
162
165
|
| Theme | Description |
|
|
163
166
|
|---|---|
|
|
@@ -171,14 +174,42 @@ Themes are self-contained Jinja2 HTML files with embedded CSS. Pass a theme name
|
|
|
171
174
|
| `academic-wide` | Same as `academic` but full viewport width |
|
|
172
175
|
| `vanilla` | Minimal HTML with no styling — useful for testing or custom CSS |
|
|
173
176
|
| `paged-professional` | Corporate style with Paged.js pagination for print-ready PDFs |
|
|
177
|
+
| `site` | Website layout: side nav with one page per H1, minimalist styling (HTML only) |
|
|
178
|
+
| `site-professional` | Same as `site` with a navy sidebar and card layout (HTML only) |
|
|
179
|
+
| `site-dark` | Same as `site` on a dark background (HTML only) |
|
|
180
|
+
| `site-terminal` | Same as `site` as a green-phosphor terminal, just for fun (HTML only) |
|
|
174
181
|
|
|
175
182
|
All themes support:
|
|
176
183
|
|
|
177
184
|
- Optional document title
|
|
178
|
-
- Optional table of contents
|
|
185
|
+
- Optional table of contents (the `site-*` themes use their side nav instead)
|
|
179
186
|
- [Mermaid.js](https://mermaid.js.org) diagram support (loaded only when diagrams are present)
|
|
180
187
|
- Graph diagram blocks
|
|
181
188
|
|
|
189
|
+
### Site themes
|
|
190
|
+
|
|
191
|
+
The `site-*` themes turn the output into a small website while keeping it a single HTML file: no web server needed, and it still works offline or as an email attachment.
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
mddoco ./docs --theme site --title "Team Handbook" --output ./out
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
- Every H1 starts a new page, and each page is listed in the side nav. Anything before the first H1 becomes an introduction page.
|
|
198
|
+
- The current page's H2 headings are listed under it in the nav.
|
|
199
|
+
- Only one page shows at a time, with previous/next links at the bottom. Each page has its own address (`handbook.html#/onboarding`, or `#/onboarding/first-week` for a heading), so the back button, bookmarks and shared links work.
|
|
200
|
+
- On narrow screens the nav moves behind a menu button.
|
|
201
|
+
- They are screen-only: `--format pdf` is refused. Printing from the browser still prints every page in sequence.
|
|
202
|
+
|
|
203
|
+
### Screen-only custom themes
|
|
204
|
+
|
|
205
|
+
A custom theme can refuse PDF output by declaring this at the top level of its template:
|
|
206
|
+
|
|
207
|
+
```jinja
|
|
208
|
+
{% set pdf_supported = false %}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
`--format pdf` with that theme then exits with an error instead of producing a PDF.
|
|
212
|
+
|
|
182
213
|
### Graph diagrams
|
|
183
214
|
|
|
184
215
|
Use ` ```graph ` fenced blocks with a JSON payload. The only required field is `data`.
|
|
@@ -4,10 +4,13 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "mddoco"
|
|
7
|
-
version = "2.0
|
|
7
|
+
version = "2.2.0"
|
|
8
8
|
description = "Markdown to HTML/PDF document converter"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "Phil Massyn", email = "phil.massyn@icloud.com" },
|
|
13
|
+
]
|
|
11
14
|
dependencies = [
|
|
12
15
|
"click>=8.0",
|
|
13
16
|
"markdown>=3.5",
|
|
@@ -23,6 +26,11 @@ dev = [
|
|
|
23
26
|
"ruff>=0.16,<0.17",
|
|
24
27
|
]
|
|
25
28
|
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/massyn/mddoco"
|
|
31
|
+
Repository = "https://github.com/massyn/mddoco"
|
|
32
|
+
Issues = "https://github.com/massyn/mddoco/issues"
|
|
33
|
+
|
|
26
34
|
[project.scripts]
|
|
27
35
|
mddoco = "mddoco.cli:main"
|
|
28
36
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "2.2.0"
|
|
@@ -6,7 +6,7 @@ import click
|
|
|
6
6
|
from mddoco.converter import convert_files
|
|
7
7
|
from mddoco.pdf import html_to_pdf
|
|
8
8
|
from mddoco.preprocessor import load_context
|
|
9
|
-
from mddoco.renderer import render_html
|
|
9
|
+
from mddoco.renderer import check_theme_format, render_html
|
|
10
10
|
from mddoco.scanner import find_markdown_files
|
|
11
11
|
from mddoco.toc import combine_toc
|
|
12
12
|
from mddoco.writer import write_output
|
|
@@ -64,6 +64,11 @@ def main(
|
|
|
64
64
|
"""Convert markdown files in INPUT_PATH to a single document."""
|
|
65
65
|
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
|
|
66
66
|
|
|
67
|
+
try:
|
|
68
|
+
check_theme_format(theme, fmt)
|
|
69
|
+
except ValueError as exc:
|
|
70
|
+
raise click.ClickException(str(exc)) from exc
|
|
71
|
+
|
|
67
72
|
try:
|
|
68
73
|
md_files = find_markdown_files(input_path)
|
|
69
74
|
except (ValueError, FileNotFoundError) as exc:
|
|
@@ -79,6 +84,7 @@ def main(
|
|
|
79
84
|
sections = [(p, html) for p, html, _ in raw]
|
|
80
85
|
toc_html = combine_toc([t for _, _, t in raw]) if toc else None
|
|
81
86
|
has_mermaid = any('<div class="mermaid">' in html for _, html in sections)
|
|
87
|
+
has_code = any("<pre><code" in html for _, html in sections)
|
|
82
88
|
|
|
83
89
|
# Resolve output directory and filename.
|
|
84
90
|
# If output_path has a suffix, treat it as an explicit file path.
|
|
@@ -99,6 +105,7 @@ def main(
|
|
|
99
105
|
toc_html=toc_html or None,
|
|
100
106
|
theme=theme,
|
|
101
107
|
has_mermaid=has_mermaid,
|
|
108
|
+
has_code=has_code,
|
|
102
109
|
)
|
|
103
110
|
except ValueError as exc:
|
|
104
111
|
raise click.ClickException(str(exc)) from exc
|
|
@@ -11,6 +11,24 @@ _INSTALL_HINT = (
|
|
|
11
11
|
"automatically. Run: playwright install chromium"
|
|
12
12
|
)
|
|
13
13
|
|
|
14
|
+
# Paged.js paginates on its own once the DOM is ready, and its page container
|
|
15
|
+
# exists before any pages are added to it. The only reliable completion signal
|
|
16
|
+
# is the previewer's "rendered" event, so this script, run before any page
|
|
17
|
+
# script, subscribes to it the moment Paged.js assigns window.PagedPolyfill.
|
|
18
|
+
_PAGED_RENDERED_HOOK = """
|
|
19
|
+
(() => {
|
|
20
|
+
let previewer;
|
|
21
|
+
Object.defineProperty(window, "PagedPolyfill", {
|
|
22
|
+
configurable: true,
|
|
23
|
+
get: () => previewer,
|
|
24
|
+
set: (value) => {
|
|
25
|
+
previewer = value;
|
|
26
|
+
value.on("rendered", () => { window.__mddocoPagedRendered = true; });
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
})();
|
|
30
|
+
"""
|
|
31
|
+
|
|
14
32
|
|
|
15
33
|
def html_to_pdf(html_content: str, dest: Path) -> None:
|
|
16
34
|
"""Render an HTML string to a PDF file using a headless Chromium browser.
|
|
@@ -55,12 +73,13 @@ def _render(sync_playwright, source: Path, dest: Path) -> None:
|
|
|
55
73
|
with sync_playwright() as p:
|
|
56
74
|
browser = p.chromium.launch()
|
|
57
75
|
page = browser.new_page()
|
|
76
|
+
page.add_init_script(_PAGED_RENDERED_HOOK)
|
|
58
77
|
page.goto(source.as_uri(), wait_until="networkidle")
|
|
59
78
|
# If Paged.js is present, wait for it to finish paginating before
|
|
60
79
|
# capturing — networkidle fires before its JS layout pass completes.
|
|
61
80
|
page.wait_for_function(
|
|
62
81
|
"typeof window.PagedPolyfill === 'undefined'"
|
|
63
|
-
" ||
|
|
82
|
+
" || window.__mddocoPagedRendered === true"
|
|
64
83
|
)
|
|
65
84
|
page.pdf(path=str(dest), format="A4", print_background=True)
|
|
66
85
|
browser.close()
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from importlib.resources import files
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from jinja2 import Environment, FileSystemLoader, Template, TemplateNotFound
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def load_theme(theme: str) -> Template:
|
|
8
|
+
"""Load a theme by built-in name or by path to an HTML template file."""
|
|
9
|
+
theme_path = Path(theme)
|
|
10
|
+
if theme_path.is_file():
|
|
11
|
+
loader_dir = str(theme_path.parent.resolve())
|
|
12
|
+
template_name = theme_path.name
|
|
13
|
+
else:
|
|
14
|
+
loader_dir = str(files("mddoco").joinpath("themes"))
|
|
15
|
+
template_name = f"{theme}.html"
|
|
16
|
+
|
|
17
|
+
env = Environment(loader=FileSystemLoader(loader_dir), autoescape=True)
|
|
18
|
+
try:
|
|
19
|
+
return env.get_template(template_name)
|
|
20
|
+
except TemplateNotFound:
|
|
21
|
+
raise ValueError(f"Theme '{theme}' not found.")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def theme_supports_pdf(theme: str) -> bool:
|
|
25
|
+
"""Return False when the theme opts out of PDF output.
|
|
26
|
+
|
|
27
|
+
A screen-only theme declares ``{% set pdf_supported = false %}`` at the
|
|
28
|
+
top level of its template; themes that say nothing support PDF.
|
|
29
|
+
"""
|
|
30
|
+
return getattr(load_theme(theme).module, "pdf_supported", True) is not False
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def check_theme_format(theme: str, fmt: str) -> None:
|
|
34
|
+
"""Raise ValueError when the theme is missing or cannot produce ``fmt``."""
|
|
35
|
+
if fmt == "pdf" and not theme_supports_pdf(theme):
|
|
36
|
+
raise ValueError(
|
|
37
|
+
f"Theme '{theme}' is screen-only and cannot produce PDF output. "
|
|
38
|
+
"Use --format html, or choose another theme for PDF."
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def render_html(
|
|
43
|
+
sections: list[tuple[Path, str]],
|
|
44
|
+
title: str | None = None,
|
|
45
|
+
toc_html: str | None = None,
|
|
46
|
+
theme: str = "default",
|
|
47
|
+
has_mermaid: bool = False,
|
|
48
|
+
has_code: bool = False,
|
|
49
|
+
) -> str:
|
|
50
|
+
"""Render a full HTML document from (path, html_fragment) pairs."""
|
|
51
|
+
return load_theme(theme).render(
|
|
52
|
+
title=title,
|
|
53
|
+
toc_html=toc_html,
|
|
54
|
+
sections=sections,
|
|
55
|
+
has_mermaid=has_mermaid,
|
|
56
|
+
has_code=has_code,
|
|
57
|
+
)
|