Giter Club home page Giter Club logo

dox's Introduction

Dox

Tests

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": "param",
        "string": "{String} html",
        "name": "html",
        "description": "",
        "types": [
          "String"
        ],
        "typesDescription": "<code>String</code>",
        "optional": false,
        "nullable": false,
        "nonNullable": false,
        "variable": false,
        "html": "<p>{String} html</p>"
      },
      {
        "type": "return",
        "string": "{String}",
        "types": [
          "String"
        ],
        "typesDescription": "<code>String</code>",
        "optional": false,
        "nullable": false,
        "nonNullable": false,
        "variable": false,
        "description": "",
        "html": "<p>{String}</p>"
      },
      {
        "type": "api",
        "string": "private",
        "visibility": "private",
        "html": "<p>private</p>"
      }
    ],
    "description": {
      "full": "<p>Escape the given <code>html</code>.</p>",
      "summary": "<p>Escape the given <code>html</code>.</p>",
      "body": ""
    },
    "isPrivate": true,
    "isConstructor": false,
    "isClass": false,
    "isEvent": false,
    "ignore": false,
    "line": 2,
    "codeStart": 10,
    "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.

write1.js:

/**
 * 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": ""
    },
    // ... other tags
  }
]

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.

write2.js:

/**
 * 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 />\nor the stream specified by <code>options</code>.</p>\n<p>Options:</p>\n<ul>\n<li><code>stream</code> defaulting to <em>stdout</em></li>\n</ul>\n<p>Examples:</p>\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 />\nor the stream specified by <code>options</code>.</p>",
      "body": "<p>Options:</p>\n<ul>\n<li><code>stream</code> defaulting to <em>stdout</em></li>\n</ul>\n<p>Examples:</p>\n<pre><code>mymodule.write('foo')\nmymodule.write('foo', { stream: process.stderr })\n</code></pre>"
    },
    // ... other tags
  }
]

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.

write_tags.js:

/**
 * 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",
        "name": "str",
        "description": "",
        "types": [
          "String"
        ],
        "typesDescription": "<code>String</code>",
        "optional": false,
        "nullable": false,
        "nonNullable": false,
        "variable": false,
        "html": "<p>{String} str</p>"
      },
      {
        "type": "param",
        "string": "{{stream: Writable}} options",
        "name": "options",
        "description": "",
        "types": [
          {
            "stream": [
              "Writable"
            ]
          }
        ],
        "typesDescription": "{stream: <code>Writable</code>}",
        "optional": false,
        "nullable": false,
        "nonNullable": false,
        "variable": false,
        "html": "<p>{{stream: Writable}} options</p>"
      },
      {
        "type": "return",
        "string": "{Object} exports for chaining",
        "types": [
          "Object"
        ],
        "typesDescription": "<code>Object</code>",
        "optional": false,
        "nullable": false,
        "nonNullable": false,
        "variable": false,
        "description": "<p>exports for chaining</p>"
      }
    ],
    // ... other tags
  }
]

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.

generatePersonInfo.js:

/**
 * 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": [
      {
        "type": "param",
        "string": "{string | {name: string, age: number | date}} name Name or person object",
        "name": "name",
        "description": "<p>Name or person object</p>",
        "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": "<p>An options object</p>",
        "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": "<p>The constructed information string</p>"
      }
    ],
    // ... other tags
  }
]

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:

ctx.js:

/** */
exports.generate = function(str, options) {};

yields:

[
  {
    // ... other tags
    "ctx": {
      "type": "method",
      "receiver": "exports",
      "name": "generate",
      "string": "exports.generate()"
    }
  }
]
/** */
var foo = 'bar';

yields:

[
  {
    // ... other tags
    "ctx": {
      "type": "declaration",
      "name": "foo",
      "value": "'bar'",
      "string": "foo"
    }
  }
]
/** */
function User() {}

yields:

