从sphinx autodoc发出RestructedText?

2024-05-23 14:36:28 发布

您现在位置:Python中文网/ 问答频道 /正文

CPython的文档不使用autodoc,我们使用手写体。在

对于pep3144(ipaddress模块),我希望使用sphinxapidoc生成初始参考文档。这意味着我要进行两次操作:

  1. 使用sphinx apidoc为依赖autodoc的模块发出sphinx项目

  2. 运行一个sphinx构建器来创建新的structuredText源文件,所有autodoc指令都替换为产生相同输出的inline restructedText内容和标记

第一步很简单,但我不知道如何做第二步,甚至想不出好的方法来搜索任何现有的项目。在


Tags: 模块项目文档sphinx指令inlinecpythonstructuredtext
2条回答

我也遇到了同样的问题,有一次生成文档时,我使用了非常难看的解决方案来修补Sphinx,请参见Make Sphinx generate RST class documentation from pydoc。在

不是一个完整的答案,或多或少是一个起点:

autodoc将自动指令转换为python指令。 因此可以使用autodoc事件来获取翻译后的python指令。在

例如,如果您有以下mymodule.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
This is my module.
"""

def my_test_func(a, b=1):
    """This is my test function"""
    return a + b 

class MyClass(object):
    """This is my class"""

    def __init__(x, y='test'):
        """The init of my class"""
        self.x = float(x)
        self.y = y 

    def my_method(self, z): 
        """This is my method.

        :param z: a number
        :type z: float, int
        :returns: the sum of self.x and z
        :rtype: float
        """
        return self.x + z 

sphinx-apidoc将创建

^{pr2}$

以下扩展名(或conf.py的加法):

^{3}$

会给你:

My module documentation
           -

.. py:module:: mymodule


This is my module.


.. py:class:: mymodule.MyClass(x, y='test')

    This is my class

    .. py:method:: mymodule.MyClass.my_method(z)

        This is my method.

        :param z: a number
        :type z: float, int
        :returns: the sum of self.x and z
        :rtype: float


.. py:function:: mymodule.my_test_func(a, b=1)

    This is my test function

但是,由于autodoc不发出任何事件,因此当翻译完成时,autodoc所做的进一步处理必须适应这里的docstring。在

相关问题 更多 >