Skip to content

Commit cfea217

Browse files
samuelcolvinclaude
andauthored
Build docs with zensical instead of mkdocs (#172)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 7b9b9e8 commit cfea217

13 files changed

Lines changed: 202 additions & 693 deletions

File tree

‎HISTORY.md‎

Lines changed: 0 additions & 93 deletions
This file was deleted.

‎Makefile‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.DEFAULT_GOAL := all
1+
.DEFAULT_GOAL := help
22

33
.PHONY: .uv
44
.uv:
@@ -50,12 +50,14 @@ clean: ## Clean up build artifacts
5050

5151
.PHONY: docs
5252
docs: ## Build docs
53-
uv run ruff check --line-length=80 docs/examples/
54-
uv run mkdocs build
53+
uv run ruff check --isolated --line-length=80 docs/examples/
54+
uv run python docs/build.py
55+
uv run zensical build --strict
5556

5657
.PHONY: docs-serve
5758
docs-serve: ## Serve docs locally
58-
uv run mkdocs serve
59+
uv run python docs/build.py
60+
uv run zensical serve
5961

6062
# (must stay last!)
6163
.PHONY: help

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# python devtools
22

33
[![CI](https://github.com/samuelcolvin/python-devtools/workflows/CI/badge.svg?event=push)](https://github.com/samuelcolvin/python-devtools/actions?query=event%3Apush+branch%3Amain+workflow%3ACI)
4-
[![Coverage](https://codecov.io/gh/samuelcolvin/python-devtools/branch/main/graph/badge.svg)](https://codecov.io/gh/samuelcolvin/python-devtools)
4+
[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/samuelcolvin/python-devtools.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/samuelcolvin/python-devtools)
55
[![pypi](https://img.shields.io/pypi/v/devtools.svg)](https://pypi.python.org/pypi/devtools)
66
[![versions](https://img.shields.io/pypi/pyversions/devtools.svg)](https://github.com/samuelcolvin/python-devtools)
77
[![license](https://img.shields.io/github/license/samuelcolvin/python-devtools.svg)](https://github.com/samuelcolvin/python-devtools/blob/main/LICENSE)
@@ -18,7 +18,7 @@ Just
1818
pip install devtools
1919
```
2020

21-
If you've got python 3.7+ and `pip` installed, you're good to go.
21+
If you've got python 3.10+ and `pip` installed, you're good to go.
2222

2323
## Usage
2424

‎docs/build.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Generate the files the docs include via `pymdownx.snippets` before zensical builds the site.
3+
4+
Zensical has no plugin hooks, so anything that needs Python (running the examples to capture their coloured output,
5+
reading the package version) happens here, writing files which the markdown then includes.
6+
"""
7+
8+
import logging
9+
import os
10+
import subprocess
11+
import sys
12+
from pathlib import Path
13+
14+
from ansi2html import Ansi2HTMLConverter
15+
16+
THIS_DIR = Path(__file__).parent
17+
PROJECT_ROOT = THIS_DIR.parent
18+
19+
logger = logging.getLogger('docs.build')
20+
21+
22+
def main() -> None:
23+
logging.basicConfig(level=logging.INFO, format='%(message)s')
24+
build_version()
25+
build_examples()
26+
27+
28+
def build_version() -> None:
29+
from devtools import __version__
30+
31+
write_if_changed(THIS_DIR / '.version.md', f'Documentation for version: **v{__version__}**\n')
32+
33+
34+
def build_examples() -> None:
35+
env = {**os.environ, 'PY_DEVTOOLS_HIGHLIGHT': 'true', 'PY_DEVTOOLS_WIDTH': '80'}
36+
conv = Ansi2HTMLConverter()
37+
for example in sorted((THIS_DIR / 'examples').glob('*.py')):
38+
logger.info('running %s to generate HTML...', example.relative_to(PROJECT_ROOT))
39+
p = subprocess.run((sys.executable, str(example)), stdout=subprocess.PIPE, check=True, env=env, cwd=THIS_DIR)
40+
html = conv.convert(p.stdout.decode(), full=False).strip('\r\n')
41+
write_if_changed(example.with_suffix('.html'), f'<pre class="ansi2html-content">\n{html}\n</pre>\n')
42+
43+
44+
def write_if_changed(path: Path, content: str) -> None:
45+
"""Avoid touching files that haven't changed so `zensical serve` doesn't rebuild needlessly."""
46+
if not path.exists() or path.read_text() != content:
47+
path.write_text(content)
48+
49+
50+
if __name__ == '__main__':
51+
main()

‎docs/history.md‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎docs/index.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# Python devtools
22

33
[![CI](https://github.com/samuelcolvin/python-devtools/workflows/CI/badge.svg?event=push)](https://github.com/samuelcolvin/python-devtools/actions?query=event%3Apush+branch%3Amain+workflow%3ACI)
4-
[![Coverage](https://codecov.io/gh/samuelcolvin/python-devtools/branch/main/graph/badge.svg)](https://codecov.io/gh/samuelcolvin/python-devtools)
4+
[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/samuelcolvin/python-devtools.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/samuelcolvin/python-devtools)
55
[![pypi](https://img.shields.io/pypi/v/devtools.svg)](https://pypi.python.org/pypi/devtools)
66
[![versions](https://img.shields.io/pypi/pyversions/devtools.svg)](https://github.com/samuelcolvin/python-devtools)
77
[![license](https://img.shields.io/github/license/samuelcolvin/python-devtools.svg)](https://github.com/samuelcolvin/python-devtools/blob/main/LICENSE)
88

9-
{{ version }}
9+
--8<-- ".version.md"
1010

1111
**Python's missing debug print command and other development tools.**
1212

1313
```py
14-
{!examples/example.py!}
14+
--8<-- "examples/example.py"
1515
```
1616

17-
{{ example_html(examples/example.py) }}
17+
--8<-- "examples/example.html"
1818

1919
Python devtools can do much more, see [Usage](./usage.md) for examples.

‎docs/install.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ pip install devtools
55
```
66

77
`devtools` has [very few dependencies](https://github.com/samuelcolvin/python-devtools/blob/main/pyproject.toml#L37).
8-
If you've got python 3.7+ and `pip` installed, you're good to go.
8+
If you've got python 3.10+ and `pip` installed, you're good to go.

‎docs/plugins.py‎

Lines changed: 0 additions & 83 deletions
This file was deleted.

‎docs/usage.md‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ and readable way to print stuff during development. (If you know why this is, I'
66
**The wait is over:**
77

88
```py
9-
{!examples/example.py!}
9+
--8<-- "examples/example.py"
1010
```
1111

12-
{{ example_html(examples/example.py) }}
12+
--8<-- "examples/example.html"
1313

1414
`debug` is like `print` after a good night's sleep and lots of coffee:
1515

@@ -21,10 +21,10 @@ and readable way to print stuff during development. (If you know why this is, I'
2121
A more complex example of `debug` shows more of what it can do.
2222

2323
```py
24-
{!examples/complex.py!}
24+
--8<-- "examples/complex.py"
2525
```
2626

27-
{{ example_html(examples/complex.py) }}
27+
--8<-- "examples/complex.html"
2828

2929
### Returning the arguments
3030

@@ -37,10 +37,10 @@ The returned arguments work as follows:
3737
* if keyword arguments are passed to `debug()`, the `kwargs` dictionary is added to the returned tuple
3838

3939
```py
40-
{!examples/return_args.py!}
40+
--8<-- "examples/return_args.py"
4141
```
4242

43-
{{ example_html(examples/return_args.py) }}
43+
--8<-- "examples/return_args.html"
4444

4545
## Other debug tools
4646

@@ -51,10 +51,10 @@ The debug namespace includes a number of other useful functions:
5151
* `debug.breakpoint()` introduces a breakpoint using `pdb`
5252

5353
```py
54-
{!examples/other.py!}
54+
--8<-- "examples/other.py"
5555
```
5656

57-
{{ example_html(examples/other.py) }}
57+
--8<-- "examples/other.html"
5858

5959
### Prettier print
6060

@@ -66,18 +66,18 @@ To get round this *devtools* comes with prettier print, my take on pretty printi
6666
in `debug()`, but it can also be used directly:
6767

6868
```py
69-
{!examples/prettier.py!}
69+
--8<-- "examples/prettier.py"
7070
```
7171

72-
{{ example_html(examples/prettier.py) }}
72+
--8<-- "examples/prettier.html"
7373

7474
For more details on prettier printing, see
7575
[`prettier.py`](https://github.com/samuelcolvin/python-devtools/blob/main/devtools/prettier.py).
7676

7777
## ANSI terminal colours
7878

7979
```py
80-
{!examples/ansi_colours.py!}
80+
--8<-- "examples/ansi_colours.py"
8181
```
8282

8383
For more details on ansi colours, see
@@ -95,10 +95,6 @@ Two ways to do this:
9595

9696
### Automatic install
9797

98-
!!! warning
99-
This is experimental, please [create an issue](https://github.com/samuelcolvin/python-devtools/issues)
100-
if you encounter any problems.
101-
10298
To install `debug` into `__builtins__` automatically, run:
10399

104100
```bash

0 commit comments

Comments
 (0)