Added doc_field_types to :class:JSCallable, arguments, errors and the return value of JavaScript functions/methods/constructors can now be documented using fields.

This commit is contained in:
Daniel Neuhäuser 2010-03-31 15:15:43 +00:00
parent f03f8356d1
commit ae0d464fb1
3 changed files with 46 additions and 3 deletions

View File

@ -554,9 +554,35 @@ The JavaScript domain (name **js**) provides the following directives:
.. directive:: .. js:function:: name(signature)
Describes a JavaScript function, method or constructor. If you want to
document optional arguments use square brackets as
describe arguments as optional use square brackets as
:ref:`documented <signatures>` for Python signatures.
You can use fields to give more details about arguments and their expected
types, errors which may be thrown by the function, and the value being
returned::
.. js:function:: $.getJSON(href, callback[, errback])
:param string href: An URI to the location of the resource.
:param callback: Get's called with the object.
:param errback:
Get's called in case the request fails. And a lot of other
text so we need multiple lines
:throws SomeError: For whatever reason in that case.
:returns: Something
This is rendered as:
.. js:function:: $.getJSON(href, callback[, errback])
:param string href: An URI to the location of the resource.
:param callback: Get's called with the object.
:param errback:
Get's called in case the request fails. And a lot of other
text so we need multiple lines.
:throws SomeError: For whatever reason in that case.
:returns: Something
.. directive:: .. js:data:: name
Describes a global variable or constant.

View File

@ -17,6 +17,7 @@ from sphinx.directives import ObjectDescription
from sphinx.domains.python import py_paramlist_re as js_paramlist_re
from sphinx.roles import XRefRole
from sphinx.util.nodes import make_refnode
from sphinx.util.docfields import Field, GroupedField, TypedField
js_sig_re = re.compile(
r'''([^ .]+\.)? # object name
@ -122,6 +123,17 @@ class JSCallable(JSObject):
"""Description of a JavaScript function, method or constructor."""
has_arguments = True
doc_field_types = [
TypedField("arguments", label=l_('Arguments'),
names=('argument', 'arg', 'parameter', 'param'),
typerolename='func', typenames=('paramtype', 'type')),
GroupedField('errors', label=l_('Throws'), rolename='err',
names=('throws', ),
can_collapse=True),
Field('returnvalue', label=l_('Returns'), has_arg=False,
names=('returns', 'return')),
]
class JSXRefRole(XRefRole):
def process_link(self, env, refnode, has_explicit_title, title, target):
# basically what sphinx.domains.python.PyXRefRole does
@ -143,6 +155,7 @@ class JavaScriptDomain(Domain):
"""JavaScript language domain."""
name = 'js'
label= 'JavaScript'
# if you add a new object type make sure to edit JSObject.get_index_string
object_types = {
'function' : ObjType(l_('js function'), 'func'),
'data' : ObjType(l_('js data'), 'data'),

View File

@ -81,11 +81,15 @@ Javascript items
.. js:data:: bar
.. documenting the method of any object
.. js:function:: bar.baz()
.. js:function:: bar.baz(href, callback[, errback])
:param string href: The location of the resource.
:param callback: Get's called with the data returned by the resource.
:throws InvalidHref: If the `href` is invalid.
:returns: `undefined`
.. js:attribute:: bar.spam
References
==========