What is a Domain?
While this was always possible, it is now much easier to easily support documentation of projects using different programming languages or even ones not supported by the main Sphinx distribution, by providing a domain for every such purpose.
A domain is a collection of markup (reStructuredText :term:`directive`s and :term:`role`s) to describe and link to :term:`object`s belonging together, e.g. elements of a programming language. Directive and role names in a domain have names like domain:name, e.g. py:function. Domains can also provide custom indices (like the Python Module Index).
Having domains means that there are no naming problems when one set of documentation wants to refer to e.g. C++ and Python classes. It also means that extensions that support the documentation of whole new languages are much easier to write.
This section describes what the domains that come with Sphinx provide. The domain API is documented as well, in the section :ref:`domain-api`.
Most domains provide a number of :dfn:`object description directives`, used to describe specific objects provided by modules. Each directive requires one or more signatures to provide basic information about what is being described, and the content should be the description. The basic version makes entries in the general index; if no index entry is desired, you can give the directive option flag :noindex:. An example using a Python domain directive:
.. py:function:: spam(eggs) ham(eggs) :noindex: Spam or ham the foo.
The domains also provide roles that link back to these object descriptions. For example, to link to one of the functions described in the example above, you could say
The function :py:func:`spam` does a similar thing.
As you can see, both directive and role names contain the domain name and the directive name.
To avoid having to writing the domain name all the time when you e.g. only describe Python objects, a default domain can be selected with either the config value :confval:`primary_domain` or this directive:
If no other default is selected, the Python domain (named py) is the default one, mostly for compatibility with documentation written for older versions of Sphinx.
Directives and roles that belong to the default domain can be mentioned without giving the domain name, i.e.
.. function:: pyfunc() Describes a Python function. Reference to :func:`pyfunc`.
For cross-reference roles provided by domains, the same facilities exist as for general cross-references. See :ref:`xref-syntax`.
- You may supply an explicit title and reference target: :role:`title <target>` will refer to target, but the link text will be title.
- If you prefix the content with !, no reference/hyperlink will be created.
- If you prefix the content with ~, the link text will only be the last component of the target. For example, :py:meth:`~Queue.Queue.get` will refer to Queue.Queue.get but only display get as the link text.
The Python Domain
The Python domain (name py) provides the following directives for module declarations:
The following directives are provided for module and class contents:
Signatures of functions, methods and class constructors can be given like they would be written in Python, with the exception that optional parameters can be indicated by brackets:
.. py:function:: compile(source[, filename[, symbol]])
It is customary to put the opening bracket before the comma. In addition to this "nested" bracket style, a "flat" style can also be used, due to the fact that most optional parameters can be given independently:
.. py:function:: compile(source[, filename, symbol])
Default values for optional arguments can be given (but if they contain commas, they will confuse the signature parser). Python 3-style argument annotations can also be given as well as return type annotations:
.. py:function:: compile(source : string[, filename, symbol]) -> ast object
Info field lists
Inside Python object description directives, reST field lists with these fields are recognized and formatted nicely:
- param, parameter, arg, argument, key, keyword: Description of a parameter.
- type: Type of a parameter.
- raises, raise, except, exception: That (and when) a specific exception is raised.
- var, ivar, cvar: Description of a variable.
- returns, return: Description of the return value.
- rtype: Return type.
The field names must consist of one of these keywords and an argument (except for returns and rtype, which do not need an argument). This is best explained by an example:
.. py:function:: format_exception(etype, value, tb[, limit=None]) Format the exception with a traceback. :param etype: exception type :param value: exception value :param tb: traceback object :param limit: maximum number of stack frames to show :type limit: integer or None :rtype: list of strings
It is also possible to combine parameter type and description, if the type is a single word, like this:
:param integer limit: maximum number of stack frames to show
This will render like this:
Cross-referencing Python objects
The following roles refer to objects in modules and are possibly hyperlinked if a matching identifier is found:
The name enclosed in this markup can include a module name and/or a class name. For example, :py:func:`filter` could refer to a function named filter in the current module, or the built-in function of that name. In contrast, :py:func:`foo.filter` clearly refers to the filter function in the foo module.
Normally, names in these roles are searched first without any further qualification, then with the current module name prepended, then with the current module and class name (if any) prepended. If you prefix the name with a dot, this order is reversed. For example, in the documentation of Python's :mod:`codecs` module, :py:func:`open` always refers to the built-in function, while :py:func:`.open` refers to :func:`codecs.open`.
A similar heuristic is used to determine whether the name is an attribute of the currently documented class.
Also, if the name is prefixed with a dot, and no exact match is found, the target is taken as a suffix and all object names with that suffix are searched. For example, :py:meth:`.TarFile.close` references the tarfile.TarFile.close() function, even if the current module is not tarfile. Since this can get ambiguous, if there is more than one possible match, you will get a warning from Sphinx.
Note that you can combine the ~ and . prefixes: :py:meth:`~.TarFile.close` will reference the tarfile.TarFile.close() method, but the visible link caption will only be close().
The C Domain
The C domain (name c) is suited for documentation of C API.
Cross-referencing C constructs
The following roles create cross-references to C-language constructs if they are defined in the documentation:
The C++ Domain
The C++ domain (name cpp) supports documenting C++ projects.
The following directives are available:
These roles link to the given object types:
Note on References
It is currently impossible to link to a specific version of an overloaded method. Currently the C++ domain is the first domain that has basic support for overloaded methods and until there is more data for comparison we don't want to select a bad syntax to reference a specific overload. Currently Sphinx will link to the first overloaded version of the method / function.
The Standard Domain
The so-called "standard" domain collects all markup that doesn't warrant a domain of its own. Its directives and roles are not prefixed with a domain name.
The standard domain is also where custom object descriptions, added using the :func:`~sphinx.application.Sphinx.add_object_type` API, are placed.
There is a set of directives allowing documenting command-line programs:
There is also a very generic object description directive, which is not tied to any domain:
These roles are provided to refer to the described objects:
The reStructuredText domain
The reStructuredText domain (name rst) provides the following directives:
These roles are provided to refer to the described objects:
The sphinx-contrib repository contains more domains available as extensions; currently a Ruby and an Erlang domain.