From 76944d1829d57afa6b9b4f7e854f905d75e0c325 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 26 May 2025 09:45:58 +0200 Subject: [PATCH] docs added --- .github/workflows/docs.yml | 40 +++++++++++++++++++ .gitignore | 5 ++- README.md | 2 +- docs/Makefile | 20 ++++++++++ docs/make.bat | 35 +++++++++++++++++ docs/source/conf.py | 32 +++++++++++++++ docs/source/generate_class_list.py | 61 +++++++++++++++++++++++++++++ docs/source/index.md | 10 +++++ docs/source/readme.md | 2 + {docs => media}/output_example.png | Bin pyproject.toml | 1 + 11 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/docs.yml 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/readme.md rename {docs => media}/output_example.png (100%) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 0000000..31e17f2 --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,40 @@ +name: Build and Deploy Docs + +on: + push: + branches: + - main + +permissions: + contents: write + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: "3.x" + - name: Install dependencies + run: pip install sphinx sphinx_rtd_theme sphinx-autodoc-typehints myst-parser + - name: Generate Class List + run: | + pip install . + python ./docs/source/generate_class_list.py + - name: Build Docs + run: | + cd docs + sphinx-apidoc -o source/ ../src/ -M --no-toc + rm source/*.rst + make html + touch build/html/.nojekyll + mkdir -p build/html/media + cp ../media/output_example.png build/html/media/ + + - name: Deploy to GitHub Pages + uses: JamesIves/github-pages-deploy-action@v4 + with: + branch: gh-pages + folder: docs/build/html diff --git a/.gitignore b/.gitignore index 373d2df..a5843fb 100644 --- a/.gitignore +++ b/.gitignore @@ -131,4 +131,7 @@ cython_debug/ pyModbusTCP_old/ test.py test_*.ipynb -settings.json \ No newline at end of file +settings.json + +# Autogenerated doc file +docs/source/modules.md diff --git a/README.md b/README.md index 8c5df03..57955f6 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ is a hard problem where LaTeX is superior. ## Example outputs -[![example output](docs/output_example.png)](https://raw.githubusercontent.com/Nonannet/pyladoc/refs/heads/main/tests/out/test_latex_render1.pdf) +[![example output](media/output_example.png)](https://raw.githubusercontent.com/Nonannet/pyladoc/refs/heads/main/tests/out/test_latex_render1.pdf) - HTML: [test_html_render1.html](https://html-preview.github.io/?url=https://github.com/Nonannet/pyladoc/blob/main/tests/out/test_html_render1.html) ([code](https://github.com/Nonannet/pyladoc/blob/main/tests/out/test_html_render1.html)) - PDF: [test_latex_render1.pdf](https://raw.githubusercontent.com/Nonannet/pyladoc/refs/heads/main/tests/out/test_latex_render1.pdf) ([code](https://github.com/Nonannet/pyladoc/blob/main/tests/out/test_html_render1.tex)) 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..14c537d --- /dev/null +++ b/docs/source/conf.py @@ -0,0 +1,32 @@ +# 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 = 'pyladoc' +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 = 'sphinx_rtd_theme' +html_static_path = ['_static'] + +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..c7306dc --- /dev/null +++ b/docs/source/generate_class_list.py @@ -0,0 +1,61 @@ +import importlib +import inspect +import fnmatch +from io import TextIOWrapper + + +def write_classes(f: TextIOWrapper, patterns: list[str], module_name: str, title: str, description: str = '', exclude: list[str] = []) -> None: + + module = importlib.import_module(module_name) + + classes = [ + name for name, obj in inspect.getmembers(module, inspect.isclass) + if (obj.__module__ == module_name and + 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__) + ] + + """Write the classes to the file.""" + f.write(f'## {title}\n\n') + if description: + f.write(f'{description}\n\n') + + for cls in classes: + f.write('```{eval-rst}\n') + f.write(f'.. autoclass:: {module_name}.{cls}\n') + f.write(' :members:\n') + f.write(' :undoc-members:\n') + f.write(' :show-inheritance:\n') + f.write(' :inherited-members:\n') + if title != 'Base classes': + f.write(' :exclude-members: select\n') + f.write('```\n\n') + + +def write_functions(f: TextIOWrapper, patterns: list[str], module_name: str, title: str, description: str = '', exclude: list[str] = []) -> None: + + module = importlib.import_module(module_name) + + classes = [ + name for name, obj in inspect.getmembers(module, inspect.isfunction) + if (obj.__module__ == module_name and + any(fnmatch.fnmatch(name, pat) for pat in patterns if pat not in exclude)) + ] + + """Write the classes to the file.""" + f.write(f'## {title}\n\n') + if description: + f.write(f'{description}\n\n') + + for func in classes: + if not func.startswith('_'): + f.write('```{eval-rst}\n') + f.write(f'.. autofunction:: {module_name}.{func}\n') + f.write('```\n\n') + + +with open('docs/source/modules.md', 'w') as f: + f.write('# Pyladoc classes, functions and submodules\n\n') + write_classes(f, ['DocumentWriter'], 'pyladoc', title='DocumentWriter Class') + write_functions(f, ['*'], 'pyladoc', title='Functions') + write_functions(f, ['*'], 'pyladoc.latex', title='Submodule latex') diff --git a/docs/source/index.md b/docs/source/index.md new file mode 100644 index 0000000..7232e27 --- /dev/null +++ b/docs/source/index.md @@ -0,0 +1,10 @@ +```{toctree} +:maxdepth: 2 +:caption: Contents: + +readme +modules +``` + +```{include} ../../README.md +``` \ No newline at end of file diff --git a/docs/source/readme.md b/docs/source/readme.md new file mode 100644 index 0000000..060259b --- /dev/null +++ b/docs/source/readme.md @@ -0,0 +1,2 @@ +```{include} ../../README.md +``` \ No newline at end of file diff --git a/docs/output_example.png b/media/output_example.png similarity index 100% rename from docs/output_example.png rename to media/output_example.png diff --git a/pyproject.toml b/pyproject.toml index b0901e2..8b90b97 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,6 +29,7 @@ dev = [ Homepage = "https://github.com/Nonannet/pyladoc" Repository = "https://github.com/Nonannet/pyladoc" Issues = "https://github.com/Nonannet/pyladoc/issues" +documentation = "https://nonannet.github.io/pyladoc/" [build-system] requires = ["setuptools>=61.0", "wheel"]