[
  {
    // ... other tags
    "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

License

(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.

dox's People

Contributors

a avatar ammojamo avatar evindor avatar fgribreau avatar forbeslindesay avatar gionkunz avatar gozala avatar hasezoey avatar heff avatar hughsk avatar iolo avatar jacksontian avatar jasonlaster avatar jfirebaugh avatar jugglinmike avatar logicalparadox avatar mekwall avatar neogeek avatar nopnop avatar olivernn avatar parasyte avatar pdehaan avatar shellscape avatar timgates42 avatar timoxley avatar tj avatar tootallnate avatar trusktr avatar twipped avatar vladtsf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dox's Issues

Makefile uses missing --verbose, --out, --title, --github, --index options

The Makefile rule for docs specifies --verbose, --out, --title, --github, --index command-line options. None of these seem to be implemented any more. Maybe that's why npm install dox followed by make docs in node_modules/dox just hangs.

I guess this is leftover from the "no longer generates an opinionated structure or style for your docs" change. Alas the web still links and describes the "dox" that no longer exists, e.g. http://dailyjs.com/2011/01/20/framework-part-47/

CoffeeScript (and other lang) support

I have a 'working' coffee-script version over here: https://github.com/cpsubrian/coffee-dox. All tests are passing.

I still need to add support for some coffeescript specific stuff (like classes).

If there is demand (and it has a chance of merging) I'd be happy to work up ideas for a language agnostic version of Dox. Could create the js parser and coffeescript parser.

I have not been following this issue queue so I'm not sure what other improvements and bug fixes are not in master yet.

Windows PowerShell support?

Hi!

Is it possible to use dox with Windows PowerShell?

The problem is that PowerShell does not implement < operator. Example:

C:\work\myproject\> dox < app.js
The '<' operator is reserved for future use.
At line:1 char:6
+ dox < <<<<  app.js
    + CategoryInfo          : ParserError: (<:OperatorToken) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RedirectionNotSupported

Any ideas?

As a workaround, I am currently using command-prompt to run dox and - for everything else - PowerShell.

remove parenthesis ( and ) in some file names

in dox / lib / dox / markdown / test / fixtures / PHP_Markdown-from-MDTest1.1.mdtest directory, these file names are causing some issues with a tool that we don't really have control of. Is it possible to replace these parenthesis?

    Inline_HTML_(Simple).html   
Inline_HTML_(Simple).json
Inline_HTML_(Simple).text
Inline_HTML_(Span).json
Inline_HTML_(Span).text
Inline_HTML_(Span).xhtml

Error using v0.1.0

/usr/local/lib/node_modules/dox/lib/dox.js:70
comment.code = code;
^
TypeError: Cannot set property 'code' of undefined
at Object.parseComments (/usr/local/lib/node_modules/dox/lib/dox.js:70:18)
at [object Object]. (/usr/local/lib/node_modules/dox/bin/dox:40:17)
at [object Object].emit (events.js:61:17)
at afterRead (fs.js:878:12)
at wrapper (fs.js:245:17)

For every file I try to process.

not possible to process folder

It is not possbible to recursively process a folder of files, or is it? What am I missing here? Hardly any documentation to be found on the conf.json file either...what gives?

CTX improperly parsed

In the following code
Spo.prototype.car = {};
Is parsed to have the ctx string
Spo.prototypcar

Which later down the line I believe is causing the failure of Spo.prototype.car.dance to parse properly

/*

  • The spo object
    */

var Spo = {};

/*

  • get on the bus
    */

Spo.prototype.car = {};

/**
*The dance party function.

*example:

  •  dance('spo');
    
  •  will give you the out put
    
  • @param {String} Spo what will you do ?
    */

Spo.prototype.car.dance = function(spo){
console.log(spo);
};

I will check it out more but I don`t think I am smart enough yet to fix your code Xp

Inclusion via nDistro is missing proper symlink

If you add "module visionmedia dox" to nDistro and run it, you'll end up with:

$ pwd
lib/node
$ index.js -> ../../modules/dox/lib/index.js

Running "$ ./bin/dox" will give you a module not found error. To correct this, you have to manually move "index.js -> dox.js". I assume the correct fix would be to modify the dox repo such that "visionmedia/dox/lib/index.js" is symlinked to "visionmedia/dox/lib/dox.js".

If you'd like me to fork & correct this, let me know :) I think I've forked enough of your work for the day :P

Have a good night!

NEEDS example of "opinionated formatting template"...

See the discussion on this StackOverflow posting:
http://stackoverflow.com/questions/6096649/documenting-node-js-projects/6995688#6995688

Basically, mere mortals are intimidated by the task of creating a template to turn pure "unopinionated" JSON code structure metadata into human consumable "opinionated" HTML. The concept of factoring apart the parser from the formatter is great; we just need an example of how a template would work.... a starting point. Adding this (plus a mention in the README.md) would go a long way towards making this module consumable by others.

As much as I'd love to do the work myself and submit a "pull request", honestly, I'm not entirely sure how to create such a template either. Not that I'd ever admit that... ;)

Cheers,
--- Dave

Tests failing .. markdown not converting <> ?

For fun and education, I'm working on a port to CoffeeScript that reads .coffee files instead of .js files.

https://github.com/cpsubrian/coffee-dox

When working on it I could not get the first tests (the ones using the 'a.js' fixture) to pass.

Markdown is not converting <> to entities in "". It looks like Dox is suffering from the same bug.

Is this is regression in github-flavored-markdown?

Ignore jshint directives

directives for jshint should be ignored:

  /*jshint latedef:false */
  var define = require('amdefine')(module);

results in

{
    "tags": [],
    "description": {
      "full": "<p>shint latedef:false</p>",
      "summary": "<p>shint latedef:false</p>",
      "body": ""
    },
    "ignore": false,
    "code": "var define = require('amdefine')(module);\n}\n\n\ndefine(function (require) {\n\t'use strict';\n\tvar _ = require('underscore');\n\t\n\tvar type = function(typename,
 required, options) {\n\t\t\tvar t = {type: typename};\n\t\t\tif (required) {\n\t\t\t\tt.required = true;\n\t\t\t}\n\t\t\treturn _.extend(t, options);\n\t};\n\t\n\tvar combine = 
function(",
    "ctx": {
      "type": "declaration",
      "name": "define",
      "value": "require('amdefine')(module)",
      "string": "define"
    }
  }

Those directives should really be treated as regular code. JSHint does not allow a space between the comment start and jshint or global:

ok: /*jshint latedef:false */, /*global console:true */
not recognized by jshint: /* jshint onevar:false */

Indented blocks of code aren't uniformly re-indented

Suppose I have the following JS file:

/** Wrap everything in a function for some reason */
(function() {
  var state = 0;

  /** Increment state by 1 */
  function inc() {
    state++;
    return state;
  };

  /** Publically expose `inc` */
  return inc;
})();

The documentation generated for inc is improperly indented. The first line isn't indented at all, but the rest is indented at the same level as the file, so it ends up looking like this:

function inc() {
    state++;
    return state;
  };

It would be preferable to either not remove the indentation on the first line, or to re-indent the entire thing.

colons make the line h2

I see this pull request so I think it's a feature not a bug :p

But when I parse this comment:

/**
 * Parse the given comment `str`.
 *
 * The comment object returned contains the following:
 * ...
 */

Then description.body is <h2>The comment object returned contains the following</h2>\n\n<p>...</p>
And without the colon after following it's <p>The comment object returned contains the following<br />...</p>

I think it would be better if it always stays with <p> and if you need <h2> then you can write:

/**
 * Parse the given comment `str`.
 *
 * ## The comment object returned contains the following:
 * ...
 */

This will always returns <h2>The comment object returned contains the following</h2>\n\n<p>...</p> with or without colons.

Otherwise if it should stay as it currently is, then there is a small bug because this comment

/**
 * Parse the given comment `str`.
 *
 *  The comment object returned contains the following
 * ...
 */

will be parsed in <p>The comment object returned contains the following:<br />...</p> even with colon (Notice the two spaces before The comment object...).

help option error

dox -h

  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


node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: process.stdout cannot be closed.
    at WriteStream.<anonymous> (node.js:284:20)
    at Command.<anonymous> (/usr/local/lib/node_modules/dox/node_modules/commander/lib/commander.js:908:18)
    at Command.emit (events.js:88:20)
    at Command.parseOptions (/usr/local/lib/node_modules/dox/node_modules/commander/lib/commander.js:439:14)
    at Command.parse (/usr/local/lib/node_modules/dox/node_modules/commander/lib/commander.js:323:20)
    at Object.<anonymous> (/usr/local/lib/node_modules/dox/bin/dox:32:9)
    at Module._compile (module.js:444:26)
    at Object..js (module.js:462:10)
    at Module.load (module.js:351:31)
    at Function._load (module.js:310:12)
node v0.6.9
dox v0.1.3

Link to documentation

Is there a link to a complete documentation for how to document code compatible with dox?

I miss specially how to document functions that takes a variable number of arguments, and arguments that accept any types. I saw JSDoc uses {...Type} for the former and {*} for the latter, maybe it's something like {...*} for an argument with variable number of arguments of all types.

How would you document a function that takes a string as the first argument, and a callback as the last, and any number of arguments of any type in between?

/**
 * Description.
 *
 * @param {String} key
 * @param {...*} argument
 * @param {Function} callback
 * @api private
 */

Also, how to document an argument that can be of e.g. Object or String types and be omitted or set to null? In JSDoc I see something like {?Object|String} or @param {Object|String} [arg].

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.