From 8b60a6735c9d92742576b0b7fa82da2eaf1003f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Eertmans?= Date: Wed, 18 Dec 2024 13:38:38 +0100 Subject: [PATCH] chore(docs): from viewcode to linkcode Link to code definition on GitHub push --- differt/src/differt/em/_special.py | 6 ++-- docs/source/conf.py | 45 ++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/differt/src/differt/em/_special.py b/differt/src/differt/em/_special.py index 11045713..b5566d2e 100644 --- a/differt/src/differt/em/_special.py +++ b/differt/src/differt/em/_special.py @@ -215,11 +215,11 @@ def fresnel( """ # Constant factors sqrtpi_2_4 = 0.31332853432887503 # 0.25 * jnp.sqrt(0.5 * jnp.pi) - _sqrt2 = 0.7071067811865476 # jnp.sqrt(0.5) + sqrt2 = 0.7071067811865476 # jnp.sqrt(0.5) # Erf function evaluations - ep = erf((1 + 1j) * _sqrt2 * z) - em = erf((1 - 1j) * _sqrt2 * z) + ep = erf((1 + 1j) * sqrt2 * z) + em = erf((1 - 1j) * sqrt2 * z) s = sqrtpi_2_4 * (1 + 1j) * (ep - 1j * em) c = sqrtpi_2_4 * (1 - 1j) * (ep + 1j * em) diff --git a/docs/source/conf.py b/docs/source/conf.py index 1fc566a0..47f1ea09 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -8,8 +8,11 @@ # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information import inspect +import operator import os +import sys from datetime import date +from pathlib import Path from typing import Any from docutils import nodes @@ -25,6 +28,9 @@ copyright = f"2023-{date.today().year}, Jérome Eertmans" # noqa: A001, DTZ011 author = "Jérome Eertmans" version = __version__ +git_ref = os.environ.get("READTHEDOCS_GIT_IDENTIFIER", "main") +conf_dir = Path(__file__).absolute().parent +root_dir = conf_dir.parent.parent # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration @@ -35,9 +41,9 @@ "sphinx.ext.autosummary", "sphinx.ext.intersphinx", "sphinx.ext.githubpages", + "sphinx.ext.linkcode", "sphinx.ext.mathjax", "sphinx.ext.napoleon", - "sphinx.ext.viewcode", # Additional "matplotlib.sphinxext.plot_directive", "myst_nb", @@ -160,16 +166,51 @@ plotly_html_show_source_link = False plotly_html_show_formats = False +# -- Linkcode settings + + +def linkcode_resolve(domain: str, info: dict[str, Any]) -> str | None: # noqa: PLR0911 + if domain != "py": + return None + + if not info["module"]: + return None + if not info["fullname"]: + return None + if info["module"].split(".", 1)[0] not in {"differt", "differt_core"}: + return None + + try: + mod = sys.modules.get(info["module"]) + obj = operator.attrgetter(info["fullname"])(mod) + if isinstance(obj, property): + obj: Any = obj.fget + obj = inspect.unwrap(obj) + filename = inspect.getsourcefile(obj) + source, lineno = inspect.getsourcelines(obj) + except (AttributeError, TypeError): + return None + + if filename is None: + return None + + filename = os.path.relpath(filename, start=root_dir) + lines = f"#L{lineno}-L{lineno + len(source) - 1}" if lineno else "" + + return f"https://github.com/jeertmans/DiffeRT/blob/{git_ref}/{filename}{lines}" + + # -- Options for HTML output ------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output + html_theme = "sphinx_book_theme" html_static_path = ["_static"] html_theme_options = { "show_toc_level": 2, "repository_url": "https://github.com/jeertmans/DiffeRT", - "repository_branch": "main", + "repository_branch": git_ref, "path_to_docs": "docs/source", "use_edit_page_button": True, "use_source_button": True,