Skip to content
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

add sphinx extension to generate a list of all contributors #1

Merged
merged 20 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the OS, Python version and other tools you might need
build:
os: ubuntu-24.04
os: ubuntu-22.04
tools:
python: "3.12"
# You can also specify other tool versions:
# nodejs: "19"
# rust: "1.64"
# golang: "1.19"

# Build documentation in the "docs/" directory with Sphinx
sphinx:
configuration: docs/conf.py
# Optionally build your docs in additional formats such as PDF and ePub
# formats:
# - pdf
# - epub

# Optional but recommended, declare the Python requirements required
# to build your documentation
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
install:
- requirements: requirements.txt
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# sphinx-all-contributors

[![All Contributors](https://img.shields.io/github/all-contributors/tkoyama010/sphinx-all-contributors?color=ee8449)](https://sphinx-all-contributors.readthedocs.io/en/latest/reference/about.html#contributors)
[![Documentation Status](https://readthedocs.org/projects/sphinx-all-contributors/badge/?version=latest)](https://sphinx-all-contributors.readthedocs.io/en/latest/?badge=latest)
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -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 = .
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)
27 changes: 27 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Configuration file for the Sphinx documentation builder. # noqa: INP001, D100
#
# 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

project = "sphinx-all-contributors"
copyright = "2024, Tetsuo Koyama" # noqa: A001
author = "Tetsuo Koyama"
release = "0.1.dev0"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = ["sphinx_all_contributors"]

templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "alabaster"
html_static_path = ["_static"]
4 changes: 4 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sphinx-all-contributors documentation
=====================================

.. all-contributors:: ../.all-contributorsrc
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
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
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Automation tasks for Nox."""
22 changes: 12 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
[build-system]
requires = ["scikit-build-core"]
build-backend = "scikit_build_core.build"
requires = ['setuptools<69.0.4']
build-backend = 'setuptools.build_meta'

[project]
name = "sphinx-all-contributors"
requires-python = '>=3.12'
version = "0.1.dev0"
dependencies = [
'poetry-core>=1.0.0'
]

[tool.mypy]
ignore_missing_imports = true
Expand All @@ -18,12 +26,6 @@ testpaths = [
"tests"
]

[tool.ruff]
target-version = "py312"

[tool.ruff.lint]
extend-select = [
"B", # flake8-bugbear
"I", # isort
"UP" # pyupgrade
]
extend-select = ["ALL"]
ignore = ["COM812", "D203", "D212", "ISC001"]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-e .
15 changes: 15 additions & 0 deletions src/sphinx_all_contributors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Sphinx extension for All Contributors."""

from sphinx.application import Sphinx

from .directive import AllContributorsDirective


def setup(app: Sphinx) -> dict[str, str | bool]:
"""Set up the extension."""
app.add_directive("all-contributors", AllContributorsDirective)
return {
"version": "0.1.dev0",
"parallel_read_safe": True,
"parallel_write_safe": True,
}
49 changes: 49 additions & 0 deletions src/sphinx_all_contributors/directive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Directive to include a list of contributors from a JSON file."""

import json
from pathlib import Path

from docutils import nodes # type: ignore[import-untyped]
from docutils.parsers.rst import Directive # type: ignore[import-untyped]


class AllContributorsDirective(Directive): # type: ignore[misc]
"""Directive to include a list of contributors from a JSON file."""

# Allow one optional argument for the relative path
required_arguments = 0
optional_arguments = 1
has_content = False

def run(self) -> list[nodes.Node]:
"""Return a list of nodes to insert into the document."""
env = self.state.document.settings.env
srcdir = Path(env.srcdir)

# Get the path from the argument or default to .all-contributorsrc
relative_path = self.arguments[0] if self.arguments else ".all-contributorsrc"
contributors_file = srcdir / relative_path

if not Path(contributors_file).exists():
return [
nodes.error(
None,
nodes.paragraph(text=f"Error: {relative_path} file not found."),
),
]

with Path(contributors_file).open(encoding="utf-8") as f:
all_contributors = json.load(f)

contributor_nodes = []

for contributor in all_contributors.get("contributors", []):
name = contributor.get("name", "Unknown Contributor")
contributions = ", ".join(contributor.get("contributions", []))
line = f"- {name} for {contributions}"

# Create a new paragraph node for each contributor
paragraph_node = nodes.paragraph(text=line)
contributor_nodes.append(paragraph_node)

return contributor_nodes