问题
I have started documenting a Python project using Sphinx. It is the first time I use it - I am used to tools which work with a JavaDoc-like syntax, and I have some doubts.
Since I want the documentation to appear near the code, I make use of the .. automodule::
, .. autoclass::
and .. automethod::
directives. So the structure of my documentation is as follows: index.rst
contains the TOC and
.. automodule:: my_main_package
and then the top-level __init__.py
contains directives like
.. automodule:: some_subpackage
for each subpackage and so on. Finally each module contains directives
.. autoclass:: some_class
:members:
for each class in the module.
This mostly works, but what I get is a single page documentation, which is a little odd to work with.
How should I organize my documentation in order to obtain a tree of hyperlinked files? That is, the main package should contain its own documentation and links to each of its subpackages and so on, until each module has its own page.
回答1:
I found this autopackage script from a comment here. It generates the necessary .rst files according to the structure of your packages.
side note: I still feel I am missing something, as I cannot believe that a tool like Sphinx, which is renowned as the most advanced tool for documenting Python, misses the functionality to do basic API documentation. Hence I will leave the question open for a while before accepting my own answer.
回答2:
Even I am not an expert in this, but I think I can answer what you have asked here(about having the organization of the documentation/ rst files).
The key you may be missing here is instead of using the autoclass/automodule/automethod calls in the same top level TOC
s rst-file, this top level file should contain links to other rst files containing these calls.
suppose you have all rst files inside doc
dir (and subdirs),
Table of contents
=================
The contents of the docs are:
.. toctree::
:maxdepth: 1
module_1/index
module_2/index
in the dir containing this top level index.rst
, you will have subdirs with name module_1
and module_2
. These will have index.rst
(name is just example specific) which in turn will contain the .. automodule::
, .. autoclass::
, and .. automethod::
. It can contain something like
:mod:`module_1`
---------------
..automodule:: module_1
:show-inheritance:
.. autoclass:: module_1.MyClass
Of course, this is not something like standard, or ideal way of doing, I am suggesting this because it's neater. you can alternatively have all the rst files with module/class/method docs in the same dir as the top level index.rst, with structure
Table of contents
=================
The contents of the docs are:
.. toctree::
:maxdepth: 1
module_1
module_2
and the same dir will contain the doc-files module_1.rst
, module_2.rst
etc. The paths are relative to the config.py
file.
回答3:
I am by no means an expert on Sphinx, but from reading the documentation it seems that you can include TOC trees in subdirectories. The TOC tree page also gives some information on paths within the tree; have you experimented with using paths in the tree?
来源:https://stackoverflow.com/questions/5319210/structuring-sphinx-documentation