| 1 |
******************************* |
| 2 |
Advanced changelog layout setup |
| 3 |
******************************* |
| 4 |
|
| 5 |
This page attempts do demonstrate one way of setting up a docs site for |
| 6 |
use with |project| and includes a few opinionated integration solutions. |
| 7 |
But of course, this layout may not fit everybody's needs. |
| 8 |
When configuring your project, try to figure out what works for you — |
| 9 |
you don't have to follow everything laid out here blindly. |
| 10 |
|
| 11 |
Project structure |
| 12 |
^^^^^^^^^^^^^^^^^ |
| 13 |
|
| 14 |
The author likes the following project directory layout: |
| 15 |
|
| 16 |
:: |
| 17 |
|
| 18 |
{{ project_root }}/ |
| 19 |
│ |
| 20 |
├─ docs/ |
| 21 |
│ ├─ changelog.d/ |
| 22 |
│ │ │─ .gitignore |
| 23 |
│ │ │─ .towncrier-template.rst.j2 |
| 24 |
│ │ │─ {{ issue_number }}.{{ changelog_fragment_type }}.rst |
| 25 |
│ │ │─ ... |
| 26 |
│ │ └─ README.rst |
| 27 |
│ ├─ changelog.rst |
| 28 |
│ ├─ conf.py |
| 29 |
│ ├─ index.rst |
| 30 |
│ ├─ requirements.in |
| 31 |
│ └─ requirements.txt |
| 32 |
├─ src/ |
| 33 |
│ └─ {{ python_importable_name }}/ |
| 34 |
│ └─ ... |
| 35 |
├─ .readthedocs.yml |
| 36 |
├─ CHANGELOG.rst |
| 37 |
├─ pyproject.toml |
| 38 |
├─ README.rst |
| 39 |
├─ tox.ini |
| 40 |
└─ ... |
| 41 |
|
| 42 |
This is an ``src``-layout project with a Python package located at |
| 43 |
``src/{{ python_importable_name }}/`` but we won't touch this topic. |
| 44 |
There are several automation, configuration, documentation and metadata |
| 45 |
files in the project root that will be described later on this page. |
| 46 |
Finally, a :doc:`Sphinx <sphinx:index>`-based site is located under |
| 47 |
the ``docs/``. |
| 48 |
|
| 49 |
The rest of this page will describe what to have in each of those files. |
| 50 |
|
| 51 |
|
| 52 |
``docs/changelog.d/`` |
| 53 |
^^^^^^^^^^^^^^^^^^^^^ |
| 54 |
|
| 55 |
Let's start with the ``docs/changelog.d/``. This is a folder where the |
| 56 |
end-users are supposed to add their changelog fragments for Towncrier to |
| 57 |
consume. |
| 58 |
|
| 59 |
``docs/changelog.d/.gitignore`` |
| 60 |
------------------------------- |
| 61 |
|
| 62 |
First, let's make sure Git only tracks files that we want there by adding |
| 63 |
a ``.gitignore`` file in this folder. First thing, it adds everything to |
| 64 |
"ignore" but then allows ``.gitignore``, ``.gitignore``, ``README.rst`` |
| 65 |
and any RST documents matching Towncrier change note fragment naming |
| 66 |
convention. |
| 67 |
|
| 68 |
.. code-block:: |
| 69 |
|
| 70 |
* |
| 71 |
|
| 72 |
!.gitignore |
| 73 |
!.towncrier-template.rst.j2 |
| 74 |
!*.*.rst |
| 75 |
!README.rst |
| 76 |
|
| 77 |
|
| 78 |
``docs/changelog.d/.towncrier-template.rst.j2`` |
| 79 |
----------------------------------------------- |
| 80 |
|
| 81 |
Then, there's ``.towncrier-template.rst.j2``. It's a changelog template, |
| 82 |
for Towncrier to use. It can be copied from |
| 83 |
https://github.com/twisted/towncrier/tree/master/src/towncrier/templates. |
| 84 |
This name is set in ``pyproject.toml`` in the project root. |
| 85 |
|
| 86 |
``docs/changelog.d/{{ issue_number }}.{{ changelog_fragment_type }}.rst`` |
| 87 |
------------------------------------------------------------------------- |
| 88 |
|
| 89 |
These are changelog fragments in RST format. They are absorbed by |
| 90 |
Towncrier during the release and before that, these files will be used |
| 91 |
in the preview generated by |project|. |
| 92 |
|
| 93 |
``docs/changelog.d/README.rst`` |
| 94 |
------------------------------- |
| 95 |
|
| 96 |
This ``README.rst`` file would normally contain — a guide for the |
| 97 |
contributors on how to write change notes. For example, setuptools has a |
| 98 |
useful write-up on :ref:`authoring changelog fragments |
| 99 |
<setuptools:adding change notes with your prs>`. It is useful to have it |
| 100 |
in this place so that it shows up on GitHub when the users navigate to |
| 101 |
the folder with the fragments via the web UI. |
| 102 |
|
| 103 |
|
| 104 |
``docs/`` |
| 105 |
^^^^^^^^^^^^^^^^^^^^^ |
| 106 |
|
| 107 |
``docs/changelog.rst`` |
| 108 |
---------------------- |
| 109 |
|
| 110 |
This is a Sphinx page that contains both the future version changelog |
| 111 |
preview via ``.. towncrier-draft-entries::`` directive and the changelog |
| 112 |
for all already released versions that is managed by Towncrier in a |
| 113 |
separate RST document ``CHANGELOG.rst`` in the project root. |
| 114 |
|
| 115 |
.. code-block:: rst |
| 116 |
|
| 117 |
********* |
| 118 |
Changelog |
| 119 |
********* |
| 120 |
|
| 121 |
Versions follow `Semantic Versioning`_ (``<major>.<minor>.<patch>``). |
| 122 |
Backward incompatible (breaking) changes will only be introduced in major |
| 123 |
versions with advance notice in the **Deprecations** section of releases. |
| 124 |
|
| 125 |
.. _Semantic Versioning: https://semver.org/ |
| 126 |
|
| 127 |
.. towncrier-draft-entries:: |release| [UNRELEASED DRAFT] as on |today| |
| 128 |
|
| 129 |
.. include:: ../CHANGELOG.rst |
| 130 |
|
| 131 |
|
| 132 |
``docs/conf.py`` |
| 133 |
---------------- |
| 134 |
|
| 135 |
The Sphinx configuration demonstrates how to keep the version |
| 136 |
information known to Sphinx in sync with the Git tag based metadata. |
| 137 |
Note the exclusion of ``docs/changelog.d/`` and the settings prefixed |
| 138 |
with ``towncrier_draft_``. |
| 139 |
|
| 140 |
.. code-block:: python |
| 141 |
|
| 142 |
"""Configuration for the Sphinx documentation generator.""" |
| 143 |
|
| 144 |
from functools import partial |
| 145 |
from pathlib import Path |
| 146 |
|
| 147 |
from setuptools_scm import get_version |
| 148 |
|
| 149 |
|
| 150 |
# -- Path setup -------------------------------------------------------------- |
| 151 |
|
| 152 |
PROJECT_ROOT_DIR = Path(__file__).parents[1].resolve() |
| 153 |
get_scm_version = partial(get_version, root=PROJECT_ROOT_DIR) |
| 154 |
|
| 155 |
|
| 156 |
# -- Project information ----------------------------------------------------- |
| 157 |
|
| 158 |
github_url = 'https://github.com' |
| 159 |
github_repo_org = 'your-org' |
| 160 |
github_repo_name = 'your-project' |
| 161 |
github_repo_slug = f'{github_repo_org}/{github_repo_name}' |
| 162 |
github_repo_url = f'{github_url}/{github_repo_slug}' |
| 163 |
github_sponsors_url = f'{github_url}/sponsors' |
| 164 |
|
| 165 |
project = github_repo_name |
| 166 |
author = f'{project} Contributors' |
| 167 |
copyright = f'2021, {author}' |
| 168 |
|
| 169 |
# The short X.Y version |
| 170 |
version = '.'.join( |
| 171 |
get_scm_version( |
| 172 |
local_scheme='no-local-version', |
| 173 |
).split('.')[:3], |
| 174 |
) |
| 175 |
|
| 176 |
# The full version, including alpha/beta/rc tags |
| 177 |
release = get_scm_version() |
| 178 |
|
| 179 |
rst_epilog = f""" |
| 180 |
.. |project| replace:: {project} |
| 181 |
""" |
| 182 |
|
| 183 |
|
| 184 |
# -- General configuration --------------------------------------------------- |
| 185 |
|
| 186 |
extensions = [ |
| 187 |
# Built-in extensions: |
| 188 |
'sphinx.ext.extlinks', |
| 189 |
'sphinx.ext.intersphinx', |
| 190 |
|
| 191 |
# Third-party extensions: |
| 192 |
'sphinxcontrib.towncrier.ext', # provides `.. towncrier-draft-entries::` |
| 193 |
] |
| 194 |
|
| 195 |
exclude_patterns = [ |
| 196 |
'_build', 'Thumbs.db', '.DS_Store', # <- Defaults |
| 197 |
'changelog.d/**', # Towncrier-managed change notes |
| 198 |
] |
| 199 |
|
| 200 |
|
| 201 |
# -- Options for HTML output ------------------------------------------------- |
| 202 |
|
| 203 |
html_theme = 'furo' |
| 204 |
|
| 205 |
|
| 206 |
# -- Extension configuration ------------------------------------------------- |
| 207 |
|
| 208 |
# -- Options for intersphinx extension --------------------------------------- |
| 209 |
|
| 210 |
intersphinx_mapping = { |
| 211 |
'python': ('https://docs.python.org/3', None), |
| 212 |
'rtd': ('https://docs.rtfd.io/en/stable', None), |
| 213 |
'sphinx': ('https://www.sphinx-doc.org/en/master', None), |
| 214 |
} |
| 215 |
|
| 216 |
# -- Options for extlinks extension ------------------------------------------ |
| 217 |
|
| 218 |
extlinks = { |
| 219 |
'issue': (f'{github_repo_url}/issues/%s', '#'), |
| 220 |
'pr': (f'{github_repo_url}/pull/%s', 'PR #'), |
| 221 |
'commit': (f'{github_repo_url}/commit/%s', ''), |
| 222 |
'gh': (f'{github_url}/%s', 'GitHub: '), |
| 223 |
'user': (f'{github_sponsors_url}/%s', '@'), |
| 224 |
} |
| 225 |
|
| 226 |
# -- Options for towncrier_draft extension ----------------------------------- |
| 227 |
|
| 228 |
towncrier_draft_autoversion_mode = 'draft' # or: 'sphinx-version', 'sphinx-release' |
| 229 |
towncrier_draft_include_empty = True |
| 230 |
towncrier_draft_working_directory = PROJECT_ROOT_DIR |
| 231 |
# Not yet supported: towncrier_draft_config_path = 'pyproject.toml' # relative to cwd |
| 232 |
|
| 233 |
# -- Strict mode ------------------------------------------------------------- |
| 234 |
|
| 235 |
default_role = 'any' |
| 236 |
|
| 237 |
nitpicky = True |
| 238 |
|
| 239 |
|
| 240 |
``docs/index.rst`` |
| 241 |
------------------ |
| 242 |
|
| 243 |
The root document includes most of the README excluding one badge and |
| 244 |
its title. It allows to flexibly control what information goes to the |
| 245 |
PyPI and GitHub repo pages and what appears in the docs. |
| 246 |
|
| 247 |
This document must contain a ``.. toctree::`` directive that has a |
| 248 |
pointer to the ``changelog`` document in the list. |
| 249 |
|
| 250 |
.. code-block:: rst |
| 251 |
|
| 252 |
Welcome to |project|'s documentation! |
| 253 |
===================================== |
| 254 |
|
| 255 |
.. include:: ../README.rst |
| 256 |
:end-before: DO-NOT-REMOVE-docs-badges-END |
| 257 |
|
| 258 |
.. include:: ../README.rst |
| 259 |
:start-after: DO-NOT-REMOVE-docs-intro-START |
| 260 |
|
| 261 |
.. toctree:: |
| 262 |
:maxdepth: 2 |
| 263 |
:caption: Contents: |
| 264 |
|
| 265 |
changelog |
| 266 |
|
| 267 |
|
| 268 |
``docs/requirements.in`` |
| 269 |
------------------------ |
| 270 |
|
| 271 |
``requirements.in`` is a standard ``requirements.txt``-type file that |
| 272 |
only lists dependencies that are directly used by the :doc:`Sphinx |
| 273 |
static docs site generator <sphinx:index>`. It may optionally contain |
| 274 |
the minimum necessary versions of those. |
| 275 |
|
| 276 |
.. code-block:: text |
| 277 |
|
| 278 |
furo |
| 279 |
setuptools-scm |
| 280 |
Sphinx |
| 281 |
sphinxcontrib-towncrier |
| 282 |
|
| 283 |
``docs/requirements.txt`` |
| 284 |
------------------------- |
| 285 |
|
| 286 |
But stating just the direct dependencies without strict version |
| 287 |
restrictions is not enough for reproducible builds. Since it is |
| 288 |
important to keep the docs build predictable over time, we use |
| 289 |
`pip-tools`_ to generate a ``constraints.txt``-type pip-compatible |
| 290 |
lockfile with pinned version constraints for the whole transitive |
| 291 |
dependency tree. This file is ``requirements.txt`` and using it will |
| 292 |
ensure that the virtualenv for building the docs always has the same |
| 293 |
software with the same versions in it. |
| 294 |
|
| 295 |
.. tip:: |
| 296 |
|
| 297 |
As a bonus, having a ``.in`` + ``.txt`` pair of files is natively |
| 298 |
supported by GitHub Dependabot. |
| 299 |
|
| 300 |
.. _pip-tools: https://github.com/jazzband/pip-tools |
| 301 |
|
| 302 |
|
| 303 |
``.readthedocs.yml`` |
| 304 |
^^^^^^^^^^^^^^^^^^^^ |
| 305 |
|
| 306 |
To set up Read the Docs, add a ``.readthedocs.yml`` file in the project |
| 307 |
root. The following configuration makes sure that the lockfile is used |
| 308 |
to provision the build env. It also configures how Sphinx should behave |
| 309 |
like failing the build on any warnings and having nice URLs. |
| 310 |
|
| 311 |
.. code-block:: yaml |
| 312 |
|
| 313 |
--- |
| 314 |
version: 2 |
| 315 |
|
| 316 |
formats: all |
| 317 |
|
| 318 |
sphinx: |
| 319 |
builder: dirhtml |
| 320 |
configuration: docs/conf.py |
| 321 |
fail_on_warning: true |
| 322 |
|
| 323 |
build: |
| 324 |
image: latest |
| 325 |
|
| 326 |
python: |
| 327 |
version: 3.8 |
| 328 |
install: |
| 329 |
- requirements: docs/requirements.txt |
| 330 |
... |
| 331 |
|
| 332 |
.. note:: |
| 333 |
|
| 334 |
When you have a Read the Docs YAML config in your repository, none |
| 335 |
of the :ref:`settings supported by it <rtd:config-file/v2:supported |
| 336 |
settings>` are derived from the web UI. |
| 337 |
|
| 338 |
.. tip:: |
| 339 |
|
| 340 |
Having :doc:`Read the Docs <rtd:index>` plugged into your project it |
| 341 |
is also possible to :doc:`enable pull-request builds |
| 342 |
<rtd:pull-requests>`. |
| 343 |
|
| 344 |
``CHANGELOG.rst`` |
| 345 |
^^^^^^^^^^^^^^^^^ |
| 346 |
|
| 347 |
This file in the project root contains the compiled changelog with the |
| 348 |
notes from the released project versions. It is managed by Towncrier and |
| 349 |
should not be edited by you manually. |
| 350 |
|
| 351 |
.. code-block:: rst |
| 352 |
|
| 353 |
.. towncrier release notes start |
| 354 |
|
| 355 |
|
| 356 |
``pyproject.toml`` |
| 357 |
^^^^^^^^^^^^^^^^^^ |
| 358 |
|
| 359 |
``pyproject.toml`` in the root contains the setup for Towncrier itself |
| 360 |
under the ``[tool.towncrier]`` section. It binds it all together |
| 361 |
pointing at the directory for the change notes, the target changelog |
| 362 |
document and the template to use when generating it. It also lists the |
| 363 |
categories for the change fragments. |
| 364 |
|
| 365 |
.. code-block:: toml |
| 366 |
|
| 367 |
[tool.towncrier] |
| 368 |
directory = "docs/changelog.d/" |
| 369 |
filename = "CHANGELOG.rst" |
| 370 |
issue_format = ":issue:`{issue}`" |
| 371 |
package_dir = "src" |
| 372 |
template = "docs/changelog.d/.towncrier-template.rst.j2" |
| 373 |
title_format = "v{version} ({project_date})" |
| 374 |
underlines = ["=", "^", "-", "~"] |
| 375 |
|
| 376 |
[[tool.towncrier.section]] |
| 377 |
path = "" |
| 378 |
|
| 379 |
[[tool.towncrier.type]] |
| 380 |
directory = "bugfix" |
| 381 |
name = "Bugfixes" |
| 382 |
showcontent = true |
| 383 |
|
| 384 |
[[tool.towncrier.type]] |
| 385 |
directory = "feature" |
| 386 |
name = "Features" |
| 387 |
showcontent = true |
| 388 |
|
| 389 |
[[tool.towncrier.type]] |
| 390 |
directory = "deprecation" |
| 391 |
name = "Deprecations (removal in next major release)" |
| 392 |
showcontent = true |
| 393 |
|
| 394 |
[[tool.towncrier.type]] |
| 395 |
directory = "breaking" |
| 396 |
name = "Backward incompatible changes" |
| 397 |
showcontent = true |
| 398 |
|
| 399 |
[[tool.towncrier.type]] |
| 400 |
directory = "doc" |
| 401 |
name = "Documentation" |
| 402 |
showcontent = true |
| 403 |
|
| 404 |
[[tool.towncrier.type]] |
| 405 |
directory = "misc" |
| 406 |
name = "Miscellaneous" |
| 407 |
showcontent = true |
| 408 |
|
| 409 |
|
| 410 |
``README.rst`` |
| 411 |
^^^^^^^^^^^^^^ |
| 412 |
|
| 413 |
The README document is an important bit of your project. It shows up on |
| 414 |
GitHub and is normally shown on PyPI. Besides that, it's possible to |
| 415 |
include its fragments into the docs front page. |
| 416 |
|
| 417 |
The example below shows how to use comment markers to include a part of |
| 418 |
the badges into a Sphinx document also embedding some prose from the |
| 419 |
README. Scroll up and see how it's being embedded into |
| 420 |
``docs/index.rst``. |
| 421 |
|
| 422 |
.. code-block:: rst |
| 423 |
|
| 424 |
.. image:: https://img.shields.io/pypi/v/your-project.svg?logo=Python&logoColor=white |
| 425 |
:target: https://pypi.org/project/your-project |
| 426 |
:alt: your-project @ PyPI |
| 427 |
|
| 428 |
.. image:: https://github.com/your-org/your-project/actions/workflows/ci-cd.yml/badge.svg?event=push |
| 429 |
:target: https://github.com/your-org/your-project/actions/workflows/ci-cd.yml |
| 430 |
:alt: GitHub Actions CI/CD build status |
| 431 |
|
| 432 |
.. DO-NOT-REMOVE-docs-badges-END |
| 433 |
|
| 434 |
.. image:: https://img.shields.io/readthedocs/your-project/latest.svg?logo=Read%20The%20Docs&logoColor=white |
| 435 |
:target: https://your-project.rtfd.io/en/latest/?badge=latest |
| 436 |
:alt: Documentation Status @ RTD |
| 437 |
|
| 438 |
your-project |
| 439 |
============ |
| 440 |
|
| 441 |
.. DO-NOT-REMOVE-docs-intro-START |
| 442 |
|
| 443 |
A project with Sphinx-managed documentation and description sourced |
| 444 |
from this README. |
| 445 |
|
| 446 |
|
| 447 |
``tox.ini`` |
| 448 |
^^^^^^^^^^^ |
| 449 |
|
| 450 |
This is an example of setting up a tox-based Sphinx invocation |
| 451 |
|
| 452 |
.. code-block:: ini |
| 453 |
|
| 454 |
[tox] |
| 455 |
envlist = python |
| 456 |
isolated_build = true |
| 457 |
minversion = 3.21.0 |
| 458 |
|
| 459 |
|
| 460 |
[testenv:docs] |
| 461 |
basepython = python3 |
| 462 |
deps = |
| 463 |
-r{toxinidir}{/}docs{/}requirements.txt |
| 464 |
description = Build The Docs |
| 465 |
commands = |
| 466 |
# Retrieve possibly missing commits: |
| 467 |
-git fetch --unshallow |
| 468 |
-git fetch --tags |
| 469 |
|
| 470 |
# Build the html docs with Sphinx: |
| 471 |
{envpython} -m sphinx \ |
| 472 |
-j auto \ |
| 473 |
-b html \ |
| 474 |
{tty:--color} \ |
| 475 |
-a \ |
| 476 |
-n \ |
| 477 |
-W --keep-going \ |
| 478 |
-d "{temp_dir}{/}.doctrees" \ |
| 479 |
{posargs:} \ |
| 480 |
. \ |
| 481 |
"{envdir}{/}docs_out" |
| 482 |
|
| 483 |
# Print out the output docs dir and a way to serve html: |
| 484 |
-{envpython} -c\ |
| 485 |
'import pathlib;\ |
| 486 |
docs_dir = pathlib.Path(r"{envdir}") / "docs_out";\ |
| 487 |
index_file = docs_dir / "index.html";\ |
| 488 |
print("\n" + "=" * 120 +\ |
| 489 |
f"\n\nDocumentation available under:\n\n\ |
| 490 |
\tfile://\{index_file\}\n\nTo serve docs, use\n\n\ |
| 491 |
\t$ python3 -m http.server --directory \ |
| 492 |
\N\{QUOTATION MARK\}\{docs_dir\}\N\{QUOTATION MARK\} 0\n\n" +\ |
| 493 |
"=" * 120)' |
| 494 |
changedir = {toxinidir}{/}docs |
| 495 |
isolated_build = true |
| 496 |
passenv = |
| 497 |
SSH_AUTH_SOCK |
| 498 |
skip_install = true |
| 499 |
whitelist_externals = |
| 500 |
git |
| 501 |
|
| 502 |
With this setup, run ``tox -e docs`` to build the site locally. Integrate |
| 503 |
the same command in your CI. |