I'm trying to add cross-references to external API into my documentation but I'm facing three different behaviors.
I am using sphinx(1.3.1) with Python(2.7.3) and my intersphinx mapping is configured as:
{
'python': ('https://docs.python.org/2.7', None),
'numpy': ('http://docs.scipy.org/doc/numpy/', None),
'cv2' : ('http://docs.opencv.org/2.4/', None),
'h5py' : ('http://docs.h5py.org/en/latest/', None)
}
I have no trouble writing a cross-reference to numpy API with :class:`numpy.ndarray`
or :func:`numpy.array`
which gives me, as expected, something like numpy.ndarray.
However, with h5py, the only way I can have a link generated is if I omit the module name. For example, :class:`Group`
(or :class:`h5py:Group`
) gives me Group but :class:`h5py.Group`
fails to generate a link.
Finally, I cannot find a way to write a working cross-reference to OpenCV API, none of these seems to work:
:func:`cv2.convertScaleAbs`
:func:`cv2:cv2.convertScaleAbs`
:func:`cv2:convertScaleAbs`
:func:`convertScaleAbs`
How to properly write cross-references to external API, or configure intersphinx, to have a generated link as in the numpy case?
I gave another try on trying to understand the content of an
objects.inv
file and hopefully this time I inspected numpy and h5py instead of only OpenCV's one.How to read an intersphinx inventory file
Despite the fact that I couldn't find anything useful about reading the content of an
object.inv
file, it is actually very simple with the intersphinx module.File structure (numpy)
After inspecting numpy's one, you can see that keys are domains:
You can see how you can write your cross-reference when you look at the content of a specific domain. For example,
py:class
:So here,
:class:`numpy.DataSource`
will work as expected.h5py
In the case of h5py, the domains are:
and if you look at the
py:class
domain:That's why I couldn't make it work as numpy references. So a good way to format them would be
:class:`h5py:Dataset`
.OpenCV
OpenCV's inventory object seems malformed. Where I would expect to find domains there is actually 902 function signatures:
and if we take the first one's value:
I'm pretty sure it is then impossible to write OpenCV cross-references with this file...
Conclusion
I thought intersphinx generated the
objects.inv
based on the content of the documentation project in an standard way, which seems not to be the case. As a result, it seems that the proper way to write cross-references is API dependent and one should inspect a specific inventory object to actually see what's available.In addition to the detailed answer from @gall, I've discovered that
intersphinx
can also be run as a module:This outputs nicely formatted info. For reference: https://github.com/sphinx-doc/sphinx/blob/master/sphinx/ext/intersphinx.py#L390
How to use OpenCV 2.4 (cv2) intersphinx
Inspired by @Gall's answer, I wanted to compare the contents of the OpenCV & numpy inventory files. I couldn't get
sphinx.ext.intersphinx.fetch_inventory
to work from ipython, but the following does work:numpy.inv has lines like this:
whereas cv2.inv has lines like this:
So presumably you'd link to the OpenCV docs with
:ocv:pyfunction:`cv2.imread`
instead of:py:function:`cv2.imread`
. Sphinx doesn't like it though:A bit of Googling revealed that the OpenCV project has its own "ocv" sphinx domain: https://github.com/opencv/opencv/blob/2.4/doc/ocv.py -- presumably because they need to document C, C++ and Python APIs all at the same time.
To use it, save
ocv.py
next to your Sphinxconf.py
, and modify yourconf.py
:In your rst files you need to say
:ocv:pyfunc:`cv2.imread`
(not:ocv:pyfunction:
).Sphinx prints some warnings (
unparseable C++ definition: u'cv2.imread'
) but the generated html documentation actually looks ok with a link to http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#cv2.imread. You can editocv.py
and remove the line that prints that warning.The accepted answer no longer works in the new version (1.5.x) ...