Dox is a JavaScript documentation generator written with node. Dox no longer generates an opinionated structure or style for your docs, it simply gives you a JSON representation, allowing you to use markdown and JSDoc-style tags.
Install from npm:
$ npm install -g dox
Usage: dox [options]
Options:
-h, --help output usage information
-v, --version output the version number
-d, --debug output parsed comments for debugging
Examples:
# stdin
$ dox > myfile.json
# operates over stdio
$ dox < myfile.js > myfile.json
dox(1)
operates over stdio:
$ dox < utils.js
...JSON...
to inspect the generated data you can use the --debug
flag, which is easier to read than the JSON output:
$ dox --debug < utils.js
utils.js:
/**
* Escape the given `html`.
*
* Examples:
*
* utils.escape('<script></script>')
* // => '<script></script>'
*
* @param {String} html string to be escaped
* @return {String} escaped html
* @api public
*/
exports.escape = function(html){
return String(html)
.replace(/&(?!\w+;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
};
output:
[
{
"tags": [
{
"type": "param",
"types": [
"String"
],
"name": "html",
"description": "string to be escaped"
},
{
"type": "return",
"types": [
"String"
],
"description": "escaped html"
},
{
"type": "api",
"visibility": "public"
}
],
"description": {
"full": "<p>Escape the given <code>html</code>.</p>\n\n<h2>Examples</h2>\n\n<pre><code>utils.escape('&lt;script&gt;&lt;/script&gt;')\n// =&gt; '&lt;script&gt;&lt;/script&gt;'\n</code></pre>",
"summary": "<p>Escape the given <code>html</code>.</p>",
"body": "<h2>Examples</h2>\n\n<pre><code>utils.escape('&lt;script&gt;&lt;/script&gt;')\n// =&gt; '&lt;script&gt;&lt;/script&gt;'\n</code></pre>"
},
"isPrivate": false,
"ignore": false,
"code": "exports.escape = function(html){\n return String(html)\n .replace(/&(?!\\w+;)/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>');\n};",
"ctx": {
"type": "method",
"receiver": "exports",
"name": "escape",
"string": "exports.escape()"
}
}
]
This output can then be passed to a template for rendering. Look below at the "Comments" section for details.
A "comment" is comprised of the following detailed properties:
- description
- tags
- code
- ctx
- isPrivate
- ignore
A dox description is comprised of three parts, the "full" description, the "summary", and the "body". The following example has only a "summary", as it consists of a single paragraph only, therefore the "full" property has only this value as well.
/**
* Output the given `str` to _stdout_.
*/
exports.write = function(str) {
process.stdout.write(str);
};
yields:
description:
{ full: '<p>Output the given <code>str</code> to <em>stdout</em>.</p>',
summary: '<p>Output the given <code>str</code> to <em>stdout</em>.</p>',
body: '' },
Large descriptions might look something like the following, where the "summary" is still the first paragraph, the remaining description becomes the "body". Keep in mind this is markdown, so you can indent code, use lists, links, etc. Dox also augments markdown, allowing "Some Title:\n" to form a header.
/**
* Output the given `str` to _stdout_
* or the stream specified by `options`.
*
* Options:
*
* - `stream` defaulting to _stdout_
*
* Examples:
*
* mymodule.write('foo')
* mymodule.write('foo', { stream: process.stderr })
*
*/
exports.write = function(str, options) {
options = options || {};
(options.stream || process.stdout).write(str);
};
yields:
description:
{ full: '<p>Output the given <code>str</code> to <em>stdout</em><br />or the stream specified by <code>options</code>.</p>\n\n<h2>Options</h2>\n\n<ul>\n<li><code>stream</code> defaulting to <em>stdout</em></li>\n</ul>\n\n<h2>Examples</h2>\n\n<pre><code>mymodule.write(\'foo\')\nmymodule.write(\'foo\', { stream: process.stderr })\n</code></pre>',
summary: '<p>Output the given <code>str</code> to <em>stdout</em><br />or the stream specified by <code>options</code>.</p>',
body: '<h2>Options</h2>\n\n<ul>\n<li><code>stream</code> defaulting to <em>stdout</em></li>\n</ul>\n\n<h2>Examples</h2>\n\n<pre><code>mymodule.write(\'foo\')\nmymodule.write(\'foo\', { stream: process.stderr })\n</code></pre>' }
Dox also supports JSdoc-style tags. Currently only @api is special-cased,
providing the comment.isPrivate
boolean so you may omit "private" utilities etc.
@param
tags also support sub-parameters, identified by @...
placed directly
underneith the @param
tag. This is useful for parameters that accept
configuration objects. Sub-parameter tags are then placed in tag.subParams
.
If dox does not have specific functionality for a particular tag, the JSON entry
for that tag will only have a name
, type
and a description
attribute.
/**
* Output the given `str` to _stdout_
* or the stream specified by `options`.
*
* @param {String} str
* @param {Object} options
* @... {stream} stream the stream reference
* @return {Object} exports for chaining
*/
exports.write = function(str, options) {
options = options || {};
(options.stream || process.stdout).write(str);
return this;
};
yields:
tags:
[ { type: 'param',
types: [ 'String' ],
name: 'str',
description: '' },
{ type: 'param',
types: [ 'Object' ],
name: 'options',
subParams: [
{
type: '...',
types: [ 'stream' ],
name: 'stream',
description: 'the stream reference'
}],
description: '' },
{ type: 'return',
types: [ 'Object' ],
description: 'exports for chaining' },
{ type: 'api',
visibility: 'public' } ]
The .code
property is the code following the comment block, in our previous examples:
exports.write = function(str, options) {
options = options || {};
(options.stream || process.stdout).write(str);
return this;
};
The .ctx
object indicates the context of the code block, is it a method, a function,
a variable etc. Below are some examples:
exports.write = function(str, options) {
};
yields:
ctx:
{ type: 'method',
receiver: 'exports',
name: 'write',
string: 'exports.write()' } }
var foo = 'bar';
yields:
ctx:
{ type: 'declaration',
name: 'foo',
value: '\'bar\'',
string: 'foo' }
function User() {
}
yields:
ctx:
{ type: 'function',
name: 'User',
string: 'User()' } }
This boolean identifies the commented code as private. If the @api
tag is used the .isPrivate
boolean will be true.
This boolean, .ignore
, identifies the comment as worth ignoring. Comments and their associated bodies
of code may be flagged with "!" to trigger this boolean ignoring, these are typically things like file comments
containing copyright etc, however you of course can output them in your templates if you want.
/**
* Normal comment.
*/
vs
/*!
* Sets the ignore flag to true.
*/
Install dev dependencies and execute make test
:
$ npm install -d
$ make test
(The MIT License)
Copyright (c) 2011 TJ Holowaychuk <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.