MartinPdeS-docs 0.1.3__tar.gz → 0.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: MartinPdeS-docs
3
- Version: 0.1.3
3
+ Version: 0.2.0
4
4
  Summary: Shared Sphinx configuration for MartinPdeS scientific Python packages.
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -39,3 +39,21 @@ globals().update(
39
39
 
40
40
  Project-specific values can be assigned below this call to override any
41
41
  shared default.
42
+
43
+ For a multi-section Sphinx-Gallery, pass the section names instead of
44
+ repeating the directory mappings:
45
+
46
+ ```python
47
+ globals().update(
48
+ configure(
49
+ project="FlowCyPy",
50
+ package="FlowCyPy",
51
+ repository="MartinPdeS/FlowCyPy",
52
+ gallery_sections=["tutorials", "studies", "validation"],
53
+ gallery_default_thumb="_static/gallery-default.png",
54
+ )
55
+ )
56
+ ```
57
+
58
+ Use `gallery_options` for project-specific Sphinx-Gallery settings such as an
59
+ additional image scraper or an example error policy.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: MartinPdeS-docs
3
- Version: 0.1.3
3
+ Version: 0.2.0
4
4
  Summary: Shared Sphinx configuration for MartinPdeS scientific Python packages.
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -39,3 +39,21 @@ globals().update(
39
39
 
40
40
  Project-specific values can be assigned below this call to override any
41
41
  shared default.
42
+
43
+ For a multi-section Sphinx-Gallery, pass the section names instead of
44
+ repeating the directory mappings:
45
+
46
+ ```python
47
+ globals().update(
48
+ configure(
49
+ project="FlowCyPy",
50
+ package="FlowCyPy",
51
+ repository="MartinPdeS/FlowCyPy",
52
+ gallery_sections=["tutorials", "studies", "validation"],
53
+ gallery_default_thumb="_static/gallery-default.png",
54
+ )
55
+ )
56
+ ```
57
+
58
+ Use `gallery_options` for project-specific Sphinx-Gallery settings such as an
59
+ additional image scraper or an example error policy.
@@ -27,3 +27,21 @@ globals().update(
27
27
 
28
28
  Project-specific values can be assigned below this call to override any
29
29
  shared default.
30
+
31
+ For a multi-section Sphinx-Gallery, pass the section names instead of
32
+ repeating the directory mappings:
33
+
34
+ ```python
35
+ globals().update(
36
+ configure(
37
+ project="FlowCyPy",
38
+ package="FlowCyPy",
39
+ repository="MartinPdeS/FlowCyPy",
40
+ gallery_sections=["tutorials", "studies", "validation"],
41
+ gallery_default_thumb="_static/gallery-default.png",
42
+ )
43
+ )
44
+ ```
45
+
46
+ Use `gallery_options` for project-specific Sphinx-Gallery settings such as an
47
+ additional image scraper or an example error policy.
@@ -1,9 +1,40 @@
1
1
  """Configurable defaults for the shared project documentation sites."""
2
2
 
3
3
  import os
4
+ from collections.abc import Mapping, Sequence
5
+ from importlib.metadata import PackageNotFoundError, version as distribution_version
4
6
  from pathlib import Path
5
7
 
6
8
 
9
+ def _raster_thumbnail_path(logo: str, favicon: str) -> str | None:
10
+ """Choose a Sphinx-Gallery-compatible fallback thumbnail.
11
+
12
+ Sphinx-Gallery requires a raster image for ``default_thumb_file``. SVG
13
+ files are valid theme logos and favicons, but are not accepted as gallery
14
+ thumbnail sources by all supported Sphinx-Gallery versions.
15
+ """
16
+ raster_suffixes = {".png", ".jpg", ".jpeg", ".gif"}
17
+ for candidate in (logo, favicon):
18
+ if Path(candidate).suffix.lower() in raster_suffixes:
19
+ return candidate
20
+ return None
21
+
22
+
23
+ def _resolve_version(package: str, version: str | None) -> str:
24
+ """Resolve the documentation version without importing the package."""
25
+ if version is not None:
26
+ return version
27
+
28
+ environment_version = os.getenv("tag")
29
+ if environment_version:
30
+ return environment_version
31
+
32
+ try:
33
+ return distribution_version(package)
34
+ except PackageNotFoundError:
35
+ return "latest"
36
+
37
+
7
38
  def configure(
8
39
  *,
9
40
  project: str,
@@ -15,7 +46,10 @@ def configure(
15
46
  extensions: list[str] | None = None,
16
47
  html_theme_options: dict | None = None,
17
48
  sphinx_gallery_conf: dict | None = None,
18
- version: str = "latest",
49
+ gallery_sections: Sequence[str] | None = None,
50
+ gallery_default_thumb: str | None = None,
51
+ gallery_options: Mapping[str, object] | None = None,
52
+ version: str | None = None,
19
53
  source_suffix: str = ".rst",
20
54
  master_doc: str = "index",
21
55
  ) -> dict:
@@ -23,13 +57,20 @@ def configure(
23
57
 
24
58
  In a project's ``conf.py`` use ``globals().update(configure(...))`` and
25
59
  keep project-specific settings below that call so they remain easy to
26
- override.
60
+ override. Pass ``gallery_sections`` when examples are split into named
61
+ directories; use ``gallery_default_thumb`` for a project-specific raster
62
+ fallback; and use ``gallery_options`` for less common Sphinx-Gallery
63
+ settings.
27
64
  """
65
+ resolved_version = _resolve_version(package, version)
66
+
28
67
  default_extensions = [
29
68
  "sphinx.ext.autodoc",
30
69
  "sphinx.ext.autosummary",
31
70
  "sphinx.ext.intersphinx",
71
+ "sphinx.ext.mathjax",
32
72
  "sphinx.ext.napoleon",
73
+ "sphinx.ext.autosectionlabel",
33
74
  "sphinx.ext.viewcode",
34
75
  "sphinx.ext.todo",
35
76
  "sphinx_design",
@@ -71,7 +112,7 @@ def configure(
71
112
  ]
72
113
  options["switcher"] = {
73
114
  "json_url": f"https://raw.githubusercontent.com/{repository}/documentation_page/version_switcher.json",
74
- "version_match": version,
115
+ "version_match": resolved_version,
75
116
  }
76
117
 
77
118
  html_context = {}
@@ -97,11 +138,25 @@ def configure(
97
138
  "plot_gallery": True,
98
139
  "thumbnail_size": [600, 600],
99
140
  "download_all_examples": False,
141
+ "reset_modules_order": "after",
142
+ "within_subsection_order": "FileNameSortKey",
143
+ # Use the project's favicon for examples that do not create a figure.
144
+ # The path is interpreted relative to the documentation source tree,
145
+ # just like ``html_favicon``.
146
+ "default_thumb_file": _raster_thumbnail_path(logo, favicon),
100
147
  "line_numbers": False,
101
148
  "remove_config_comments": True,
102
149
  "capture_repr": ("_repr_html_", "__repr__"),
103
150
  "nested_sections": True,
104
151
  }
152
+ if gallery_sections:
153
+ sections = list(gallery_sections)
154
+ gallery["examples_dirs"] = [f"../examples/{section}" for section in sections]
155
+ gallery["gallery_dirs"] = [f"gallery/{section}" for section in sections]
156
+ if gallery_default_thumb is not None:
157
+ gallery["default_thumb_file"] = gallery_default_thumb
158
+ if gallery_options:
159
+ gallery.update(gallery_options)
105
160
  if sphinx_gallery_conf:
106
161
  gallery.update(sphinx_gallery_conf)
107
162
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "MartinPdeS-docs"
7
- version = "0.1.3"
7
+ version = "0.2.0"
8
8
  description = "Shared Sphinx configuration for MartinPdeS scientific Python packages."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -0,0 +1,81 @@
1
+ from martinpdes_docs import configure
2
+
3
+
4
+ def test_configuration_has_project_overrides():
5
+ config = configure(project="Example", package="example", repository="MartinPdeS/Example")
6
+
7
+ assert config["project"] == "Example"
8
+ assert config["package"] == "example"
9
+ assert config["html_logo"] == "_static/logo.svg"
10
+ assert config["html_static_path"][0] == "_static"
11
+ assert config["html_static_path"][1].endswith("martinpdes_docs/static")
12
+ assert config["templates_path"] == ["_templates"]
13
+ assert config["html_css_files"] == ["default.css", "martinpdes_layout.css"]
14
+ assert config["epub_exclude_files"] == ["search.html"]
15
+ assert config["sphinx_gallery_conf"]["image_scrapers"] == ("matplotlib",)
16
+ assert config["sphinx_gallery_conf"]["thumbnail_size"] == [600, 600]
17
+ assert config["sphinx_gallery_conf"]["default_thumb_file"] is None
18
+ assert config["sphinx_gallery_conf"]["reset_modules_order"] == "after"
19
+ assert config["sphinx_gallery_conf"]["within_subsection_order"] == "FileNameSortKey"
20
+
21
+ sections = configure(
22
+ project="Example",
23
+ package="example",
24
+ gallery_sections=["tutorials", "validation"],
25
+ gallery_default_thumb="_static/gallery-default.png",
26
+ )
27
+ assert sections["sphinx_gallery_conf"]["examples_dirs"] == [
28
+ "../examples/tutorials",
29
+ "../examples/validation",
30
+ ]
31
+ assert sections["sphinx_gallery_conf"]["gallery_dirs"] == [
32
+ "gallery/tutorials",
33
+ "gallery/validation",
34
+ ]
35
+ assert sections["sphinx_gallery_conf"]["default_thumb_file"] == (
36
+ "_static/gallery-default.png"
37
+ )
38
+
39
+ raster = configure(
40
+ project="Example",
41
+ package="example",
42
+ logo="../images/logo.png",
43
+ favicon="_static/favicon.svg",
44
+ )
45
+ assert raster["sphinx_gallery_conf"]["default_thumb_file"] == "../images/logo.png"
46
+ assert "github_url" not in config["html_theme_options"]
47
+ assert config["html_context"]["github_repo"] == "Example"
48
+
49
+
50
+ def test_custom_extensions_and_theme_options_are_preserved():
51
+ config = configure(
52
+ project="Example",
53
+ package="example",
54
+ extensions=["custom.extension"],
55
+ html_theme_options={"announcement": "Hello"},
56
+ )
57
+
58
+ assert config["extensions"] == ["custom.extension"]
59
+ assert config["html_theme_options"]["announcement"] == "Hello"
60
+
61
+
62
+ def test_gallery_options_override_shared_defaults():
63
+ config = configure(
64
+ project="Example",
65
+ package="example",
66
+ gallery_options={"image_scrapers": ("matplotlib", "pyvista")},
67
+ )
68
+
69
+ assert config["sphinx_gallery_conf"]["image_scrapers"] == ("matplotlib", "pyvista")
70
+
71
+
72
+ def test_version_can_be_resolved_from_the_environment(monkeypatch):
73
+ monkeypatch.setenv("tag", "v2.3.4")
74
+
75
+ config = configure(
76
+ project="Example",
77
+ package="example",
78
+ repository="MartinPdeS/Example",
79
+ )
80
+
81
+ assert config["html_theme_options"]["switcher"]["version_match"] == "v2.3.4"
@@ -1,30 +0,0 @@
1
- from martinpdes_docs import configure
2
-
3
-
4
- def test_configuration_has_project_overrides():
5
- config = configure(project="Example", package="example", repository="MartinPdeS/Example")
6
-
7
- assert config["project"] == "Example"
8
- assert config["package"] == "example"
9
- assert config["html_logo"] == "_static/logo.svg"
10
- assert config["html_static_path"][0] == "_static"
11
- assert config["html_static_path"][1].endswith("martinpdes_docs/static")
12
- assert config["templates_path"] == ["_templates"]
13
- assert config["html_css_files"] == ["default.css", "martinpdes_layout.css"]
14
- assert config["epub_exclude_files"] == ["search.html"]
15
- assert config["sphinx_gallery_conf"]["image_scrapers"] == ("matplotlib",)
16
- assert config["sphinx_gallery_conf"]["thumbnail_size"] == [600, 600]
17
- assert "github_url" not in config["html_theme_options"]
18
- assert config["html_context"]["github_repo"] == "Example"
19
-
20
-
21
- def test_custom_extensions_and_theme_options_are_preserved():
22
- config = configure(
23
- project="Example",
24
- package="example",
25
- extensions=["custom.extension"],
26
- html_theme_options={"announcement": "Hello"},
27
- )
28
-
29
- assert config["extensions"] == ["custom.extension"]
30
- assert config["html_theme_options"]["announcement"] == "Hello"