22

CPython はドキュメントに autodoc を使用しません。手書きの散文を使用します。

PEP 3144 (ipaddress モジュール) については、sphinx-apidoc を使用して最初のリファレンス ドキュメントを生成したいと考えています。つまり、2 パス操作を実行したいということです。

  1. sphinx-apidoc を使用して、autodoc に依存するモジュールの Sphinx プロジェクトを発行します

  2. 新しい reStructuredText ソース ファイルを作成する sphinx ビルダーを実行します。すべての autodoc ディレクティブは、同じ出力を生成するインライン reStructuredText コンテンツとマークアップに置き換えられます。

最初のステップは簡単ですが、2 番目のステップを実行する方法がわかりません。また、これらの線に沿って既存のプロジェクトを検索する良い方法も思いつきません。

4

2 に答える 2

9

私は同じ問題を抱えていて、Sphinxにパッチを当てるために非常に醜いソリューションを使用したドキュメントの一度の生成については、Make Sphinx generate RST class documentation from pydoc を参照してください。

于 2012-05-04T09:37:33.170 に答える
3

完全な答えではなく、多かれ少なかれ出発点です:

autodocauto ディレクティブを 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作成します

mymodule Module
===============

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

次の拡張子 (または への追加conf.py):

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)

あなたに与える:

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 に適合させる必要があります。

于 2012-05-02T21:43:28.090 に答える