Set up a new project¶
This is a guide for adding Sphinx Stack to a new or existing project. You’ll configure it, build the documentation locally, and set up automatic documentation checks.
Before you start¶
Decide where your documentation will live. Consider a dedicated documentation repository if the documentation covers multiple repositories, has a separate release process, or needs different ownership or access controls. Otherwise, keep the documentation in the same repository as the project. Both approaches use the same Sphinx Stack directory structure and build commands.
Conventionally, the files go in a docs directory, which is the default layout used
by the Sphinx Stack. This lets contributors review documentation alongside related
project changes.
Add the Sphinx Stack¶
Choose the setup that matches your project.
Create a new repository from the template¶
Go to the Sphinx Stack repository and select Use this template > Create a new repository, then fill in the form. Choose an owner; if you’re creating documentation for a Canonical project, set the repository owner to canonical. Enter a repository name, choose its visibility, add a description if needed, and create the repository.
Add the Sphinx Stack to an existing repository¶
Copy these paths from the Sphinx Stack repository into the root of your project’s repository:
the
docsdirectory.readthedocs.yaml, which configures builds on Read the Docsthe workflow files in the
.github/workflowsdirectory
If your project already has a .github/workflows directory, add the relevant
documentation workflow files to it rather than replacing the existing directory.
Review the template files¶
Before committing your setup, review the files you copied or received from the template.
If you created a repository from the template, remove the files that can’t be reused,
such as CONTRIBUTING.md and .github/CODEOWNERS. Also remove
.github/workflows/test-sphinx-stack.yml, which tests the Sphinx Stack itself and
can’t be reused by your project.
Review the remaining workflows in .github/workflows/, remove any that duplicate
checks your project already runs, and keep the ones you need. In particular:
cla-check.ymlverifies whether contributors have signed the Canonical License Agreement. All Canonical projects require this check, so remove it only if your project already runs it.sphinx-python-dependency-build-checks.ymlverifies Python dependencies for the documentation system. Remove it if your project has its own dependency checks.markdown-style-checks.ymlruns the built-in Markdown linter. Remove it if your project already validates its Markdown files.
Configure your project¶
Open docs/conf.py and replace the project-specific values marked with TODO, such
as the project name, author, and repository information.
You can leave optional settings at their default values and change them later. See Configure your project for details about the available settings.
Build and preview the documentation¶
Building the documentation requires make, python3, python3-venv,
python3-pip.
From the docs directory, run:
make run
This creates a virtual environment in docs/.venv, installs the documentation
dependencies, builds the documentation, and starts a local preview server.
Open http://127.0.0.1:8000/ and confirm that the home page loads and
displays the project information you configured.
The server rebuilds the documentation when you change source files or docs/conf.py.
The documentation build is self-contained, so you do not need to integrate it with your project’s build system during initial setup.
If you later want the project build to also build the documentation, see Parent projects and the build for how the two builds can coexist, and Bridge project and documentation builds for the steps to do it.
Add your content¶
The home page is docs/index.rst. The template also provides directories for
tutorials, how-to guides, reference material, and explanations under docs/.
The navigation menu structure is set by .. toctree:: directives. These directives
define the hierarchy of included content throughout the documentation. The
index.rst page’s toctree block contains the top level navigation, which by
default is the Diátaxis documentation structure.
To add a new page to the documentation, for example a Reference page about your project’s settings:
Create a new file under the
docs/reference/directory calledsettings.rstwith the following heading:reStructuredText title example¶Settings ========
If you prefer to use Markdown (MyST) syntax instead of reST, you can create the equivalent
settings.mdfile and add the following Markdown-formatted heading at the beginning:Markdown title example¶# SettingsAdd the new page to the navigation menu: open the
docs/reference/index.rstfile or another file where you want to nest the new page; at the bottom of the file, add the followingtoctreedirective:.. toctree:: :hidden: :maxdepth: 2 settings
The page will appear in the navigation after the documentation rebuilds. For more on the supported syntax, see reStructuredText syntax and MyST syntax.
By default, the page’s title (the first heading in the file) is shown in the global
navigation. You can override the name of a menu entry by specifying it explicitly in the
toctree block (e.g., Reference </reference/index>).
Review the automatic checks¶
The Sphinx Stack includes GitHub workflows for documentation checks, including spelling, links, and inclusive language. Review the workflows’ triggers and paths for your repository.
If you move the documentation from docs/, update the relevant paths and working
directories. See GitHub workflows for the available workflows, or
run the checks locally.
Configure pre-commit hooks (optional)¶
Optional pre-commit hooks run documentation checks when you create a commit, so you can catch issues before opening a pull request.
The Sphinx Stack includes a ready-to-use .pre-commit-config.yaml file under
docs/_dev/:
repos:
- repo: local
hooks:
- id: make-spelling
name: Run make spelling
entry: make -C docs spelling
language: system
pass_filenames: false
files: ^docs/.*\.(rst|md|txt)$
- id: make-linkcheck
name: Run make linkcheck
entry: make -C docs linkcheck
language: system
pass_filenames: false
files: ^docs/.*\.(rst|md|txt)$
- id: make-woke
name: Run make woke
entry: make -C docs woke
language: system
pass_filenames: false
files: ^docs/.*\.(rst|md|txt)$
If your repository does not already use pre-commit, copy this file to the repository
root as .pre-commit-config.yaml. If it does, add the documentation hooks to its
existing configuration.
To apply the configuration, install the Sphinx Stack hooks, for instance:
pre-commit install --config docs/_dev/.pre-commit-config.yaml
After that, you should see the checks running with every commit:
user@host:~$git commit -m 'add spelling errors'Run make spelling.......................................................Failed
Run make linkcheck......................................................Passed
Run make woke...........................................................Passed
Next steps¶
Before publishing the documentation:
Replace the template pages with content for your project.
Review the remaining settings in Configure your project.
Run the documentation checks and confirm that the GitHub workflows you kept behave as expected.
Follow Publish on Read the Docs.
After the initial setup, you can:
Use Bridge project and documentation builds to integrate the documentation and project builds.
Explore Optional customisation for features like diagrams, API references, and custom templates.
Follow Update your Sphinx Stack to keep your project’s Sphinx Stack up to date.