3

Django プロジェクトを文書化するために autodoc で Sphinx を使用しています。

デザイン担当者は、プロジェクトで定義されているすべてのテンプレート タグに関するドキュメント ページを必要としています。もちろん、テンプレートの処理関数をすべて手で列挙すれば、このようなページを作成できますが、DRY ではないと思いますよね。実際、テンプレート タグ処理関数はすべて@register.inclusion_tagデコレータでマークされています。したがって、何らかのルーチンでそれらをすべて収集して文書化することは可能で自然なことのようです。

フィルター機能についても同様です。

私はそれをグーグルで検索し、Djangoのドキュメントを検索しましたが、そうではありませんでした。このような自然な機能が誰かによって実装されていないことが信じられません。

4

3 に答える 3

3

この時点で止まらず、Sphinx の autodoc 拡張機能を実装しました。

スニペット 2. Sphinx autodoc 拡張機能

"""
    Extension of Sphinx autodoc for Django template tag libraries.

    Usage:
       .. autotaglib:: some.module.templatetags.mod
           (options)

    Most of the `module` autodoc directive flags are supported by `autotaglib`.     

    Andrew "Hatter" Ponomarev, 2010
"""

from sphinx.ext.autodoc import ModuleDocumenter, members_option, members_set_option, bool_option, identity
from sphinx.util.inspect import safe_getattr

from django.template import get_library, InvalidTemplateLibrary

class TaglibDocumenter(ModuleDocumenter):           
    """
    Specialized Documenter subclass for Django taglibs.
    """
    objtype = 'taglib'
    directivetype = 'module'
    content_indent = u''

    option_spec = {
        'members': members_option, 'undoc-members': bool_option,
        'noindex': bool_option,
        'synopsis': identity,
        'platform': identity, 'deprecated': bool_option,
        'member-order': identity, 'exclude-members': members_set_option,
    }

    @classmethod
    def can_document_member(cls, member, membername, isattr, parent):
        # don't document submodules automatically
        return False

    def import_object(self):
        """
        Import the taglibrary.

        Returns True if successful, False if an error occurred.
        """
        # do an ordinary module import      
        if not super(ModuleDocumenter, self).import_object():
            return False        

        try:    
            # ask Django if specified module is a template tags library
            # and - if it is so - get and save Library instance         
            self.taglib = get_library(self.object.__name__)
            return True
        except InvalidTemplateLibrary, e:
            self.taglib = None
            self.directive.warn(unicode(e))

        return False    

    def get_object_members(self, want_all):
        """
        Decide what members of current object must be autodocumented.

        Return `(members_check_module, members)` where `members` is a
        list of `(membername, member)` pairs of the members of *self.object*.

        If *want_all* is True, return all members.  Else, only return those
        members given by *self.options.members* (which may also be none).
        """
        if want_all:
            return True, self.taglib.tags.items()
        else:
            memberlist = self.options.members or []
        ret = []
        for mname in memberlist:
            if mname in taglib.tags:
                ret.append((mname, self.taglib.tags[mname]))
            else:
                self.directive.warn(
                    'missing templatetag mentioned in :members: '
                    'module %s, templatetag %s' % (
                    safe_getattr(self.object, '__name__', '???'), mname))
        return False, ret

def setup(app):
    app.add_autodocumenter(TaglibDocumenter)

この拡張機能は、automodule のように動作する Sphinx ディレクティブautotaglibを定義しますが、関数を実装するタグのみを列挙します。

例:

.. autotaglib:: lib.templatetags.bfmarkup
   :members:
   :undoc-members:
   :noindex:
于 2010-10-12T22:23:51.527 に答える
1

記録のために、Django には自動ドキュメンテーション システムがあります (あなたの に追加django.contrib.admindocsしてくださいINSTALLED_APPS)。

/admin/docs/これにより、モデル、ビュー (URL に基づく)、テンプレート タグ、およびテンプレート フィルターを表す、管理画面 (通常は ) に追加のビューが提供されます。

これに関する詳細なドキュメントは、admindocs セクションにあります。

そのコードを見て、ドキュメントやDjango ドキュメントの拡張機能に含めることができます。

于 2011-06-08T18:23:53.927 に答える
0

私は問題を解決し、私のスニペットを共有したいと思います - それらが誰かに役立つ場合に備えて.

スニペット 1. シンプルなドキュメンター

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'

from django.template import get_library

def show_help(libname):
    lib = get_library(libname)
    print lib, ':'
    for tag in lib.tags:
        print tag
        print lib.tags[tag].__doc__


if __name__ == '__main__':
    show_help('lib.templatetags.bfmarkup')

このスクリプトを実行する前に、PYTHONPATH 環境変数を設定する必要があります。

于 2010-10-12T22:14:38.847 に答える