To reference label defined in any document of the project.
It allows linking across files, while the rest way
is limited to a location in the same file.
If the label definition is followed by a section title then
displayedtext can be omitted and will be replaced by the section
title.
E.g. the ReST – reStructuredTextExplicit hyperlink target section is preceded
with .._explicit_target:, so we have:
`Transition`_`how to draw an horizontal line <Transition>`_
we cannot use in the present chapter this ReST way of referencing a target
because ReST processor know only one document. but we can use it with the
autosectionlabel extension
with
| The section :ref:`Transition` show
|:ref:`how to draw an horizontal line <Transition>`| in your document.
Once you use the autosectionlabel extension
Sphinx will detect duplicate labels, in contrast with Implicit Hyperlink Targetsautolabel define a new label for each section, so if you have manually put a label
before a section title which is identical to the title, it will be detected as
duplicate.
These duplicate are harmless since they reference the same point. But some title in many
document can be identical, you can have many introduction or conclusion in different
parts. The duplicate may be problematic as any one can be matched by a reference. To
disembiguate the labels there is an configuration option beginning version 1.6autosectionlabel_prefix_document which prefix the automatic labels with with the
name of the document it is in, followed by a colon.
With this setting instead of :ref:`Transition` you have to use
:ref:`ReST:Transition` it avoid a potential ambiguity with a Transition paragraph
in an other document, and has the additional benefit to avoid also all ambiguities with
explicit labels in your documents.
The intersphinx extension
generates automatic links to the documentation
in other projects for objects not in your own project. It interprets
the references to roles
To configure it, give in conf.py a dictionary like:
Here extlinks is a dictionary, the second item of the tuple is the default
caption, it must contain exactly one %s which will be replaced by the
value substituted in the link. The second element can also be None, in
this case the full url is used.
with this setting when you write:
See also :sphinx:`Using Sphinx <using.html>` or :sphinx:`faq.html`.
The math directive is yet present in docutils ReST, but the rendering in
Sphinx, which is done through extensions, extend the original docutils math
extension. We give here some examples of its use.
You then can type standard LaTeX math expressions, either inline:
:math:`‹LaTeX math expression›`
or in display mode:
..math::
‹LaTeX math expressions›
The second version is also available for a one line expression:
Several lines of math expressions can be entered by leaving a blank line between
them. In addition there is something like an align environment syntax if
lines are not separated by a blank line.
Explicit LaTeX with amsmath mechanism
If the option nowrap is specified then the full LaTeX code (including the
math-environment) has to be given. We can assume that the amsmath
package is loaded. This is not limited to math typesetting, any LaTeX
construct can be rendered in this way.
Entries in the Index
are created automatically from all information units
like modules, classes, functions or attributes but those with a :noindex:
option.
The first two versions create single (sub-)entries, while pair
creates two entries “‹1st part›; ‹2nd part›” and “‹2nd part›; <1st
part›”; and triple makes three entries.
With the exclamation mark, the <entry 3> is the main entry for this
term and is put in bold.
You can also use the keywords see and seealso with see:foobar or seealso:barfoo.
A block may be included depending of the presence of some tag
(Sphinx ref):
..only::<expression>
The expression is made of tags combined in boolean expressions
like htmlanddraft.
The format and the name of the current
builder is set as predefined tag, if needed it can be prefixed to differentiate
format and builer, like format_html or builder_html
You can define tags via the -t command-line option of
sphinx-build.
In the configuration file you can use
tags.has('tag') to query,
tags.add('tag') and tags.remove('tag') to change.
..role:: red
An example of :red:`red text`.
..container:: red
Here the full block is red.
An undecorated paragraph.
..class:: red
This paragraph too is is red.
..admonition:: Big warning
:class: red
Big warning text is red.
After applying rst2html you get:
<p>An example of <spanclass="red">red text</span>.</p><divclass="red container">
Here the full block of test is red.</div><p>An undecorated paragraph.</p><pclass="red">This paragraph too is is red.</p><divclass="red admonition"><pclass="first admonition-title">Big warning</p><pclass="last">Big warning text is red.</p>
Here I have taken the admonition directive as example but any directive that
allow the :class: option would do.
As it generates a span the role directive is the only one that allow to
apply your style to a part of a paragraph.
The class works as expected with rest2html, but directive fail with
Sphinx. You have to replace it with
..rst-class:: red
This paragraph too is is red.
Sphinx use rst-class to replace the ReSt class
directive that is shadowed by Sphinx. This is only stated in a small
footnote of Sphinx reSt Primer.
You put it in a stylesheet, to give it’s location:
With rst2html you must specify the stylesheet’s location with a
--stylesheet (for a URL) or --stylesheet-path for a local file.
With Sphinx a flexible solution is to add your own css file in the
_static directory and give its location with a template that
you put in the _template directory. You can use a file layout.html
wich extend the original template of the same name:
The code blocks are highlighted by sphinx, there is a default language
of Python that can be changed in the configuration, by setting the
configuration option highlight_language.
The default Highlighting language used by Pygment in Literal Blocks is
set for following snippets of code examples by:
The additional linenothreshold option switches on line numbering
for blocks of size beyond ‹number› lines.
When using Sphinx you can specify the highlighting in a single literal
block:
..code-block:: ‹language›
:linenos:
‹body›
The linenos option switches on line numbering.
You can also use some options that are described for the
Source code include., namely linenos, lineno-start,
caption, name, dedent.
When using base ReST parser use instead code keyword.
automodule, autoclass, autoexception.
Document a module, class or exception. They insert the docstring
of the object itself; if you add a :members: option followed by
a specific list of members, they will be included in the
documentation.
An empty list of members includes all members.
..autoclass:: Noodle
:members: eat, slurp
autofunction, autodata, automethod, autoattribute are used to
document the respective type of object. autodata and autoattribute
support an annotation option taht will show not only the name but the
representation of the object.
defdivide(self,i,j):"""Divide two numbers. This function is to show how the docstring look, using pure Sphinx syntax, with explicit fields. :param i: numerator :type i: int :param j: denominator :type j: int :return: quotient :rtype: integer :raises: :exc:`ZeroDivisionError`"""
arithmetic.divide.divide(self, i, j)
Divide two numbers.
This function is to show how the docstring look, using pure Sphinx syntax,
with explicit fields.
The previous fields have a very good rendering in Sphinx, but
they are quite cumbersome for the programmer and make the code cryptic. Two alternates
syntaxes are popular the Numpy docstring syntax and the Google docstring syntax.
These two styles are not recognized directly by the autocode extension but should
preprocessed.
The extension napoleon preprocess NumPy and
Google style docstrings and converts them to reStructuredText before Sphinx parse the
source code. You need to configure it in your conf.py file.
defexample(arg1,arg2):""" Divide two float numbers This is a Numpy docstring example. Parameters ---------- arg1 : float the first value arg2 : float the second value Returns ------- float arg1 divided by arg2 Raises ------ ZeroDivisionError when the second value is null. Examples -------- >>> import template >>> a = MainClass() >>> a.example(6,2) 1.5 Warning ------- arg2 must be non-zero. Notes ----- Make sure we are using numpy syntax [1]_ See Also -------- :py:exc:`ZeroDivisionError` Todo ---- We should use type inference References ---------- .. [1] `Numpy Style Guide`_ """
defexample(arg1,arg2):"""Division of two reals. This function is only to show how we can format docstrings using Google Style Guide. Args: - arg1 (float): The first value. - arg2 (float): The second value, cannot be Nul. Returns: float: Division arg1/arg2. Raises: ZeroDivisionError: if arg2 is zero. Example: >>> import template >>> a = MainClass() >>> a.example(6,2) 1.5 Todo: Check that arg2 is non zero, and throw an exception."""
arithmetic.gstyle_docstring.example(arg1, arg2)
Division of two reals.
This function is only to show how we can format docstrings using
Google Style Guide.
We may be curious to know what exactly napoleon generates. It can
allow us to fix error in the output by changing our docstring or
adding Sphinx fields.
We can call napoleon from python to learn what it generates:
>>> docstring="""... Division of two reals.......""">>> from sphinx.ext.napoleon import Config>>> from sphinx.ext.napoleon.docstring import GoogleDocstring>>> config = Config(napoleon_use_param=True, napoleon_use_rtype=True)>>> print(GoogleDocstring(docstring), config)
And we have the decorated result:
Division of two reals.
This function is only to show how we can format docstrings using
Google Style Guide.
:param - arg1: The first value.
:type - arg1: float
:param - arg2: The second value, cannot be Nul.
:type - arg2: float
:returns: Division arg1/arg2.
:rtype: float
:raises::exc:`ZeroDivisionError` -- if arg2 is zero.
..rubric:: Example
>>> import template
>>> a = MainClass()
>>> a.example(6,2)
1.5
..todo:: Check that arg2 is non zero, and throw an exception.
If you want to experiment with output of napoleon you can look at
the parameters of GoogleDocstring in the Napoleon source code