-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New Sphinx tutorial, part II #9355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
1cbaf7f
Minor style fixes
astrojuanlu 167828f
Add section about exporting to other formats
astrojuanlu dc5dc61
Turn plain image into figure
astrojuanlu 1e49218
Add tutorial section about extensions and themes
astrojuanlu 2058acc
Add local table of contents
astrojuanlu 57b0a1b
Add section about the toctree
astrojuanlu e022872
Add section about cross-references
astrojuanlu 5a057d3
Actually use new theme
astrojuanlu fb23605
Apply style suggestions from code review
astrojuanlu 6f71c7b
Use absolute links for Sphinx documentation references
astrojuanlu e75f31a
Add link to third-party sphinx-themes
astrojuanlu d08c367
Avoid reference to earlier content
astrojuanlu 3fcaa17
Move reference to list of Pygments lexers
astrojuanlu 8377a55
Simplify out-of-toctree warning
astrojuanlu 0581a17
Clarify this is about HTML themes
astrojuanlu 3571426
Add text representation of the document structure
astrojuanlu e865c52
Add filenames as captions for relevant code blocks
astrojuanlu 106346a
Make section title more goal-oriented
astrojuanlu a3478eb
Update doc/tutorial/index.rst
astrojuanlu 5f41044
Tone down LaTeX info to "note"
astrojuanlu c9d2a73
Rename to root document
astrojuanlu 15fe52d
Separate extensions and themes more clearly
astrojuanlu 013e67f
Clarify what happens if no title is used in the :ref:
astrojuanlu 1383157
Keep old name for root document in the glossary
astrojuanlu d5f452a
Remove redundant local table of contents
astrojuanlu 74e565f
Consistent use of "build documentation"
astrojuanlu 50bd1c3
Remove unnecessary label
astrojuanlu f303a4a
Consistent use of "rst" for syntax highlight
astrojuanlu 6f9bc6e
Use "builtin" for extensions label as it is done for themes
astrojuanlu cb846a3
Add link to docutils docs
astrojuanlu b9da157
Merge branch '4.x' into new-tutorial-part-ii
astrojuanlu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,8 @@ Setting up your project and development environment | |
| In a new directory, create a file called ``README.rst`` with the following | ||
| content. | ||
|
|
||
| .. code-block:: rest | ||
| .. code-block:: rst | ||
| :caption: README.rst | ||
|
|
||
| Lumache | ||
| ======= | ||
|
|
@@ -124,7 +125,7 @@ The purpose of each of these files is: | |
| as some extra configuration keys. | ||
|
|
||
| ``source/index.rst`` | ||
| The :term:`master document` of the project, which serves as welcome page and | ||
| The :term:`root document` of the project, which serves as welcome page and | ||
| contains the root of the "table of contents tree" (or *toctree*). | ||
|
|
||
| Thanks to this bootstrapping step, you already have everything needed to render | ||
|
|
@@ -134,23 +135,32 @@ the documentation as HTML for the first time. To do that, run this command: | |
|
|
||
| (.venv) $ sphinx-build -b html docs/source/ docs/build/html | ||
|
|
||
| And finally, open `docs/build/html/index.html` in your browser. You should see | ||
| And finally, open ``docs/build/html/index.html`` in your browser. You should see | ||
| something like this: | ||
|
|
||
| .. image:: /_static/tutorial/lumache-first-light.png | ||
| .. figure:: /_static/tutorial/lumache-first-light.png | ||
| :width: 80% | ||
| :align: center | ||
| :alt: Freshly created documentation of Lumache | ||
|
|
||
| Freshly created documentation of Lumache | ||
|
|
||
| There we go! You created your first HTML documentation using Sphinx. | ||
|
|
||
| Making some tweaks to the index | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| First steps to document your project using Sphinx | ||
| ------------------------------------------------- | ||
|
|
||
| Building your HTML documentation | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| The ``index.rst`` file that ``sphinx-quickstart`` created has some content | ||
| already, and it gets rendered as the front page of our HTML documentation. It | ||
| already, and it gets rendered as the front page of your HTML documentation. It | ||
| is written in reStructuredText, a powerful markup language. | ||
|
|
||
| Modify the file as follows: | ||
|
|
||
| .. code-block:: rest | ||
| .. code-block:: rst | ||
| :caption: docs/source/index.rst | ||
|
|
||
| Welcome to Lumache's documentation! | ||
| =================================== | ||
|
|
@@ -184,6 +194,256 @@ as before, or leverage the convenience script as follows: | |
| After running this command, you will see that ``index.html`` reflects the new | ||
| changes! | ||
|
|
||
| Building your documentation in other formats | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Sphinx supports a variety of formats apart from HTML, including PDF, EPUB, | ||
| :ref:`and more <builders>`. For example, to build your documentation | ||
| in EPUB format, run this command from the ``docs`` directory: | ||
|
|
||
| .. code-block:: console | ||
|
|
||
| (.venv) $ make epub | ||
astrojuanlu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| After that, you will see the files corresponding to the e-book under | ||
| ``docs/build/epub/``. You can either open ``Lumache.epub`` with an | ||
| EPUB-compatible e-book viewer, like `Calibre <https://calibre-ebook.com/>`_, | ||
| or preview ``index.xhtml`` on a web browser. | ||
|
|
||
| .. note:: | ||
|
|
||
| To quickly display a complete list of possible output formats, plus some | ||
| extra useful commands, you can run :code:`make help`. | ||
|
|
||
| Each output format has some specific configuration options that you can tune, | ||
| :ref:`including EPUB <epub-options>`. For instance, the default value of | ||
| :confval:`epub_show_urls` is ``inline``, which means that, by default, URLs are | ||
| shown right after the corresponding link, in parentheses. You can change that | ||
| behavior by adding the following code at the end of your ``conf.py``: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| # EPUB options | ||
| epub_show_urls = 'footnote' | ||
|
|
||
| With this configuration value, and after running ``make epub`` again, you will | ||
| notice that URLs appear now as footnotes, which avoids cluttering the text. | ||
| Sweet! | ||
|
|
||
| .. note:: | ||
|
|
||
| Generating a PDF using Sphinx can be done running ``make latexpdf``, | ||
| provided that the system has a working :math:`\LaTeX` installation, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This breaks PDF production, as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| as explained in the documentation of :class:`sphinx.builders.latex.LaTeXBuilder`. | ||
| Although this is perfectly feasible, such installations are often big, | ||
| and in general LaTeX requires careful configuration in some cases, | ||
| so PDF generation is out of scope for this tutorial. | ||
|
|
||
| More Sphinx customization | ||
| ------------------------- | ||
|
|
||
| There are two main ways to customize your documentation beyond what is possible | ||
| with core Sphinx: extensions and themes. | ||
|
|
||
astrojuanlu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Enabling a built-in extension | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| In addition to these configuration values, you can customize Sphinx even more | ||
| by using :doc:`extensions </usage/extensions/index>`. Sphinx ships several | ||
| :ref:`builtin ones <builtin-extensions>`, and there are many more | ||
| :ref:`maintained by the community <third-party-extensions>`. | ||
|
|
||
| For example, to enable the :mod:`sphinx.ext.duration` extension, | ||
| locate the ``extensions`` list in your ``conf.py`` and add one element as | ||
| follows: | ||
|
|
||
| .. code-block:: python | ||
| :caption: docs/source/conf.py | ||
|
|
||
| # Add any Sphinx extension module names here, as strings. They can be | ||
| # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom | ||
| # ones. | ||
| extensions = [ | ||
| 'sphinx.ext.duration', | ||
| ] | ||
|
|
||
| After that, every time you generate your documentation, you will see a short | ||
| durations report at the end of the console output, like this one: | ||
|
|
||
| .. code-block:: console | ||
|
|
||
| (.venv) $ make html | ||
| ... | ||
| The HTML pages are in build/html. | ||
|
|
||
| ====================== slowest reading durations ======================= | ||
| 0.042 temp/source/index | ||
astrojuanlu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Using a third-party HTML theme | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Themes, on the other hand, are a way to customize the appearance of your | ||
| documentation. Sphinx has several :ref:`builtin themes <builtin-themes>`, and | ||
| there are also `third-party ones <https://sphinx-themes.org/>`_. | ||
|
|
||
| For example, to use the `Furo <https://pradyunsg.me/furo/>`_ third-party theme | ||
| in your HTML documentation, first you will need to install it with ``pip`` in | ||
| your Python virtual environment, like this: | ||
|
|
||
| .. code-block:: console | ||
|
|
||
| (.venv) $ pip install furo | ||
|
|
||
| And then, locate the ``html_theme`` variable on your ``conf.py`` and replace | ||
| its value as follows: | ||
|
|
||
| .. code-block:: python | ||
| :caption: docs/source/conf.py | ||
|
|
||
| # The theme to use for HTML and HTML Help pages. See the documentation for | ||
| # a list of builtin themes. | ||
| # | ||
| html_theme = 'furo' | ||
|
|
||
| With this change, you will notice that your HTML documentation has now a new | ||
| appearance: | ||
|
|
||
| .. figure:: /_static/tutorial/lumache-furo.png | ||
| :width: 80% | ||
| :align: center | ||
| :alt: HTML documentation of Lumache with the Furo theme | ||
|
|
||
| HTML documentation of Lumache with the Furo theme | ||
|
|
||
| Narrative documentation in Sphinx | ||
| --------------------------------- | ||
|
|
||
| Structuring your documentation across multiple pages | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| The file ``index.rst`` created by ``sphinx-quickstart`` is the :term:`root | ||
| document`, whose main function is to serve as a welcome page and to contain the | ||
astrojuanlu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| root of the "table of contents tree" (or *toctree*). Sphinx allows you to | ||
| assemble a project from different files, which is helpful when the project | ||
| grows. | ||
|
|
||
| As an example, create a new file ``docs/source/usage.rst`` (next to | ||
| ``index.rst``) with these contents: | ||
|
|
||
| .. code-block:: rst | ||
| :caption: docs/source/usage.rst | ||
|
|
||
| Usage | ||
astrojuanlu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ===== | ||
|
|
||
| Installation | ||
| ------------ | ||
|
|
||
| To use Lumache, first install it using pip: | ||
|
|
||
| .. code-block:: console | ||
|
|
||
| (.venv) $ pip install lumache | ||
|
|
||
| This new file contains two :ref:`section <rst-sections>` headers, normal | ||
| paragraph text, and a :rst:dir:`code-block` directive that renders | ||
| a block of content as source code, with appropriate syntax highlighting | ||
| (in this case, generic ``console`` text). | ||
|
|
||
| The structure of the document is determined by the succession of heading | ||
| styles, which means that, by using ``---`` for the "Installation" section | ||
| after ``===`` for the "Usage" section, you have declared "Installation" to | ||
| be a *subsection* of "Usage". | ||
|
|
||
| To complete the process, add a ``toctree`` :ref:`directive <rst-directives>` at | ||
| the end of ``index.rst`` including the document you just created, as follows: | ||
|
|
||
| .. code-block:: rst | ||
| :caption: docs/source/index.rst | ||
|
|
||
| Contents | ||
| -------- | ||
|
|
||
| .. toctree:: | ||
|
|
||
| usage | ||
|
|
||
| This step inserts that document in the root of the *toctree*, so now it belongs | ||
| to the structure of your project, which so far looks like this: | ||
|
|
||
| .. code-block:: text | ||
|
|
||
| index | ||
| └── usage | ||
|
|
||
| If you build the HTML documentation running ``make html``, you will see | ||
| that the ``toctree`` gets rendered as a list of hyperlinks, and this allows you | ||
| to navigate to the new page you just created. Neat! | ||
|
|
||
| .. warning:: | ||
|
|
||
| Documents outside a *toctree* will result in ``WARNING: document isn't | ||
| included in any toctree`` messages during the build process, and will be | ||
| unreachable for users. | ||
|
|
||
| Adding cross-references | ||
| ~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| One powerful feature of Sphinx is the ability to seamlessly add | ||
| :ref:`cross-references <xref-syntax>` to specific parts of the documentation: | ||
| a document, a section, a figure, a code object, etc. This tutorial is full of | ||
| them! | ||
|
|
||
| To add a cross-reference, write this sentence right after the | ||
| introduction paragraph in ``index.rst``: | ||
|
|
||
| .. code-block:: rst | ||
| :caption: docs/source/index.rst | ||
|
|
||
| Check out the :doc:`usage` section for further information. | ||
|
|
||
| The :rst:role:`doc` role you used automatically references a specific document | ||
| in the project, in this case the ``usage.rst`` you created earlier. | ||
|
|
||
| Alternatively, you can also add a cross-reference to an arbitrary part of the | ||
| project. For that, you need to use the :rst:role:`ref` role, and add an | ||
| explicit *label* that acts as `a target`__. | ||
|
|
||
| __ https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#hyperlink-targets | ||
astrojuanlu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| For example, to reference the "Installation" subsection, add a label right | ||
| before the heading, as follows: | ||
|
|
||
| .. code-block:: rst | ||
| :caption: docs/source/usage.rst | ||
| :emphasize-lines: 4 | ||
|
|
||
astrojuanlu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Usage | ||
| ===== | ||
|
|
||
| .. _installation: | ||
|
|
||
| Installation | ||
| ------------ | ||
|
|
||
| ... | ||
|
|
||
| And make the sentence you added in ``index.rst`` look like this: | ||
|
|
||
| .. code-block:: rst | ||
| :caption: docs/source/index.rst | ||
|
|
||
| Check out the :doc:`usage` section for further information, including how to | ||
| :ref:`install <installation>` the project. | ||
|
|
||
| Notice a trick here: the ``install`` part specifies how the link will look like | ||
| (we want it to be a specific word, so the sentence makes sense), whereas the | ||
| ``<installation>`` part refers to the actual label we want to add a | ||
| cross-reference to. If you do not include an explicit title, hence using | ||
| ``:ref:`installation```, the section title will be used (in this case, | ||
| ``Installation``). Both the ``:doc:`` and the ``:ref:`` roles will be rendered | ||
| as hyperlinks in the HTML documentation. | ||
astrojuanlu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Where to go from here | ||
| --------------------- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.