Skip to content

Commit 5e1a778

Browse files
authored
docs: use natsort for docs (#51)
* docs: use natsort for docs * fix catalog sorting * docs: use index and fix search
1 parent ed71244 commit 5e1a778

File tree

4 files changed

+48
-30
lines changed

4 files changed

+48
-30
lines changed

docs/_gen_cmaps.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import json
22
from pathlib import Path
3-
from typing import TYPE_CHECKING, cast
3+
from typing import TYPE_CHECKING
44

55
import mkdocs_gen_files
6+
import natsort
67
import numpy as np
78

89
if TYPE_CHECKING:
@@ -84,7 +85,8 @@
8485

8586

8687
def build_catalog(catalog: "_catalog.Catalog") -> None:
87-
for name in catalog:
88+
nav = mkdocs_gen_files.Nav()
89+
for name in natsort.natsorted(catalog, alg=natsort.ns.IGNORECASE):
8890
if ":" not in name:
8991
continue
9092
try:
@@ -116,7 +118,9 @@ def build_catalog(catalog: "_catalog.Catalog") -> None:
116118
aliases = _make_aliases_md(_aliases) if _aliases else ""
117119

118120
# write the actual markdown file
119-
with mkdocs_gen_files.open(f"catalog/{category}/{name.lower()}.md", "w") as f:
121+
doc_path = f"{category}/{name.lower()}.md"
122+
nav[(category.title(), name)] = doc_path
123+
with mkdocs_gen_files.open(f"catalog/{doc_path}", "w") as f:
120124
f.write(
121125
TEMPLATE.format(
122126
name=name,
@@ -130,9 +134,15 @@ def build_catalog(catalog: "_catalog.Catalog") -> None:
130134
)
131135
)
132136

137+
# sort categories alphabetically
138+
nav._data = dict(sorted(nav._data.items(), key=lambda x: x[0][0]))
139+
with mkdocs_gen_files.open("catalog/SUMMARY.md", "w") as nav_file:
140+
nav_file.writelines(["# SUMMARY { data-search-exclude }\n"])
141+
nav_file.writelines(nav.build_literate_nav())
142+
133143

134144
def _make_aliases_md(aliases: list[str]) -> str:
135145
return "**Aliases**: " + ", ".join(f"`{a}`" for a in aliases)
136146

137147

138-
build_catalog(cast("_catalog.Catalog", Colormap.catalog()))
148+
build_catalog(Colormap.catalog())

docs/_hooks.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
from typing import TYPE_CHECKING, Any, Sequence, cast
77

8+
import natsort
89
import numpy as np
910

1011
from cmap import Colormap, _util
@@ -24,7 +25,7 @@
2425
"""
2526
CMAP_LINK = '<a href="{url}">' + CMAP_DIV + "</a>"
2627
DEV_MODE = "serve" in sys.argv
27-
SINERAMP = _util.sineramp((96, 512))[:, ::-1]
28+
SINERAMP = _util.sineramp((96, 826))[:, ::-1]
2829

2930

3031
def _to_img_tag(
@@ -34,7 +35,7 @@ def _to_img_tag(
3435
img: np.ndarray | None = None,
3536
) -> str:
3637
"""Return a base64-encoded <img> tag for the given colormap."""
37-
_img = cm._repr_png_(width=256, height=1, img=img)
38+
_img = cm._repr_png_(width=826, height=1, img=img)
3839
data = base64.b64encode(_img).decode("ascii")
3940
return (
4041
f'<img style="height: {height}" width="{width}" src="data:image/png;base64,'
@@ -91,7 +92,9 @@ def _cmap_catalog() -> str:
9192
"""
9293
categories = set()
9394
lines = []
94-
for cmap_name, details in sorted(CATALOG.items(), key=lambda x: x[0].lower()):
95+
for cmap_name, details in natsort.natsorted(
96+
CATALOG.items(), key=lambda x: x[0].lower()
97+
):
9598
if "alias" in details:
9699
continue
97100
category = details.get("category") or "Uncategorized"
@@ -185,7 +188,9 @@ def on_page_content(html: str, **kwargs: Any) -> str:
185188

186189
def _write_cmap_redirects(site_dir: str) -> None:
187190
sd = Path(site_dir)
188-
for cmap_name, details in sorted(CATALOG.items(), key=lambda x: x[0].lower()):
191+
for cmap_name, details in natsort.natsorted(
192+
CATALOG.items(), key=lambda x: x[0].lower()
193+
):
189194
if "alias" in details:
190195
cmap_name = cmap_name.replace(":", "-")
191196
real = Colormap(details["alias"]) # type: ignore
@@ -198,6 +203,6 @@ def _write_cmap_redirects(site_dir: str) -> None:
198203
f.write(content)
199204

200205

201-
def on_post_build(config, **kwargs: Any) -> None:
206+
def on_post_build(config: dict, **kwargs: Any) -> None:
202207
"""Copy the extra javascripts to the output directory."""
203208
_write_cmap_redirects(config["site_dir"])

mkdocs.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ plugins:
6969
- search:
7070
separator: '[\s\-\_,:!=\[\]()"/]+|(?!\b)(?=[A-Z][a-z])|\.(?!\d)|&[lg]t;'
7171
- autorefs
72-
- literate-nav
72+
- literate-nav:
73+
implicit_index: true
74+
nav_file: SUMMARY.md
7375
- gen-files:
7476
scripts:
7577
- docs/_gen_cmaps.py

pyproject.toml

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ globs = ["LICENSE/*"]
3131
# https://peps.python.org/pep-0621/#dependencies-optional-dependencies
3232
[project.optional-dependencies]
3333
docs = [
34-
'mkdocs',
35-
'mkdocs-material',
36-
'mkdocstrings-python',
37-
'mkdocs-minify-plugin',
38-
'mkdocs-literate-nav',
39-
'mkdocs-gen-files',
34+
'colorcet',
4035
'colorspacious',
4136
'imageio',
42-
'colorcet',
37+
'mkdocs-gen-files',
38+
'mkdocs-literate-nav',
39+
'mkdocs-material',
40+
'mkdocs-minify-plugin',
41+
'mkdocs',
42+
'mkdocstrings-python',
43+
'natsort',
4344
]
4445
test_min = ["pytest>=6.0"]
4546
test = ["cmap[test_min]", "pytest-cov"]
@@ -96,20 +97,20 @@ src = ["src"]
9697
[tool.ruff.lint]
9798
pydocstyle = { convention = "numpy" }
9899
select = [
99-
"E", # style errors
100-
"F", # flakes
101-
"D", # pydocstyle
100+
"E", # style errors
101+
"F", # flakes
102+
"D", # pydocstyle
102103
"D417", # Missing argument descriptions in Docstrings
103-
"I", # isort
104-
"UP", # pyupgrade
105-
"S", # bandit
106-
"C4", # flake8-comprehensions
107-
"B", # flake8-bugbear
108-
"ISC", # implicit-str-concat
109-
"TID", # tidy-imports
110-
"RUF", # ruff-specific rules
111-
"TCH", # flake8-type-checking
112-
"TID", # flake8-tidy-imports
104+
"I", # isort
105+
"UP", # pyupgrade
106+
"S", # bandit
107+
"C4", # flake8-comprehensions
108+
"B", # flake8-bugbear
109+
"ISC", # implicit-str-concat
110+
"TID", # tidy-imports
111+
"RUF", # ruff-specific rules
112+
"TCH", # flake8-type-checking
113+
"TID", # flake8-tidy-imports
113114
]
114115
ignore = [
115116
"D100", # Missing docstring in public module

0 commit comments

Comments
 (0)