-
Notifications
You must be signed in to change notification settings - Fork 17
docs: Add sphinx API docs #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
crandmck
wants to merge
16
commits into
main
Choose a base branch
from
add-sphinx-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2b06fa4
Initial try for API docs generated by Sphinx
crandmck 517dfb6
Add .gitignore to _build dir
crandmck 1cd0104
Remove _build dir
crandmck 8a01dd7
Add generated doc files to .gitignore
crandmck 60ee542
Fix .gitignore
crandmck 45a9bed
Remove non-API docs from Sphinx docs
crandmck 2c2f5b6
Remove docs/_static/.gitkeep
crandmck 2e71c55
Move API docs to api-docs directory, update code comments and enable …
crandmck a3d4b30
Address Tania comments
crandmck e4f9fa3
Add publish workflow
crandmck 0132ed1
Fix workflow per Tania comments
crandmck faa45a7
Update wf with install dev deps and remove nx command
crandmck bb331fc
Remove push to main as wf trigger, change pages to github-pages
crandmck 0ce5e5c
Change path where docs are built
crandmck c2daaeb
docs: Debug workflow (#169)
tmathern 2740beb
Merge branch 'main' into add-sphinx-docs
tmathern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
name: Build and publish docs | ||
|
||
on: | ||
release: | ||
types: [published] | ||
|
||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: read | ||
pages: write | ||
id-token: write | ||
|
||
concurrency: | ||
group: 'github-pages' | ||
cancel-in-progress: false | ||
|
||
jobs: | ||
deploy: | ||
environment: | ||
name: github-pages | ||
url: ${{ steps.deployment.outputs.page_url }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
filter: tree:0 | ||
fetch-depth: 0 | ||
|
||
- name: Setup Pages | ||
uses: actions/configure-pages@v5 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.10" | ||
cache: "pip" | ||
|
||
- name: Install dev deps | ||
run: python3 -m pip install -r requirements-dev.txt | ||
|
||
- name: Build docs | ||
run: python3 scripts/generate_api_docs.py | ||
|
||
- name: Upload artifact | ||
uses: actions/upload-pages-artifact@v3 | ||
with: | ||
path: 'api-docs/_build/html/' | ||
|
||
- name: Deploy to GitHub Pages | ||
id: deployment | ||
uses: actions/deploy-pages@v4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
# -- Project information ----------------------------------------------------- | ||
|
||
project = "c2pa-python" | ||
author = "Content Authenticity Initiative (CAI)" | ||
|
||
# -- General configuration --------------------------------------------------- | ||
|
||
extensions = [ | ||
"myst_parser", | ||
"autoapi.extension", | ||
"sphinx.ext.napoleon", | ||
] | ||
|
||
myst_enable_extensions = [ | ||
"colon_fence", | ||
"deflist", | ||
"fieldlist", | ||
"strikethrough", | ||
"tasklist", | ||
"attrs_block", | ||
"attrs_inline", | ||
] | ||
|
||
templates_path = ["_templates"] | ||
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] | ||
|
||
# -- AutoAPI configuration --------------------------------------------------- | ||
|
||
project_root = Path(__file__).resolve().parents[1] | ||
autoapi_type = "python" | ||
# Allow overriding the source path used by AutoAPI (for preprocessing) | ||
autoapi_dirs = [ | ||
os.environ.get( | ||
"C2PA_DOCS_SRC", | ||
str(project_root / "src" / "c2pa"), | ||
) | ||
] | ||
autoapi_root = "api" | ||
autoapi_keep_files = True | ||
autoapi_add_toctree_entry = True | ||
autoapi_options = [ | ||
"members", | ||
"undoc-members", | ||
"show-inheritance", | ||
"show-module-summary", | ||
"imported-members", | ||
] | ||
|
||
# -- Options for HTML output ------------------------------------------------- | ||
|
||
html_theme = "furo" | ||
html_static_path = ["_static"] | ||
|
||
# Avoid executing package imports during docs build | ||
autodoc_typehints = "description" | ||
|
||
# Napoleon (Google/Numpy docstring support) | ||
napoleon_google_docstring = True | ||
napoleon_numpy_docstring = False | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.. c2pa-python documentation master file | ||
Welcome to c2pa-python's documentation! | ||
======================================= | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
:caption: Contents: | ||
|
||
api/index | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Generate API documentation using Sphinx + AutoAPI. | ||
|
||
This script builds HTML docs into docs/_build/html. | ||
It avoids importing the package by relying on sphinx-autoapi | ||
to parse source files directly. | ||
""" | ||
|
||
import shutil | ||
import os | ||
import sys | ||
from pathlib import Path | ||
import importlib | ||
|
||
|
||
def ensure_tools_available() -> None: | ||
try: | ||
importlib.import_module("sphinx") | ||
importlib.import_module("autoapi") | ||
importlib.import_module("myst_parser") | ||
except Exception as exc: | ||
root = Path(__file__).resolve().parents[1] | ||
req = root / "requirements-dev.txt" | ||
print( | ||
"Missing documentation dependencies. " | ||
f"Install with: python3 -m pip install -r {req}", | ||
file=sys.stderr, | ||
) | ||
raise SystemExit(1) from exc | ||
|
||
|
||
def build_docs() -> None: | ||
root = Path(__file__).resolve().parents[1] | ||
docs_dir = root / "api-docs" | ||
build_dir = docs_dir / "_build" / "html" | ||
api_dir = docs_dir / "api" | ||
|
||
# Preprocess sources: convert Markdown code fences in docstrings to reST | ||
src_pkg_dir = root / "src" / "c2pa" | ||
pre_dir = docs_dir / "_preprocessed" | ||
pre_pkg_dir = pre_dir / "c2pa" | ||
if pre_dir.exists(): | ||
shutil.rmtree(pre_dir) | ||
pre_pkg_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
def convert_fences_to_rst(text: str) -> str: | ||
lines = text.splitlines() | ||
out: list[str] = [] | ||
i = 0 | ||
while i < len(lines): | ||
line = lines[i] | ||
stripped = line.lstrip() | ||
indent = line[: len(line) - len(stripped)] | ||
if stripped.startswith("```"): | ||
fence = stripped | ||
lang = fence[3:].strip() or "text" | ||
# Start directive | ||
out.append(f"{indent}.. code-block:: {lang}") | ||
out.append("") | ||
i += 1 | ||
# Emit indented code until closing fence | ||
while i < len(lines): | ||
l2 = lines[i] | ||
if l2.lstrip().startswith("```"): | ||
i += 1 | ||
break | ||
out.append(f"{indent} {l2}") | ||
i += 1 | ||
continue | ||
out.append(line) | ||
i += 1 | ||
return "\n".join(out) + ("\n" if text.endswith("\n") else "") | ||
|
||
for src_path in src_pkg_dir.rglob("*.py"): | ||
rel = src_path.relative_to(src_pkg_dir) | ||
dest = pre_pkg_dir / rel | ||
dest.parent.mkdir(parents=True, exist_ok=True) | ||
content = src_path.read_text(encoding="utf-8") | ||
dest.write_text(convert_fences_to_rst(content), encoding="utf-8") | ||
|
||
# Point AutoAPI to preprocessed sources | ||
os.environ["C2PA_DOCS_SRC"] = str(pre_pkg_dir) | ||
|
||
# Clean AutoAPI output to avoid stale pages | ||
if api_dir.exists(): | ||
shutil.rmtree(api_dir) | ||
|
||
build_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
try: | ||
sphinx_build_mod = importlib.import_module("sphinx.cmd.build") | ||
sphinx_main = getattr(sphinx_build_mod, "main") | ||
code = sphinx_main([ | ||
"-b", | ||
"html", | ||
str(docs_dir), | ||
str(build_dir), | ||
]) | ||
if code != 0: | ||
raise SystemExit(code) | ||
except Exception: | ||
# Fallback to subprocess if needed | ||
import subprocess | ||
|
||
cmd = [ | ||
sys.executable, | ||
"-m", | ||
"sphinx", | ||
"-b", | ||
"html", | ||
str(docs_dir), | ||
str(build_dir), | ||
] | ||
subprocess.run(cmd, check=True) | ||
|
||
print(f"API docs generated at: {build_dir}") | ||
|
||
|
||
if __name__ == "__main__": | ||
ensure_tools_available() | ||
build_docs() | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.