Giter Site home page Giter Site logo

sphinx-api-relink's Introduction

sphinx-api-relink

PyPI package Supported Python versions BSD 3-Clause license Open in Visual Studio Code CI status Checked with mypy pre-commit.ci status Spelling checked code style: prettier Ruff

This package is a plugin for the sphinx.ext.autodoc extension. The autodoc_type_aliases configuration does not always work well when using postponed evaluation of annotations (PEP 563, i.e. from __future__ import annotations) or when importing through typing.TYPE_CHECKING, because sphinx.ext.autodoc generates the API dynamically (not statically, as opposed to sphinx-autoapi).

Installation

Just install through PyPI with pip:

pip install sphinx-api-relink

Next, in your Sphinx configuration file (conf.py), add "sphinx_api_relink" to your extensions:

extensions = [
    "sphinx_api_relink",
]

Usage

There are two ways to relink type hint references in your API. The first one, api_target_substitutions, should be used when the target is different than the type hint itself:

api_target_substitutions: dict[str, str | tuple[str, str]] = {
    "sp.Expr": "sympy.core.expr.Expr",
    "Protocol": ("obj", "typing.Protocol"),
}

The second, api_target_types, is useful when you want to redirect the reference type. This is for instance useful when Sphinx thinks the reference is a class, but it should be an obj:

api_target_types: dict[str, str] = {
    "RangeDefinition": "obj",
}

The extension can also link to the source code on GitHub through the sphinx.ext.linkcode extension. To activate, specify the GitHub organization and the repository name as follows:

api_github_repo: str = "ComPWA/sphinx-api-relink"

Set api_linkcode_debug = True to print the generated URLs to the console.

Generate API

To generate the API for sphinx.ext.autodoc, add this to your conf.py:

generate_apidoc_package_path = "../src/my_package"  # relative to conf.py

Multiple packages can be listed as well:

generate_apidoc_package_path = [
    "../src/package1",
    "../src/package2",
]

The API is generated with the same style used by the ComPWA repositories (see e.g. ampform.rtfd.io/en/stable/api/ampform.html). To use the default template, set:

generate_apidoc_use_compwa_template = False

Other configuration values (with their defaults):

generate_apidoc_directory = "api"
generate_apidoc_excludes = ["version.py"]

sphinx-api-relink's People

Contributors

pre-commit-ci[bot] avatar redeboer avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

redeboer

sphinx-api-relink's Issues

"Suggest edit" button broken

image

The "Suggest edit" button is broken if the ref used to point to the source code is a commit. This is because you cannot edit files on GitHub if you are not on a branch.
image

Add option to skip URL-check in `linkcode_resolve`

If api_github_repo is set in conf.py, sphinx-api-relink will check if the current SHA or default branch exists on the GitHub repository. For this, it needs access to the repository and this is not the case if the repo is private. We therefore need an option like:

api_skip_fallback: bool = False

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.