| 1 |
====================== |
| 2 |
Contributing to Sphinx |
| 3 |
====================== |
| 4 |
|
| 5 |
There are many ways you can contribute to Sphinx, be it filing bug reports or |
| 6 |
feature requests, writing new documentation or submitting patches for new or |
| 7 |
fixed behavior. This guide serves to illustrate how you can get started with |
| 8 |
this. |
| 9 |
|
| 10 |
|
| 11 |
Get help |
| 12 |
-------- |
| 13 |
|
| 14 |
The Sphinx community maintains a number of mailing lists and IRC channels. |
| 15 |
|
| 16 |
Stack Overflow with tag `python-sphinx`_ |
| 17 |
Questions and answers about use and development. |
| 18 |
|
| 19 |
`GitHub Discussions Q&A`__ |
| 20 |
Question-and-answer style forum for discussions. |
| 21 |
|
| 22 |
__ https://github.com/orgs/sphinx-doc/discussions/categories/q-a |
| 23 |
|
| 24 |
sphinx-users <[email protected]> |
| 25 |
Mailing list for user support. |
| 26 |
|
| 27 |
sphinx-dev <[email protected]> |
| 28 |
Mailing list for development related discussions. |
| 29 |
|
| 30 |
#sphinx-doc on irc.libera.chat |
| 31 |
IRC channel for development questions and user support. |
| 32 |
|
| 33 |
.. _python-sphinx: https://stackoverflow.com/questions/tagged/python-sphinx |
| 34 |
|
| 35 |
|
| 36 |
Bug Reports and Feature Requests |
| 37 |
-------------------------------- |
| 38 |
|
| 39 |
If you have encountered a problem with Sphinx or have an idea for a new |
| 40 |
feature, please submit it to the `issue tracker`_ on GitHub. |
| 41 |
|
| 42 |
For bug reports, please include the output produced during the build process |
| 43 |
and also the log file Sphinx creates after it encounters an unhandled |
| 44 |
exception. |
| 45 |
The location of this file should be shown towards the end of the error message. |
| 46 |
Please also include the output of :program:`sphinx-build --bug-report`. |
| 47 |
|
| 48 |
Including or providing a link to the source files involved may help us fix the |
| 49 |
issue. If possible, try to create a minimal project that produces the error |
| 50 |
and post that instead. |
| 51 |
|
| 52 |
.. _`issue tracker`: https://github.com/sphinx-doc/sphinx/issues |
| 53 |
|
| 54 |
|
| 55 |
Contribute code |
| 56 |
--------------- |
| 57 |
|
| 58 |
The Sphinx source code is managed using Git and is `hosted on GitHub`_. The |
| 59 |
recommended way for new contributors to submit code to Sphinx is to fork this |
| 60 |
repository and submit a pull request after committing changes to their fork. |
| 61 |
The pull request will then need to be approved by one of the core developers |
| 62 |
before it is merged into the main repository. |
| 63 |
|
| 64 |
.. _hosted on GitHub: https://github.com/sphinx-doc/sphinx |
| 65 |
|
| 66 |
|
| 67 |
.. _contribute-get-started: |
| 68 |
|
| 69 |
Getting started |
| 70 |
~~~~~~~~~~~~~~~ |
| 71 |
|
| 72 |
Before starting on a patch, we recommend checking for open issues |
| 73 |
or opening a fresh issue to start a discussion around a feature idea or a bug. |
| 74 |
If you feel uncomfortable or uncertain about an issue or your changes, |
| 75 |
feel free to `start a discussion`_. |
| 76 |
|
| 77 |
.. _start a discussion: https://github.com/orgs/sphinx-doc/discussions/ |
| 78 |
|
| 79 |
These are the basic steps needed to start developing on Sphinx. |
| 80 |
|
| 81 |
#. Create an account on GitHub. |
| 82 |
|
| 83 |
#. Fork_ the main Sphinx repository (`sphinx-doc/sphinx`_) |
| 84 |
using the GitHub interface. |
| 85 |
|
| 86 |
.. _Fork: https://github.com/sphinx-doc/sphinx/fork |
| 87 |
.. _sphinx-doc/sphinx: https://github.com/sphinx-doc/sphinx |
| 88 |
|
| 89 |
#. Clone the forked repository to your machine. |
| 90 |
|
| 91 |
.. code-block:: shell |
| 92 |
|
| 93 |
git clone https://github.com/<USERNAME>/sphinx |
| 94 |
cd sphinx |
| 95 |
|
| 96 |
#. Install uv and set up your environment. |
| 97 |
|
| 98 |
We recommend using :program:`uv` for dependency management. |
| 99 |
Install it with: |
| 100 |
|
| 101 |
.. code-block:: shell |
| 102 |
|
| 103 |
python -m pip install -U uv |
| 104 |
|
| 105 |
Then, set up your environment: |
| 106 |
|
| 107 |
.. code-block:: shell |
| 108 |
|
| 109 |
uv sync |
| 110 |
|
| 111 |
**Alternative:** If you prefer not to use :program:`uv`, you can use |
| 112 |
:program:`pip`: |
| 113 |
|
| 114 |
.. code-block:: shell |
| 115 |
|
| 116 |
python -m venv .venv |
| 117 |
. .venv/bin/activate |
| 118 |
python -m pip install -e . |
| 119 |
|
| 120 |
#. Create a new working branch. Choose any name you like. |
| 121 |
|
| 122 |
.. code-block:: shell |
| 123 |
|
| 124 |
git switch -c feature-xyz |
| 125 |
|
| 126 |
#. Hack, hack, hack. |
| 127 |
|
| 128 |
Write your code along with tests that shows that the bug was fixed or that |
| 129 |
the feature works as expected. |
| 130 |
|
| 131 |
#. Add a bullet point to :file:`CHANGES.rst` if the fix or feature is not trivial |
| 132 |
(small doc updates, typo fixes), then commit: |
| 133 |
|
| 134 |
.. code-block:: shell |
| 135 |
|
| 136 |
git commit -m 'Add useful new feature that does this.' |
| 137 |
|
| 138 |
#. Push changes in the branch to your forked repository on GitHub: |
| 139 |
|
| 140 |
.. code-block:: shell |
| 141 |
|
| 142 |
git push origin feature-xyz |
| 143 |
|
| 144 |
#. Submit a pull request from your branch to the ``master`` branch. |
| 145 |
|
| 146 |
GitHub recognizes certain phrases that can be used to automatically |
| 147 |
update the issue tracker. |
| 148 |
For example, including 'Closes #42' in the body of your pull request |
| 149 |
will close issue #42 if the PR is merged. |
| 150 |
|
| 151 |
#. Wait for a core developer or contributor to review your changes. |
| 152 |
|
| 153 |
You may be asked to address comments on the review. If so, please avoid |
| 154 |
force pushing to the branch. Sphinx uses the *squash merge* strategy when |
| 155 |
merging PRs, so follow-up commits will all be combined. |
| 156 |
|
| 157 |
|
| 158 |
Coding style |
| 159 |
~~~~~~~~~~~~ |
| 160 |
|
| 161 |
Please follow these guidelines when writing code for Sphinx: |
| 162 |
|
| 163 |
* Try to use the same code style as used in the rest of the project. |
| 164 |
|
| 165 |
* For non-trivial changes, please update the :file:`CHANGES.rst` file. |
| 166 |
If your changes alter existing behavior, please document this. |
| 167 |
|
| 168 |
* New features should be documented. |
| 169 |
Include examples and use cases where appropriate. |
| 170 |
If possible, include a sample that is displayed in the generated output. |
| 171 |
|
| 172 |
* When adding a new configuration variable, |
| 173 |
be sure to :doc:`document it </usage/configuration>` |
| 174 |
and update :file:`sphinx/cmd/quickstart.py` if it's important enough. |
| 175 |
|
| 176 |
* Add appropriate unit tests. |
| 177 |
|
| 178 |
Style and type checks can be run as follows: |
| 179 |
|
| 180 |
.. code-block:: shell |
| 181 |
|
| 182 |
uv run ruff check |
| 183 |
uv run ruff format |
| 184 |
uv run mypy |
| 185 |
|
| 186 |
|
| 187 |
Unit tests |
| 188 |
~~~~~~~~~~ |
| 189 |
|
| 190 |
Sphinx is tested using pytest_ for Python code and Jasmine_ for JavaScript. |
| 191 |
|
| 192 |
.. _pytest: https://docs.pytest.org/en/latest/ |
| 193 |
.. _Jasmine: https://jasmine.github.io/ |
| 194 |
|
| 195 |
To run Python unit tests, we recommend using :program:`tox`, which provides a number |
| 196 |
of targets and allows testing against multiple different Python environments: |
| 197 |
|
| 198 |
* To list all possible targets: |
| 199 |
|
| 200 |
.. code-block:: shell |
| 201 |
|
| 202 |
tox -av |
| 203 |
|
| 204 |
* To run unit tests for a specific Python version, such as Python 3.14: |
| 205 |
|
| 206 |
.. code-block:: shell |
| 207 |
|
| 208 |
tox -e py314 |
| 209 |
|
| 210 |
* Arguments to :program:`pytest` can be passed via :program:`tox`, |
| 211 |
e.g., in order to run a particular test: |
| 212 |
|
| 213 |
.. code-block:: shell |
| 214 |
|
| 215 |
tox -e py314 tests/test_module.py::test_new_feature |
| 216 |
|
| 217 |
You can also test by installing dependencies in your local environment: |
| 218 |
|
| 219 |
.. code-block:: shell |
| 220 |
|
| 221 |
uv run pytest |
| 222 |
|
| 223 |
Or with :program:`pip`: |
| 224 |
|
| 225 |
.. code-block:: shell |
| 226 |
|
| 227 |
python -m pip install . --group test |
| 228 |
pytest |
| 229 |
|
| 230 |
To run JavaScript tests, use :program:`npm`: |
| 231 |
|
| 232 |
.. code-block:: shell |
| 233 |
|
| 234 |
npm install |
| 235 |
npm run test |
| 236 |
|
| 237 |
.. tip:: |
| 238 |
|
| 239 |
:program:`jasmine` requires a Firefox binary to use as a test browser. |
| 240 |
|
| 241 |
On Unix systems, you can check the presence and location of the ``firefox`` |
| 242 |
binary at the command-line by running ``command -v firefox``. |
| 243 |
|
| 244 |
New unit tests should be included in the :file:`tests/` directory where necessary: |
| 245 |
|
| 246 |
* For bug fixes, first add a test that fails without your changes and passes |
| 247 |
after they are applied. |
| 248 |
|
| 249 |
* Tests that need a :program:`sphinx-build` run should be integrated in one of the |
| 250 |
existing test modules if possible. |
| 251 |
|
| 252 |
* Tests should be quick and only test the relevant components, as we aim that |
| 253 |
*the test suite should not take more than a minute to run*. |
| 254 |
In general, avoid using the ``app`` fixture and ``app.build()`` |
| 255 |
unless a full integration test is required. |
| 256 |
|
| 257 |
.. versionadded:: 1.8 |
| 258 |
|
| 259 |
Sphinx also runs JavaScript tests. |
| 260 |
|
| 261 |
.. versionchanged:: 1.5.2 |
| 262 |
Sphinx was switched from nose to pytest. |
| 263 |
|
| 264 |
|
| 265 |
Contribute documentation |
| 266 |
------------------------ |
| 267 |
|
| 268 |
Contributing to documentation involves modifying the source files |
| 269 |
found in the :file:`doc/` folder. |
| 270 |
To get started, you should first follow :ref:`contribute-get-started`, |
| 271 |
and then take the steps below to work with the documentation. |
| 272 |
|
| 273 |
The following sections describe how to get started with contributing |
| 274 |
documentation, as well as key aspects of a few different tools that we use. |
| 275 |
|
| 276 |
.. todo:: Add a more extensive documentation contribution guide. |
| 277 |
|
| 278 |
|
| 279 |
Build the documentation |
| 280 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 281 |
|
| 282 |
To build the documentation, run the following command: |
| 283 |
|
| 284 |
.. code-block:: shell |
| 285 |
|
| 286 |
sphinx-build -M html ./doc ./build/sphinx --fail-on-warning |
| 287 |
|
| 288 |
This will parse the Sphinx documentation's source files and generate HTML for |
| 289 |
you to preview in :file:`build/sphinx/html`. |
| 290 |
|
| 291 |
You can also build a **live version of the documentation** that you can preview |
| 292 |
in the browser. It will detect changes and reload the page any time you make |
| 293 |
edits. |
| 294 |
To do so, use `sphinx-autobuild`_ to run the following command: |
| 295 |
|
| 296 |
.. code-block:: shell |
| 297 |
|
| 298 |
sphinx-autobuild ./doc ./build/sphinx/ |
| 299 |
|
| 300 |
.. _sphinx-autobuild: https://github.com/sphinx-doc/sphinx-autobuild |
| 301 |
|
| 302 |
Translations |
| 303 |
------------ |
| 304 |
|
| 305 |
The parts of messages in Sphinx that go into builds are translated into several |
| 306 |
locales. The translations are kept as gettext ``.po`` files translated from the |
| 307 |
master template :file:`sphinx/locale/sphinx.pot`. |
| 308 |
|
| 309 |
These Sphinx core messages are translated using the online `Transifex |
| 310 |
<https://explore.transifex.com/sphinx-doc/sphinx-1/>`__ platform. |
| 311 |
|
| 312 |
Translated strings from the platform are pulled into the Sphinx repository |
| 313 |
by a maintainer before a new release. |
| 314 |
|
| 315 |
We do not accept pull requests altering the translation files directly. |
| 316 |
Instead, please contribute translations via the Transifex platform. |
| 317 |
|
| 318 |
Translations notes for maintainers |
| 319 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 320 |
|
| 321 |
The `transifex CLI <https://developers.transifex.com/docs/cli>`__ (``tx``) |
| 322 |
can be used to pull translations in ``.po`` format from Transifex. |
| 323 |
To do this, go to :file:`sphinx/locale` and then run ``tx pull -f -l LANG`` |
| 324 |
where ``LANG`` is an existing language identifier. |
| 325 |
It is good practice to run ``python utils/babel_runner.py update`` afterwards |
| 326 |
to make sure the ``.po`` file has the canonical Babel formatting. |
| 327 |
|
| 328 |
Sphinx uses `Babel <https://babel.pocoo.org/en/latest/>`_ to extract messages |
| 329 |
and maintain the catalog files. The :file:`utils` directory contains a helper |
| 330 |
script, :file:`utils/babel_runner.py`. |
| 331 |
|
| 332 |
* Use ``python babel_runner.py extract`` to update the ``.pot`` template. |
| 333 |
* Use ``python babel_runner.py update`` to update all existing language |
| 334 |
catalogs in ``sphinx/locale/*/LC_MESSAGES`` with the current messages in the |
| 335 |
template file. |
| 336 |
* Use ``python babel_runner.py compile`` to compile the ``.po`` files to binary |
| 337 |
``.mo`` files and ``.js`` files. |
| 338 |
|
| 339 |
When an updated ``.po`` file is submitted, run |
| 340 |
``python babel_runner.py compile`` to commit both the source and the compiled |
| 341 |
catalogs. |
| 342 |
|
| 343 |
When a new locale is added, add a new directory with the ISO 639-1 language |
| 344 |
identifier and put ``sphinx.po`` in there. Don't forget to update the possible |
| 345 |
values for :confval:`language` in :file:`doc/usage/configuration.rst`. |
| 346 |
|
| 347 |
|
| 348 |
Debugging tips |
| 349 |
-------------- |
| 350 |
|
| 351 |
* Delete the build cache before building documents if you make changes in the |
| 352 |
code by running the command ``make clean`` or using the |
| 353 |
:option:`sphinx-build --fresh-env` option. |
| 354 |
|
| 355 |
* Use the :option:`sphinx-build --pdb` option to run ``pdb`` on exceptions. |
| 356 |
|
| 357 |
* Use ``node.pformat()`` and ``node.asdom().toxml()`` to generate a printable |
| 358 |
representation of the document structure. |
| 359 |
|
| 360 |
* Set the configuration variable :confval:`keep_warnings` to ``True`` so |
| 361 |
warnings will be displayed in the generated output. |
| 362 |
|
| 363 |
* Set the configuration variable :confval:`nitpicky` to ``True`` so that Sphinx |
| 364 |
will complain about references without a known target. |
| 365 |
|
| 366 |
* Set the debugging options in the `Docutils configuration file |
| 367 |
<https://docutils.sourceforge.io/docs/user/config.html>`_. |
| 368 |
|
| 369 |
|
| 370 |
Updating generated files |
| 371 |
------------------------ |
| 372 |
|
| 373 |
* JavaScript stemming algorithms in :file:`sphinx/search/non-minified-js/*.js` |
| 374 |
and stopword files in :file:`sphinx/search/_stopwords/` |
| 375 |
are generated from the `Snowball project`_ |
| 376 |
by running :file:`utils/generate_snowball.py`. |
| 377 |
|
| 378 |
Minified files in :file:`sphinx/search/minified-js/*.js` are generated from |
| 379 |
non-minified ones using :program:`uglifyjs` (installed via npm). |
| 380 |
See :file:`sphinx/search/minified-js/README.rst`. |
| 381 |
|
| 382 |
.. _Snowball project: https://snowballstem.org/ |
| 383 |
|
| 384 |
* The :file:`searchindex.js` files found in |
| 385 |
the :file:`tests/js/fixtures/*` directories |
| 386 |
are generated by using the standard Sphinx HTML builder |
| 387 |
on the corresponding input projects found in :file:`tests/js/roots/*`. |
| 388 |
The fixtures provide test data used by the Sphinx JavaScript unit tests, |
| 389 |
and can be regenerated by running |
| 390 |
the :file:`utils/generate_js_fixtures.py` script. |