|
| 1 | +# Sphinx Gitref |
| 2 | + |
| 3 | + |
| 4 | +[](https://pypi.org/project/sphinx-gitref/) |
| 5 | +[](https://github.com/radiac/sphinx-gitref/actions/workflows/ci.yml) |
| 6 | +[](https://codecov.io/gh/radiac/sphinx-gitref) |
| 7 | + |
| 8 | +Adds a `` :gitref:`..` `` role to sphinx to link to your code on GitHub, GitLab or |
| 9 | +Bitbucket, and to make sure the references in your docs match the code. |
| 10 | + |
| 11 | +Key features: |
| 12 | + |
| 13 | +* Check code references are up to date |
| 14 | +* Link to source code on github |
| 15 | +* Incorporate into tests or git hooks |
| 16 | + |
| 17 | +Supports Python 3.6+ |
| 18 | + |
| 19 | + |
| 20 | +Installation |
| 21 | +============ |
| 22 | + |
| 23 | +Install:: |
| 24 | + |
| 25 | + pip install sphinx-gitref |
| 26 | + |
| 27 | + |
| 28 | +Modify your Sphinx ``conf.py``: |
| 29 | + |
| 30 | +1. Add ``sphinx_gitref`` to the ``extensions`` list in your Sphinx ``conf.py``: |
| 31 | + |
| 32 | + |
| 33 | + ```python |
| 34 | + extensions = [ |
| 35 | + ... |
| 36 | + 'sphinx_gitref', |
| 37 | + ] |
| 38 | + ``` |
| 39 | + |
| 40 | +2. Optional: Explicitly specify the remote URL. |
| 41 | + |
| 42 | + Gitref will try to detect your remote origin URL from the ``.git`` dir in your docs' |
| 43 | + parent dir. If it can't find it, or detects the wrong remote, you can set or override |
| 44 | + the remote URL explicitly with:: |
| 45 | + |
| 46 | + ```python |
| 47 | + gitref_remote_url = "https://github.com/username/repository.git" |
| 48 | + ``` |
| 49 | + |
| 50 | +3. Optional: Explicitly specify the branch to link to. |
| 51 | + |
| 52 | + Gitref will try to detect your current branch from the ``.git`` dir in your docs' |
| 53 | + parent dir. If it can't find it, or you'd like it to use a different branch, you can |
| 54 | + set or override it explicitly with:: |
| 55 | + |
| 56 | + ```python |
| 57 | + gitref_branch = "master" |
| 58 | + ``` |
| 59 | + |
| 60 | +4. Optional: Change the link label format when a coderef is provided without an |
| 61 | + explicit label, eg `` :gitref:`filename.py::coderef` `` |
| 62 | + |
| 63 | + Gitref defaults to using showing the coderef and dropping the filename. This can be |
| 64 | + overridden by setting a format string:: |
| 65 | + |
| 66 | + ```python |
| 67 | + gitref_label_format = "{filename} {coderef}" |
| 68 | + ``` |
| 69 | + |
| 70 | +## Usage |
| 71 | + |
| 72 | +The `` :gitref:`..` `` role supports the following features: |
| 73 | + |
| 74 | +* `` :gitref:`path/to/filename` `` |
| 75 | +* `` :gitref:`path/to/filename.py::coderef` `` |
| 76 | + |
| 77 | +You can optionally use them with a text label: |
| 78 | + |
| 79 | +* `` :gitref:`text <path/to/filename>` `` |
| 80 | +* `` :gitref:`text <path/to/filename.py::coderef>` `` |
| 81 | + |
| 82 | +where ``coderef`` is a Python class, function or variable. You can also refer to class |
| 83 | +attributes as you would in python, eg ``MyClass.attribute``. |
| 84 | + |
| 85 | +These will be replaced by a link to the code. |
| 86 | + |
| 87 | +If you do not provide a ``coderef``, gitref will just check that the file exists. |
| 88 | + |
| 89 | +Where you provide a ``coderef``, gitref will check that an object with that name exists |
| 90 | +in the code, and will add its line number to the link. |
| 91 | + |
| 92 | + |
| 93 | +### Examples |
| 94 | + |
| 95 | +Link to a file on gitref:: |
| 96 | + |
| 97 | +``` |
| 98 | +This file is :gitref:`README.rst` |
| 99 | +For more information, see the :gitref:`project README <README.rst>` |
| 100 | +``` |
| 101 | + |
| 102 | +Link to a variable, function or class in a python file:: |
| 103 | + |
| 104 | +``` |
| 105 | +The method which turns a reference into a line number |
| 106 | +is :gitref:`sphinx_python/parse.py::python_to_lineno` - |
| 107 | +this will raise a warning if the reference is not found. |
| 108 | +
|
| 109 | +Reference class attributes as you would in Python, such |
| 110 | +as :gitref:`sphinx_python/git.py::Repo.path`. |
| 111 | +``` |
| 112 | + |
| 113 | +### Using in tests |
| 114 | + |
| 115 | +Because ``sphinx-gitref`` integrates into Sphinx, you can test the references are valid |
| 116 | +by performing a test build of your documentation. |
| 117 | + |
| 118 | + |
| 119 | +### Custom remotes |
| 120 | + |
| 121 | +If your code is stored somewhere other than one of the supported platforms, you can add |
| 122 | +a custom remote by subclassing ``sphinx_github.remotes.Remote`` in your Sphinx |
| 123 | +``conf.py``; for example:: |
| 124 | + |
| 125 | +```python |
| 126 | +from sphinx_github.remotes import Remote |
| 127 | +class Gitea(Remote): |
| 128 | + remote_match = re.compile(r"^git@gitea.example.com:(?P<repo>.+?)\.git$") |
| 129 | + url_pattern = "https://gitea.example.com/{repo}/blob/{branch}/{filename}{line}" |
| 130 | + url_pattern_line = "#L{line}" |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | +## Changelog |
| 135 | + |
| 136 | +0.3.0 - 2024-05-19 |
| 137 | +* Support latest Sphinx |
| 138 | + |
| 139 | +0.2.1 - 2022-02-19 |
| 140 | +* Improve repository pattern matching |
| 141 | + |
| 142 | +0.2.0 - 2022-02-13 |
| 143 | +* Add custom label formatting with ``gitref_label_format`` |
| 144 | +* Fix bug when node target has no id |
| 145 | +* Improve branch detection to support a recently detached ``HEAD`` |
| 146 | + |
| 147 | +0.1.0 - 2020-04-18 |
| 148 | +* Initial release |
0 commit comments