如何使用intersphinx正确编写外部文档的交叉引用?

Gal*_*all 21 python documentation opencv python-sphinx autodoc

我正在尝试将外部API的交叉引用添加到我的文档中,但我面临三种不同的行为.

我正在使用sphinx(1.3.1)和Python(2.7.3),我的intersphinx映射配置为:

{
'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)
}
Run Code Online (Sandbox Code Playgroud)

我可以毫不费力地编写numpy API的交叉引用,:class:`numpy.ndarray`或者:func:`numpy.array`像我们预期的那样给我一些像numpy.ndarray这样的东西.

但是,使用h5py,我可以生成链接的唯一方法是省略模块名称.例如,:class:`Group`(或:class:`h5py:Group`)给我Group:class:`h5py.Group`无法生成链接.

最后,我找不到一种方法来编写一个工作交叉引用OpenCV API,这些似乎都没有工作:

:func:`cv2.convertScaleAbs`
:func:`cv2:cv2.convertScaleAbs`
:func:`cv2:convertScaleAbs`
:func:`convertScaleAbs`
Run Code Online (Sandbox Code Playgroud)

如何正确编写对外部API的交叉引用,或配置intersphinx,以便在numpy情况下生成链接?

Gal*_*all 24

我试图了解objects.inv文件的内容再试一次,希望这次我检查numpy和h5py而不是OpenCV的那个.

如何阅读intersphinx库存文件

尽管我找不到任何有关读取object.inv文件内容的有用信息,但实际上使用intersphinx模块非常简单.

from sphinx.ext import intersphinx
import warnings


def fetch_inventory(uri):
    """Read a Sphinx inventory file into a dictionary."""
    class MockConfig(object):
        intersphinx_timeout = None  # type: int
        tls_verify = False

    class MockApp(object):
        srcdir = ''
        config = MockConfig()

        def warn(self, msg):
            warnings.warn(msg)

    return intersphinx.fetch_inventory(MockApp(), '', uri)


uri = 'http://docs.python.org/2.7/objects.inv'

# Read inventory into a dictionary
inv = fetch_inventory(uri)
# Or just print it
intersphinx.debug(['', uri])
Run Code Online (Sandbox Code Playgroud)

文件结构(numpy)

检查numpy之后,您可以看到密钥是域:

[u'np-c:function',
 u'std:label',
 u'c:member',
 u'np:classmethod',
 u'np:data',
 u'py:class',
 u'np-c:member',
 u'c:var',
 u'np:class',
 u'np:function',
 u'py:module',
 u'np-c:macro',
 u'np:exception',
 u'py:method',
 u'np:method',
 u'np-c:var',
 u'py:exception',
 u'np:staticmethod',
 u'py:staticmethod',
 u'c:type',
 u'np-c:type',
 u'c:macro',
 u'c:function',
 u'np:module',
 u'py:data',
 u'np:attribute',
 u'std:term',
 u'py:function',
 u'py:classmethod',
 u'py:attribute']
Run Code Online (Sandbox Code Playgroud)

当您查看特定域的内容时,您可以看到如何编写交叉引用.例如,py:class:

{u'numpy.DataSource': (u'NumPy',
  u'1.9',
  u'http://docs.scipy.org/doc/numpy/reference/generated/numpy.DataSource.html#numpy.DataSource',
  u'-'),
 u'numpy.MachAr': (u'NumPy',
  u'1.9',
  u'http://docs.scipy.org/doc/numpy/reference/generated/numpy.MachAr.html#numpy.MachAr',
  u'-'),
 u'numpy.broadcast': (u'NumPy',
  u'1.9',
  u'http://docs.scipy.org/doc/numpy/reference/generated/numpy.broadcast.html#numpy.broadcast',
  u'-'),
  ...}
Run Code Online (Sandbox Code Playgroud)

所以在这里,:class:`numpy.DataSource`将按预期工作.

h5py

在h5py的情况下,域名是:

[u'py:attribute', u'std:label', u'py:method', u'py:function', u'py:class']
Run Code Online (Sandbox Code Playgroud)

如果你看看py:class域名:

