Skip to main content

Link to source code and validate references in documentation

Project description

https://travis-ci.org/wildfish/sphinx-gitref.svg?branch=master https://codecov.io/gh/wildfish/sphinx-gitref/branch/master/graph/badge.svg

Adds a :gitref:`..` role to sphinx to link to your code on GitHub, GitLab or Bitbucket, and to make sure the references in your docs match the code.

Key features:

  • Check code references are up to date

  • Link to source code on github

  • Incorporate into tests or git hooks

Supports Python 3.6+

Installation

Install:

pip install sphinx-gitref

Modify your Sphinx conf.py:

  1. Add sphinx_gitref to the extensions list in your Sphinx conf.py:

    extensions = [
        ...
        'sphinx_gitref',
    ]
  2. Optional: Explicitly specify the remote URL.

    Gitref will try to detect your remote origin URL from the .git dir in your docs’ parent dir. If it can’t find it, or detects the wrong remote, you can set or override the remote URL explicitly with:

    gitref_remote_url = "https://github.com/username/repository.git"
  3. Optional: Explicitly specify the branch to link to.

    Gitref will try to detect your current branch from the .git dir in your docs’ parent dir. If it can’t find it, or you’d like it to use a different branch, you can set or override it explicitly with:

    gitref_branch = "master"

Usage

The :gitref:`..` role supports the following features:

  • :gitref:`path/to/filename`

  • :gitref:`path/to/filename.py::coderef`

You can optionally use them with a text label:

  • :gitref:`text <path/to/filename>`

  • :gitref:`text <path/to/filename.py::coderef>`

where coderef is a Python class, function or variable. You can also refer to class attributes as you would in python, eg MyClass.attribute.

These will be replaced by a link to the code.

If you do not provide a coderef, gitref will just check that the file exists.

Where you provide a coderef, gitref will check that an object with that name exists in the code, and will add its line number to the link.

Examples

Link to a file on gitref:

This file is :gitref:`README.rst`
For more information, see the :gitref:`project README <README.rst>`

Link to a variable, function or class in a python file:

The method which turns a reference into a line number
is :gitref:`sphinx_python/parse.py::python_to_lineno` -
this will raise a warning if the reference is not found.

Reference class attributes as you would in Python, such
as :gitref:`sphinx_python/git.py::Repo.path`.

Using in tests

Because sphinx-gitref integrates into Sphinx, you can test the references are valid by performing a test build of your documentation.

Custom remotes

If your code is stored somewhere other than one of the supported platforms, you can add a custom remote by subclassing sphinx_github.remotes.Remote in your Sphinx conf.py; for example:

from sphinx_github.remotes import Remote
class Gitea(Remote):
    remote_match = re.compile(r"^git@gitea.example.com:(?P<repo>.+?)\.git$")
    url_pattern = "https://gitea.example.com/{repo}/blob/{branch}/{filename}{line}"
    url_pattern_line = "#L{line}"

Contributing

Contributions are welcome by pull request.

They will be merged more quickly if they are provided with unit tests; to run tests locally with tox:

pip install tox
tox

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinx-gitref-0.0.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sphinx_gitref-0.0.1-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file sphinx-gitref-0.0.1.tar.gz.

File metadata

  • Download URL: sphinx-gitref-0.0.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.9

File hashes

Hashes for sphinx-gitref-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4aae21c99b4185705d286f64dd6abff4158891e85c4752e33724db70971839cd
MD5 f926892b3b6255a696cda65d315fbaa8
BLAKE2b-256 995fad74b9cc8d0f9f21e2a7028e078d04ed882432682ecba17297119ec939f8

See more details on using hashes here.

File details

Details for the file sphinx_gitref-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: sphinx_gitref-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.9

File hashes

Hashes for sphinx_gitref-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4978dc860394230495ce8cece5bfc21ddd49c0cf6bf720189a96017899d071f0
MD5 fa3debdc9f8c595ea50f9cfe3dc6cae2
BLAKE2b-256 7260dc1c7766ee5960942d8e06a2eb2490912fb06653ae1a8ff6a64ec79d2098

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page