首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否从sphinx autodoc中发出reStructuredText?

是否从sphinx autodoc中发出reStructuredText?
EN

Stack Overflow用户
提问于 2012-04-30 09:37:49
回答 2查看 1.8K关注 0票数 22

CPython的文档没有使用autodoc --我们使用的是手写的文章。

对于PEP3144( ipaddress模块),我想使用sphinx-apidoc来生成初始参考文档。这意味着我想运行一个两遍操作:

  1. 使用sphinx-apidoc为依赖于autodoc的模块发出Sphinx项目
  2. 运行创建新reStructuredText源文件的sphinx构建器,其中所有autodoc指令都替换为内联reStructuredText内容和标记,生成与

相同的输出。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-04 17:37:33

我也遇到了同样的问题,有一次我使用了相当丑陋的解决方案来修补Sphinx,见Make Sphinx generate RST class documentation from pydoc

票数 9
EN

Stack Overflow用户

发布于 2012-05-03 05:43:28

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

autodoc将auto指令转换为python指令。所以我们可以使用autodoc events来获得翻译后的python指令。

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

代码语言:javascript
复制
#!/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将创建

代码语言:javascript
复制
mymodule Module
===============

.. automodule:: mymodule
    :members:
    :undoc-members:
    :show-inheritance:

以下扩展(或对conf.py的补充):

代码语言:javascript
复制
NAMES = []
DIRECTIVES = {}

def get_rst(app, what, name, obj, options, signature,
            return_annotation):
    doc_indent = '    '
    directive_indent = ''
    if what in ['method', 'attribute']:
        doc_indent += '    '
        directive_indent += '    '
    directive = '%s.. py:%s:: %s' % (directive_indent, what, name)
    if signature:  # modules, attributes, ... don't have a signature
        directive += signature
    NAMES.append(name)
    rst = directive + '\n\n' + doc_indent + obj.__doc__ + '\n'
    DIRECTIVES[name] = rst 

def write_new_docs(app, exception):
    txt = ['My module documentation']
    txt.append('-----------------------\n')
    for name in NAMES:
        txt.append(DIRECTIVES[name])
    print '\n'.join(txt)
    with open('../doc_new/generated.rst', 'w') as outfile:
        outfile.write('\n'.join(txt))

def setup(app):
    app.connect('autodoc-process-signature', get_rst)
    app.connect('build-finished', write_new_docs)

将为您提供:

代码语言:javascript
复制
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所做的进一步处理必须适应此处的文档字符串。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10377576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档