From 2cb549929951be4ea3a26e802485850be542f91a Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 6 Dec 2025 18:11:42 +0100 Subject: [PATCH] sphinx based docs added --- .gitignore | 2 + docs/Makefile | 20 +++++++ docs/make.bat | 35 ++++++++++++ docs/source/conf.py | 37 +++++++++++++ docs/source/generate_class_list.py | 86 ++++++++++++++++++++++++++++++ docs/source/index.md | 9 ++++ docs/source/repo.md | 3 ++ pyproject.toml | 6 +++ 8 files changed, 198 insertions(+) create mode 100644 docs/Makefile create mode 100644 docs/make.bat create mode 100644 docs/source/conf.py create mode 100644 docs/source/generate_class_list.py create mode 100644 docs/source/index.md create mode 100644 docs/source/repo.md diff --git a/.gitignore b/.gitignore index f6a7942..bdd5a7a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ build/* /src/*.pyd vc140.pdb benchmark_results* +docs/build +docs/source/api \ No newline at end of file diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..d0c3cbf --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = source +BUILDDIR = build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/make.bat b/docs/make.bat new file mode 100644 index 0000000..dc1312a --- /dev/null +++ b/docs/make.bat @@ -0,0 +1,35 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set SOURCEDIR=source +set BUILDDIR=build + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.https://www.sphinx-doc.org/ + exit /b 1 +) + +if "%1" == "" goto help + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/docs/source/conf.py b/docs/source/conf.py new file mode 100644 index 0000000..55a33b6 --- /dev/null +++ b/docs/source/conf.py @@ -0,0 +1,37 @@ +# Configuration file for the Sphinx documentation builder. +# +# For the full list of built-in configuration values, see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Project information ----------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information + +import os +import sys +sys.path.insert(0, os.path.abspath("../src/")) + +project = 'copapy' +copyright = '2025, Nicolas Kruse' +author = 'Nicolas Kruse' + +# -- General configuration --------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration + +extensions = ["sphinx.ext.autodoc", "sphinx.ext.napoleon", "sphinx_autodoc_typehints", "myst_parser"] + +templates_path = ['_templates'] +exclude_patterns = [] + +# -- Options for HTML output ------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output + +# html_theme = 'alabaster' +html_theme = 'pydata_sphinx_theme' +html_static_path = ['_static'] +html_theme_options = { + "secondary_sidebar_items": ["page-toc"], + "footer_start": ["copyright"] +} +html_theme_options["footer_end"] = [] + +autodoc_inherit_docstrings = True diff --git a/docs/source/generate_class_list.py b/docs/source/generate_class_list.py new file mode 100644 index 0000000..e69df0f --- /dev/null +++ b/docs/source/generate_class_list.py @@ -0,0 +1,86 @@ +# This script generates the source md-files for all classes and functions for the docs + +import importlib +import inspect +import fnmatch +from io import TextIOWrapper +import os + + +def write_manual(f: TextIOWrapper, doc_files: list[str], title: str) -> None: + write_dochtree(f, title, doc_files) + + +def write_classes(f: TextIOWrapper, patterns: list[str], module_name: str, title: str, description: str = '', exclude: list[str] = []) -> None: + """Write the classes to the file.""" + module = importlib.import_module(module_name) + + classes = [ + name for name, obj in inspect.getmembers(module, inspect.isclass) + if (any(fnmatch.fnmatch(name, pat) for pat in patterns if pat not in exclude) and + obj.__doc__ and '(Automatic generated stub)' not in obj.__doc__) + ] + + if description: + f.write(f'{description}\n\n') + + write_dochtree(f, title, classes) + + for cls in classes: + with open(f'docs/source/api/{cls}.md', 'w') as f2: + f2.write(f'# {module_name}.{cls}\n') + f2.write('```{eval-rst}\n') + f2.write(f'.. autoclass:: {module_name}.{cls}\n') + f2.write(' :members:\n') + f2.write(' :undoc-members:\n') + f2.write(' :show-inheritance:\n') + f2.write(' :inherited-members:\n') + f2.write('```\n\n') + + +def write_functions(f: TextIOWrapper, patterns: list[str], module_name: str, title: str, description: str = '', exclude: list[str] = []) -> None: + """Write the classes to the file.""" + module = importlib.import_module(module_name) + + functions = [ + name for name, _ in inspect.getmembers(module, inspect.isfunction) + if (any(fnmatch.fnmatch(name, pat) for pat in patterns if pat not in exclude)) + ] + + if description: + f.write(f'{description}\n\n') + + write_dochtree(f, title, functions) + + for func in functions: + if not func.startswith('_'): + with open(f'docs/source/api/{func}.md', 'w') as f2: + f2.write(f'# {module_name}.{func}\n') + f2.write('```{eval-rst}\n') + f2.write(f'.. autofunction:: {module_name}.{func}\n') + f2.write('```\n\n') + + +def write_dochtree(f: TextIOWrapper, title: str, items: list[str]): + f.write('```{toctree}\n') + f.write(':maxdepth: 1\n') + f.write(f':caption: {title}:\n') + #f.write(':hidden:\n') + for text in items: + if not text.startswith('_'): + f.write(f"{text}\n") + f.write('```\n\n') + + +if __name__ == "__main__": + # Ensure the output directory exists + os.makedirs('docs/source/api', exist_ok=True) + + with open('docs/source/api/index.md', 'w') as f: + f.write('# Classes and functions\n\n') + + write_classes(f, ['*'], 'copapy', title='Classes') + + write_functions(f, ['*'], 'copapy', title='Functions') + + #write_manual(f, ['../ndfloat', '../floatarray'], title='Types') diff --git a/docs/source/index.md b/docs/source/index.md new file mode 100644 index 0000000..b65f4c4 --- /dev/null +++ b/docs/source/index.md @@ -0,0 +1,9 @@ +```{toctree} +:maxdepth: 1 +:hidden: +api/index +repo +``` + +```{include} ../../README.md +``` \ No newline at end of file diff --git a/docs/source/repo.md b/docs/source/repo.md new file mode 100644 index 0000000..110e578 --- /dev/null +++ b/docs/source/repo.md @@ -0,0 +1,3 @@ +# Code repository + +Code repository is on GitHub: [github.com/Nonannet/copapy](https://github.com/Nonannet/copapy). \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 6108eda..0900c1a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,6 +36,12 @@ dev = [ "mypy", "pytest" ] +doc_build = [ + "sphinx", + "pydata_sphinx_theme", + "sphinx-autodoc-typehints", + "myst-parser" +] [tool.mypy] files = ["src", "tools", "stencils"]