Sphinx 101 β Complete Guideο
What Is Sphinx?ο
Sphinx is an open-source documentation generator originally created for the Python language reference documentation (it still powers the official Python docs). Today it is widely used across languages β including Java, C++, and JavaScript projects β whenever developers need to produce structured, professional, multi-page technical documentation from plain text source files.
Sphinx takes plain text files written in reStructuredText (.rst) or Markdown (.md, via the MyST parser extension) and generates:
Static HTML websites (the most common output)
PDF documents (via LaTeX)
ePub e-books
Man pages
The result is a maintainable, version-controlled documentation site that lives alongside the source code and can be automatically published through CI/CD.
What Is Sphinx Useful For?ο
Use Case |
Why Sphinx |
|---|---|
Project portals |
Aggregates API docs, guides, reports, and tutorials in one navigable site |
API reference |
Can auto-import Python docstrings; for Java projects, embeds Javadoc/Doxygen links |
Tutorials & how-tos |
Numbered sections, admonition boxes, code blocks with syntax highlighting |
Multi-version docs |
Tools like |
Search |
Built-in full-text client-side search (no server needed) |
Cross-references |
|
Theming |
Dozens of themes ( |
Sphinx vs. Alternativesο
Tool |
Primary audience |
Markup |
Auto-API |
Best for |
|---|---|---|---|---|
Sphinx |
Python / polyglot |
RST / Markdown |
Python (autodoc) |
Comprehensive project portals |
Javadoc |
Java |
Javadoc tags |
Yes (Java) |
API reference only |
Doxygen |
C/C++/Java |
Doxygen tags |
Yes |
Cross-referenced code ref |
MkDocs |
Any |
Markdown |
No |
Simple Markdown-first sites |
Docusaurus |
JS / React |
Markdown/MDX |
No |
Product/SaaS documentation |
Sphinx shines when you need a single portal that combines hand-written narrative documentation, auto-generated API references, test reports, and external links β exactly the goal of this project.
How Is Sphinx Used?ο
1. Install Sphinxο
pip install sphinx sphinx-rtd-theme myst-parser
Or pin versions in a requirements.txt:
sphinx==7.4.7
sphinx-rtd-theme==2.0.0
myst-parser==3.0.1
2. Initialise a Projectο
sphinx-quickstart docs-sphinx
This interactive wizard creates conf.py (the configuration file), index.rst (the master table of contents), and a Makefile.
3. Source File Structureο
docs-sphinx/
βββ source/
β βββ conf.py β Sphinx configuration
β βββ index.rst β Master table of contents (toctree)
β βββ overview.md β A page written in Markdown
β βββ api.rst β A page written in reStructuredText
β βββ _static/ β Custom CSS / images
βββ Makefile β make html, make pdf, β¦
βββ requirements.txt β Pinned Python dependencies
4. The toctree Directiveο
The .. toctree:: directive in index.rst defines the siteβs navigation hierarchy:
.. toctree::
:maxdepth: 2
:caption: Project Overview
overview
architecture
getting_started
Each entry maps to a .rst or .md file. Sphinx resolves them recursively.
5. Build the Documentationο
cd docs-sphinx
make html # output β _build/html/index.html
make latexpdf # output β _build/latex/*.pdf
make clean # remove previous build artefacts
6. Key reStructuredText Syntaxο
Section Heading
===============
Sub-heading
-----------
A paragraph with **bold**, *italic*, and ``inline code``.
.. code-block:: java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello from Sphinx!");
}
}
.. note::
This is an admonition β a highlighted information box.
.. warning::
This is a warning box.
7. Markdown with MySTο
With the myst-parser extension enabled in conf.py, you can write pages in Markdown:
# My Page
A paragraph with **bold** and *italic*.
```java
public class Hello { }
```
```{note}
This is a MyST admonition.
```
```{toctree}
:maxdepth: 2
overview
architecture
```
How to Integrate Sphinx with GitHub Actionsο
The workflow below builds the Sphinx HTML and publishes it to the gh-pages branch using the peaceiris/actions-gh-pages action. It preserves other content already on gh-pages (e.g., Maven Site) via keep_files: true.
# .github/workflows/sphinx-docs.yml
name: Build and Deploy Sphinx Docs
on:
push:
branches: [main]
workflow_dispatch: # allow manual trigger
jobs:
sphinx:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Cache pip packages
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('docs-sphinx/requirements.txt') }}
restore-keys: ${{ runner.os }}-pip-
- name: Install Sphinx & extensions
run: pip install -r docs-sphinx/requirements.txt
- name: Build Sphinx HTML
run: |
cd docs-sphinx
make html
- name: Deploy to GitHub Pages (sphinx/ subfolder)
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs-sphinx/_build/html
destination_dir: sphinx # published at /sphinx/ URL path
keep_files: true # preserve Maven Site & Doxygen content
Required Repository Settingsο
Settings β Actions β General β Workflow permissions β Read and write permissions
Settings β Pages β Source:
gh-pagesbranch, folder:/
The Sphinx portal will then be live at:
https://<owner>.github.io/<repo>/sphinx/
How This Project Uses Sphinxο
Documentation Architectureο
The Sphinx portal for SpringBootLibrary is a meta-documentation hub β it does not duplicate content that already lives in other tools; instead it links out to the generated artefacts:
GitHub Pages (gh-pages branch)
βββ index.html β Landing page redirecting to Sphinx or Maven site
βββ sphinx/ β Sphinx portal (this site)
β βββ index.html
β βββ overview.html
β βββ testing.html
β βββ reports.html β Links to JaCoCo, Surefire, PMD reports
β βββ javadoc_link.html β Links to Javadoc in site/apidocs/
β βββ sphinx_101.html β This page
βββ site/ β Maven Site (Javadoc, JaCoCo, PMD, Checkstyle β¦)
β βββ apidocs/
β βββ jacoco/
β βββ surefire-report.html
β βββ β¦
βββ doxygen/ β Doxygen cross-referenced HTML
βββ html/
Integration Pointsο
External Artefact |
Where Sphinx links to it |
|---|---|
Javadoc |
|
JaCoCo |
|
Surefire |
|
PMD / Checkstyle |
|
Performance report |
|
Doxygen |
|
Adding a New Documentation Pageο
Create
docs-sphinx/source/my_page.mdAdd
my_pageto the appropriate.. toctree::inindex.rstPush to
mainβ the GitHub Actions workflow rebuilds and redeploys automatically
Adding the conf.py Extensions for This Projectο
extensions = [
'sphinx.ext.autodoc', # auto-import Python docstrings (optional here)
'sphinx.ext.viewcode', # adds [source] links
'sphinx.ext.napoleon', # Google/NumPy style docstrings
'myst_parser', # Markdown .md files
'sphinx_rtd_theme', # Read the Docs theme
]
Useful Sphinx Resourcesο
Resource |
URL |
|---|---|
Official Sphinx docs |
https://www.sphinx-doc.org |
MyST Markdown parser |
https://myst-parser.readthedocs.io |
Read the Docs theme |
https://sphinx-rtd-theme.readthedocs.io |
Furo theme (modern) |
https://pradyunsg.me/furo |
sphinx-multiversion |
https://holzhaus.github.io/sphinx-multiversion |
peaceiris/actions-gh-pages |
https://github.com/peaceiris/actions-gh-pages |