canmove, Confirmed users
737
edits
No edit summary |
|||
Line 1: | Line 1: | ||
__NOTOC__ | |||
== Writing Documentation for the Add-ons Builder SDK == | == Writing Documentation for the Add-ons Builder SDK == | ||
Line 5: | Line 6: | ||
The "cfx docs" command launches a small web server in order to render the documentation for human consumption. It relies on three things: | The "cfx docs" command launches a small web server in order to render the documentation for human consumption. It relies on three things: | ||
* That the documentation is written in Markdown syntax | * That the documentation is written in [http://daringfireball.net/projects/markdown/ Markdown syntax] | ||
* That the API component is described in APIDoc syntax | * That the API component is described in APIDoc syntax | ||
* That there are corresponding .js/.md files in the lib/docs directories (e.g. tabs.js/tabs.md) | * That there are corresponding .js/.md files in the lib/docs directories (e.g. tabs.js/tabs.md) | ||
== APIDoc Syntax == | |||
"APIDoc" is a simple markup syntax we've defined for documenting the component parts of an API. | |||
In APIDoc each component of an API - functions, constructors, properties, and so on - is enclosed in a pair of <code><api></api></code> tags. These are recognized by the documentation system, which parses them into a JSON structure and the renders them to HTML. | |||
The main benefit of using a defined syntax is that we can ensure that all APIs are consistently documented. Having said that, we'd like to move to a more standard syntax in the future. | |||
A good way to understand the syntax is to look at the existing module documentation, which can be found in your SDK installation, under <code>packages/addon-kit/docs/</code>. | |||
=== Component Types === | |||
== | APIDoc defines the following component types: | ||
* <code>function</code> | |||
* <code>class</code> | |||
* <code>constructor</code> | |||
* <code>method</code> | |||
* <code>property</code> | |||
* <code>event</code> | |||
The next section describes the parts of the syntax that are common across all component types, then we'll explain component-type-specific syntax in the subsequent sections. | |||
=== Common Component Syntax === | |||
In APIDoc each API component is enclosed inside <code><api></api></code> tags. | |||
* The opening tag contains a "name" attribute which is the identifier for the component. | |||
* The line immediately following the opening tag is the name of the component type, preceded by "@". | |||
* The lines following the component-type line contain the description for the component. You can include Markdown in the description. | |||
For example: | |||
<api name="postMessage"> | |||
@function | |||
Use this function to send a message | |||
to any [content scripts](/link/to/doc.html). | |||
</api> | |||
<api name="open"> | |||
@event | |||
This event is generated when the | |||
corresponding `tab` object is opened. | |||
</api> | |||
==== Component Scope ==== | |||
Certain components are allowed to contain other components: <code>class</code> and <code>property</code> components may contain <code>constructor</code>, <code>method</code>, <code>property</code>, and <code>event</code> components. So an object called <code>panel</code> that emits a <code>show</code> would be documented like this: | |||
<api name="panel"> | |||
@class | |||
... | |||
<api name="show"> | |||
@event | |||
... | |||
</api> | |||
</api> | |||
(The indentation doesn't have any meaning, it just makes things easier to read.) | |||
If the component isn't nested like this, that means it's defined at module scope. | |||
=== <code>constructor</code>, <code>function</code>, and <code>method</code> Syntax === | |||
The syntax for <code>function</code> and <code>method</code> types is identical: the only difference is that <code>method</code> components are nested inside <code>class</code> or <code>property</code> components, whereas <code>function</code> components aren't. The syntax for <code>constructor</code> is identical, except that it must be nested inside a <code>class</code> component and must not return a value. | |||
==== Parameters ==== | |||
You list parameters after the function's description using the "@param" tag. This is followed, on the same line, by the name of the parameter and its data type. The data type is enclosed in braces. Subsequent lines can describe the parameter: | |||
<api name="alert"> | |||
@method | |||
Displays an alert message. | |||
@param alerttext {string} | |||
The string to display. | |||
@param anotherparam {bool} | |||
Another, less useful, parameter. | |||
If the parameter is optional, then the name is enclosed in square brackets: | |||
<api name="alert"> | |||
@method | |||
Displays an alert message. | |||
@param [alerttext] {string} | |||
The string to display. | |||
If the parameter has a default value, then the name is followed by "=" and then the default value: | |||
<api name="alert"> | |||
@method | |||
Displays an alert message. | |||
@param [alerttext] {string} | |||
The string to display. | |||
@param [anotherparam=false] {bool} | |||
Another, less useful, parameter. | |||
Parameters can contain properties of their own. In this case the properties are listed after the parameter description, using a line which: | |||
* starts with "@prop" | |||
* is followed by the name of the property, enclosed in square brackets if the property is optional | |||
* is followed by the data type, enclosed in braces | |||
= | <api name="open"> | ||
@function | |||
Open a new window. | |||
@param options {object} | |||
An object containing configurable options for how this window will be opened, | |||
as well as a callback for being notified when the window has fully opened. | |||
@prop url {string} | |||
String URL to be opened in the new window. | |||
@prop [onOpen] {function} | |||
A callback function that is called when the window has opened. | |||
==== Return values ==== | |||
You can specify return values using the "@returns" tag. This is followed, on the same line, by the data type of the return value, enclosed in braces: | |||
<api name="get"> | |||
@method | |||
Make a `GET` request. | |||
@returns {Request} | |||
</api> | |||
=== <code>class</code> Syntax === | |||
<code>class</code> components don't have any special syntax, except that you can nest <code>constructor</code>, <code>method</code>, <code>property</code>, or <code>event</code> components inside them. | |||
=== <code>property</code> Syntax === | |||
<code>property</code> components indicate the data type of the property, enclosed in braces, immediately following the @property tag: | |||
<api name="label"> | |||
@property {string} | |||
The widget's label. Read-only. | |||
</api> | |||
You can nest <code>method</code>, <code>property</code>, or <code>event</code> components inside <code>property</code> components. | |||
=== <code>event</code> components === | |||
If the code emits events you use the <code>event</code> component to document them. The only extra bit of syntax events need is to define the arguments that will be passed into any listener functions, and this is done using a "@argument" tag, followed by the data type of the argument in braces: | |||
The | <api name="complete"> | ||
@event | |||
The `Request` object emits this event when the request has completed and a | |||
response has been received. | |||
@argument {Response} | |||
Listener functions are passed the response to the request as a `Response` object. | |||
</api> |