3 from sphinx.addnodes import desc_addname
4 from sphinx.domains.python import PyModulelevel, PyXRefRole
5 from sphinx.ext import autodoc
11 class TemplateTag(PyModulelevel):
12 indextemplate = "pair: %s; template tag"
14 def get_signature_prefix(self, sig):
15 return self.objtype + ' '
17 def handle_signature(self, sig, signode):
18 fullname, name_prefix = PyModulelevel.handle_signature(self, sig, signode)
20 for i, node in enumerate(signode):
21 if isinstance(node, desc_addname):
22 lib = '.'.join(node[0].split('.')[-2:])
23 new_node = desc_addname(lib, lib)
26 return fullname, name_prefix
29 class TemplateTagDocumenter(autodoc.FunctionDocumenter):
30 objtype = 'templatetag'
34 def can_document_member(cls, member, membername, isattr, parent):
35 # Only document explicitly.
38 def format_args(self):
41 class TemplateFilterDocumenter(autodoc.FunctionDocumenter):
42 objtype = 'templatefilter'
46 def can_document_member(cls, member, membername, isattr, parent):
47 # Only document explicitly.
51 app.add_directive_to_domain(DOMAIN, 'templatetag', TemplateTag)
52 app.add_role_to_domain(DOMAIN, 'ttag', PyXRefRole())
53 app.add_directive_to_domain(DOMAIN, 'templatefilter', TemplateTag)
54 app.add_role_to_domain(DOMAIN, 'tfilter', PyXRefRole())
55 app.add_autodocumenter(TemplateTagDocumenter)
56 app.add_autodocumenter(TemplateFilterDocumenter)