Pages

Wednesday, 31 May 2017

Dox和Doxmate

javaScript documentation generator for node using markdown and jsdoc.
Build Status
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.

Installation

Install from npm:
$ npm install -g dox

Usage Examples

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`.
 *
 * @example
 *     utils.escape('<script></script>')
 *     // => '&lt;script&gt;&lt;/script&gt;'
 *
 * @param {String} html string to be escaped
 * @return {String} escaped html
 * @api public
 */

exports.escape = function(html){
  return String(html)
    .replace(/&(?!\w+;)/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;');
};
output:
[
  {
    "tags": [
      {
        "type": "example",
        "string": "    utils.escape('<script></script>')\n    // => '&lt;script&gt;&lt;/script&gt;'",
        "html": "<pre><code>utils.escape(&#39;&lt;script&gt;&lt;/script&gt;&#39;)\n// =&gt; &#39;&amp;lt;script&amp;gt;&amp;lt;/script&amp;gt;&#39;\n</code></pre>"
      },
      {
        "type": "param",
        "string": "{String} html string to be escaped",
        "types": [
          "String"
        ],
        "name": "html",
        "description": "string to be escaped"
      },
      {
        "type": "return",
        "string": "{String} escaped html",
        "types": [
          "String"
        ],
        "description": "escaped html"
      },
      {
        "type": "api",
        "string": "public",
        "visibility": "public"
      }
    ],
    "description": {
      "full": "<p>Escape the given <code>html</code>.</p>",
      "summary": "<p>Escape the given <code>html</code>.</p>",
      "body": ""
    },
    "isPrivate": false,
    "ignore": false,
    "code": "exports.escape = function(html){\n  return String(html)\n    .replace(/&(?!\\w+;)/g, '&amp;')\n    .replace(/</g, '&lt;')\n    .replace(/>/g, '&gt;');\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 "Properties" section for details.

Usage


Usage: dox [options]

  Options:

    -h, --help                     output usage information
    -V, --version                  output the version number
    -r, --raw                      output "raw" comments, leaving the markdown intact
    -a, --api                      output markdown readme documentation
    -s, --skipPrefixes [prefixes]  skip comments prefixed with these prefixes, separated by commas
    -d, --debug                    output parsed comments for debugging
    -S, --skipSingleStar           set to false to ignore `/* ... */` comments

  Examples:

    # stdin
    $ dox > myfile.json

    # operates over stdio
    $ dox < myfile.js > myfile.json

Programmatic Usage

var dox = require('dox'),
    code = "...";

var obj = dox.parseComments(code);

// [{ tags:[ ... ], description, ... }, { ... }, ...]

Properties

A "comment" is comprised of the following detailed properties:
- tags
- description
- isPrivate
- isEvent
- isConstructor
- line
- ignore
- code
- ctx

Description

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>' }

Tags

Dox also supports JSdoc-style tags. Currently only @api is special-cased, providing the comment.isPrivate boolean so you may omit "private" utilities etc.
/**
 * Output the given `str` to _stdout_
 * or the stream specified by `options`.
 *
 * @param {String} str
 * @param {{stream: Writable}} options
 * @return {Object} exports for chaining
 */

exports.write = function(str, options) {
  options = options || {};
  (options.stream || process.stdout).write(str);
  return this;
};
yields:
tags:
   [ { type: 'param',
       string: '{String} str',
       types: [ 'String' ],
       name: 'str',
       description: '' },
     { type: 'param',
       string: '{{stream: Writable}} options',
       types: [ { stream: ['Writable'] } ],
       name: 'options',
       description: '' },
     { type: 'return',
       string: '{Object} exports for chaining',
       types: [ 'Object' ],
       description: 'exports for chaining' },
     { type: 'api',
       visibility: 'public' } ]

Complex jsdoc tags

dox supports all jsdoc type strings specified in the jsdoc documentation. You can specify complex object types including optional flag =, nullable ?, non-nullable ! and variable arguments ....
Additionally you can use typesDescription which contains formatted HTML for displaying complex types.
/**
 * Generates a person information string based on input.
 *
 * @param {string | {name: string, age: number | date}} name Name or person object
 * @param {{separator: string} =} options An options object
 * @return {string} The constructed information string
 */

exports.generatePersonInfo = function(name, options) {
  var str = '';
  var separator = options && options.separator ? options.separator : ' ';

  if(typeof name === 'object') {
    str = [name.name, '(', name.age, ')'].join(separator);
  } else {
    str = name;
  }
};
yields:
tags:
[
  {
    "tags": [
      {
        "type": "param",
        "string": "{string | {name: string, age: number | date}} name Name or person object",
        "name": "name",
        "description": "Name or person object",
        "types": [
          "string",
          {
            "name": [
              "string"
            ],
            "age": [
              "number",
              "date"
            ]
          }
        ],
        "typesDescription": "<code>string</code>|{ name: <code>string</code>, age: <code>number</code>|<code>date</code> }",
        "optional": false,
        "nullable": false,
        "nonNullable": false,
        "variable": false
      },
      {
        "type": "param",
        "string": "{{separator: string} =} options An options object",
        "name": "options",
        "description": "An options object",
        "types": [
          {
            "separator": [
              "string"
            ]
          }
        ],
        "typesDescription": "{ separator: <code>string</code> }|<code>undefined</code>",
        "optional": true,
        "nullable": false,
        "nonNullable": false,
        "variable": false
      },
      {
        "type": "return",
        "string": "{string} The constructed information string",
        "types": [
          "string"
        ],
        "typesDescription": "<code>string</code>",
        "optional": false,
        "nullable": false,
        "nonNullable": false,
        "variable": false,
        "description": "The constructed information string"
      }
    ]

Code

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;
};

Ctx

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()' } }

Extending Context Matching

Context matching in dox is done by performing pattern matching against the code following a comment block. dox.contextPatternMatchers is an array of all pattern matching functions, which dox will iterate over until one of them returns a result. If none return a result, then the comment block does not receive a ctx value.
This array is exposed to allow for extension of unsupported context patterns by adding more functions. Each function is passed the code following the comment block and (if detected) the parent context if the block.
dox.contextPatternMatchers.push(function (str, parentContext) {
  // return a context object if found
  // return false otherwise
});

Ignore

Comments and their associated bodies of code may be flagged with "!" to be considered worth ignoring, these are typically things like file comments containing copyright etc, however you of course can output them in your templates if you want.
/**
 * Not ignored.
 */
vs
/*!
 * Ignored.
 */
You may use -S, --skipSingleStar or {skipSingleStar: true} to ignore /* ... */ comments.

Running tests

Install dev dependencies and execute make test:
 $ npm install -d
 $ make test

 from https://github.com/tj/dox
--------------------------------




文档伴侣

Doxmate 不再为文档而发愁 Build Status

来源

过去通常要自己维护API文档,这会是一件比较蛋疼的事情。所幸我们有dox,dox可以帮我们解析注解。但是dox不能帮我们任意生成文档。于是就有了doxmate,doxmate基于dox的注解对象,加入模板。在遵循Github和CommonJS的约定后,doxmate可以帮你的模块包快速生成文档。

Installation

安装doxmate为全局工具:
$ npm install doxmate -g

Usage

此处将以doxmate项目自身作为例子:
// 签出doxmate项目
$ git clone git://github.com/JacksonTian/doxmate.git ~/git/doxmate
// 去到项目目录
$ cd doxmate
$ doxmate build
// 在docs目录下将会得到文档
$ open ~/git/doxmate/doc/index.html
// 或者 -o folder,可以将文档生成到指定的目录下
$ doxmate build -o ~/output

选择模版

// 带上-s参数后,可以选择doxmate提供的几种模板
$ doxmate build -s wordpress

自定义模版

如果doxmate提供的几个模板不能满足你的需求
// 查看doxmate目前已有的模板
$ doxmate theme list
// 在当前项目目录导出主题模板
$ doxmate theme export
// 将会在当前目录下生成{doxmate-templates/主题名}的目录
// 带上-s参数后,可以选择doxmate提供的几种模板
$ doxmate theme export -s pomelo
// 通过doxmate build创建文档的时,优先读取该目录
// 导出主题模板后,自行修改,即可实现自定义模板的目的

查看文档效果

通过将生成的文档放到gh-pages分支中,可以通过链接http://jacksontian.github.com/doxmate直接查看效果。
目前提供三种模板

默认风格

defautl 默认风格

wordpress风格

wordpress

pomelo风格

Github与CommonJS规范

  • 每个github项目下应该有一个README.md文件
  • CommonJS规范建议文档存在在doc目录下
  • CommonJS规范建议代码存在在lib目录下
Doxmate将会扫描项目下的README.md和doc目录下的md文件,通过markdown渲染,生成页面。扫描lib目录下的文件,通过dox解析内容,生成API文档页面.

from https://github.com/JacksonTian/doxmate

No comments:

Post a Comment