{u'AttributeManager': (u'h5py',
  u'2.5',
  u'http://docs.h5py.org/en/latest/high/attr.html#AttributeManager',
  u'-'),
 u'Dataset': (u'h5py',
  u'2.5',
  u'http://docs.h5py.org/en/latest/high/dataset.html#Dataset',
  u'-'),
 u'ExternalLink': (u'h5py',
  u'2.5',
  u'http://docs.h5py.org/en/latest/high/group.html#ExternalLink',
  u'-'),
 ...}
Run Code Online (Sandbox Code Playgroud)

这就是为什么我不能让它作为numpy引用工作.因此,格式化它们的好方法是:class:`h5py:Dataset`.

OpenCV的

OpenCV的库存对象似乎格格不入.在我希望找到域名的地方,实际上有902个功能签名:

[u':',
 u'AdjusterAdapter::create(const',
 u'AdjusterAdapter::good()',
 u'AdjusterAdapter::tooFew(int',
 u'AdjusterAdapter::tooMany(int',
 u'Algorithm::create(const',
 u'Algorithm::getList(vector<string>&',
 u'Algorithm::name()',
 u'Algorithm::read(const',
 u'Algorithm::set(const'
 ...]
Run Code Online (Sandbox Code Playgroud)

如果我们取第一个值:

{u'Ptr<AdjusterAdapter>': (u'OpenCV',
  u'2.4',
  u'http://docs.opencv.org/2.4/detectorType)',
  u'ocv:function 1 modules/features2d/doc/common_interfaces_of_feature_detectors.html#$ -')}
Run Code Online (Sandbox Code Playgroud)

我很确定用这个文件编写OpenCV交叉引用是不可能的......

结论

我认为intersphinx objects.inv标准方式生成基于文档项目内容的内容,但似乎并非如此.因此,似乎编写交叉引用的正确方法是依赖于API的,并且应该检查特定的库存对象以实际查看可用的内容.

  • 与其编写这个不再工作的 Python 程序,不如按照 [this other answer](/sf/answers/2578183891/): python -m sphinx 运行 intersphinx 模块来提取信息。 ext.intersphinx FILE.inv (3认同)
  • 旁注:opencv3删除了所有sphinx文档(赞成doxygen) (2认同)

lin*_*ish 21

除了@gall的详细解答之外,我发现它intersphinx也可以作为模块运行:

python -m sphinx.ext.intersphinx 'http://python-eve.org/objects.inv'
Run Code Online (Sandbox Code Playgroud)

这输出格式正确的信息.供参考:https://github.com/sphinx-doc/sphinx/blob/master/sphinx/ext/intersphinx.py#L390

  • 它适用于本地文件:`wget http:// python-eve.org/objects.inv`,然后是`python -m sphinx.ext.intersphinx object.inv` (3认同)
  • 非常好的发现.它只适用于1.4版本. (2认同)
  • 在Sphinx 1.4.3中被打破了吗?由于AttributeError,我得到`intersphinx inventory'http://docs.python.org/'不可读:'MockApp'对象没有属性'info' (2认同)
  • `python -m sphinx.ext.intersphinx'http:// python-eve.org/objects.inv'`在Sphinx 1.6.2中再次运行. (2认同)

Fra*_*las 5

检查objects.inv文件的另一种方法是使用sphobjinv模块。

您可以搜索本地甚至远程库存文件(具有模糊匹配)。以 scipy 为例:

$ sphobjinv suggest -t 90 -u https://docs.scipy.org/doc/scipy/reference/objects.inv "signal.convolve2d"

Remote inventory found.

:py:function:`scipy.signal.convolve2d`
:std:doc:`generated/scipy.signal.convolve2d`
Run Code Online (Sandbox Code Playgroud)

请注意,您可能需要使用:py:func:而不是:py:function:(我很乐意知道为什么)。

  • 在交叉引用中必须使用 `:func:` 而不是 `objects.inv` 中列出的 `:function:` 只是 Sphinx 中 Python 域的一个特性。所有这些术语都在[此处]定义(https://github.com/sphinx-doc/sphinx/blob/685e3fdb49c42b464e09ec955e1033e2a8729fff/sphinx/domains/python.py#L845-L881);您必须查阅底层“docutils”的文档才能了解所有语法的实际用途。 (6认同)