-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bc6517f
Showing
4 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
# Changes # | |||
|
|||
## 0.0.1 / 2011-09-02 ## | |||
|
|||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,88 @@ | |||
# doc # | |||
|
|||
Runtime documentation tool for REPL. | |||
|
|||
## Usage ## | |||
|
|||
Have you ever wished you could see docs for the given function right out of | |||
the REPL? If so, this tool is for you! | |||
|
|||
```js | |||
var doc = require('doc').doc | |||
doc(doc) // Prints following output: | |||
|
|||
/* | |||
function doc(source) { ... } | |||
----------------------------------------------- | |||
Prints documentanion of the given function | |||
*/ | |||
|
|||
// You can also document your own functions: | |||
|
|||
function compose() { | |||
doc: "Returns the composition of a list of functions, where each function" | |||
| "consumes the return value of the function that follows. In math" | |||
| "terms, composing the functions `f()`, `g()`, and `h()` produces" | |||
| "`f(g(h()))`." | |||
| "Usage:" | |||
| "var greet = function(name) { return 'hi: ' + name }" | |||
| "var exclaim = function(statement) { return statement + '!' }" | |||
| "var welcome = compose(exclaim, greet)" | |||
| "welcome('moe')" | |||
| "//> 'hi: moe!'" | |||
|
|||
var funcs = Array.prototype.slice.call(arguments) | |||
return function composed() { | |||
var args = slice.call(arguments) | |||
var i = funcs.length | |||
while (0 <= --i) args = [ funcs[i].apply(this, args) ] | |||
return args[0] | |||
} | |||
} | |||
|
|||
doc(compose) // Prints following output: | |||
|
|||
/* | |||
function compose() { ... } | |||
----------------------------------------------- | |||
Returns the composition of a list of functions, where each function | |||
consumes the return value of the function that follows. In math | |||
terms, composing the functions `f()`, `g()`, and `h()` produces | |||
`f(g(h()))`. | |||
Usage: | |||
var greet = function(name) { return 'hi: ' + name } | |||
var exclaim = function(statement) { return statement + '!' } | |||
var welcome = compose(exclaim, greet) | |||
welcome('moe') | |||
//> 'hi: moe!' | |||
*/ | |||
|
|||
// Alternative way to documenting functions (Not cross platform though): | |||
|
|||
function sum(a, b) { | |||
/** | |||
Takes arbitary number of arguments and returns their sum. | |||
Usage: | |||
sum(7, 2, 8) //> 17 | |||
**/ | |||
|
|||
var count = arguments.length, index = 0, value = 0 | |||
while (index < count) value += arguments[index++] | |||
return value | |||
} | |||
|
|||
doc(sum) // Prints following output: | |||
|
|||
/* | |||
function sum(a, b) { ... } | |||
----------------------------------------------- | |||
Takes arbitary number of arguments and returns their sum. | |||
Usage: | |||
sum(7, 2, 8) //> 17 | |||
*/ | |||
|
|||
``` | |||
|
|||
## Install ## | |||
|
|||
npm install doc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,66 @@ | |||
/* vim:set ts=2 sw=2 sts=2 expandtab */ | |||
/*jshint asi: true undef: true es5: true node: true devel: true | |||
forin: true latedef: false supernew: true */ | |||
/*global define: true */ | |||
|
|||
!(typeof(define) !== "function" ? function($) { $(typeof(require) !== 'function' ? (function() { throw Error('require unsupported'); }) : require, typeof(exports) === 'undefined' ? this : exports, typeof(module) === 'undefined' ? {} : module); } : define)(function(require, exports, module) { | |||
|
|||
"use strict"; | |||
|
|||
function extractDocs(source, value) { | |||
doc: "Extracts documentation from the give `source` function" | |||
|
|||
var docs | |||
value = value || '' | |||
|
|||
if (!source) return value | |||
if (typeof(source.doc) === 'function') return extractDocs(source.doc) | |||
if (typeof(source.doc) === 'string') return source.doc | |||
|
|||
if ((docs = /doc\:\s*([\s\S]*?)(\n(?!(\s*\|)))/.exec(String(source)))) { | |||
value = !docs[1] ? value : docs[1].split(/\n\s*\||\|/g).map(function($) { | |||
return $.replace(/^\s*['|"]([\s\S]*?)['|"]\s*;*$/, "$1") || $ | |||
}).join('\n') | |||
} else if ((docs = /\{\s*\/\*\*\n*([\s\S]*)?\*\*\//.exec(String(source)))) { | |||
var indent = /^\s*/.exec(docs[1])[0].length | |||
value = docs[1].split('\n').map(function($) { | |||
return $.substr(indent) | |||
}).join('\n') | |||
} | |||
|
|||
return value | |||
} | |||
|
|||
function extractArgs(source) { | |||
doc: "Extracts documentanion from the given `source` function" | |||
|
|||
var declaration, names, arity = source.length, index = 0 | |||
|
|||
declaration = /\(([\s\S]*?)\)/.exec(source)[1].trim() | |||
names = declaration ? declaration.split(/\s*,\s*/) : [] | |||
|
|||
while (++index <= arity) names[index - 1] = names[index - 1] || '$' + index | |||
|
|||
return names | |||
} | |||
|
|||
function doc(source) { | |||
doc: "Prints documentanion of the given function" | |||
|
|||
var docs = extractDocs(source) | |||
var args = extractArgs(source) | |||
var name = source.displayName || source.name | |||
|
|||
console.log([ | |||
'\n', | |||
'function ', | |||
name, | |||
'(' + args.join(', ') + ')', | |||
' { ... }', | |||
docs ? '\n-----------------------------------------------\n' + docs : docs, | |||
'\n' | |||
].join('')) | |||
} | |||
exports.doc = doc | |||
|
|||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,35 @@ | |||
{ | |||
"name": "doc", | |||
"id": "doc", | |||
"version": "0.0.1", | |||
"description": "Runtime documentation tool for REPL", | |||
"keywords": [ ], | |||
"author": "Irakli Gozalishvili <rfobic@gmail.com> (http://jeditoolkit.com)", | |||
"homepage": "https://github.com/Gozala/doc", | |||
"repository": { | |||
"type": "git", | |||
"url": "https://github.com/Gozala/doc.git", | |||
"web": "https://github.com/Gozala/doc" | |||
}, | |||
"bugs": { | |||
"web": "http://github.com/Gozala/doc/issues/" | |||
}, | |||
"devDependencies": { | |||
"test": ">=0.0.10" | |||
}, | |||
"dependencies": { | |||
"graphquire": ">=0.6.1" | |||
}, | |||
"main": "./index.js", | |||
"engines": { | |||
"node": "0.4.x" | |||
}, | |||
"scripts": { | |||
"test": "node tests/test-all.js", | |||
"install": "graphquire --clean --write" | |||
}, | |||
"licenses": [{ | |||
"type" : "MIT", | |||
"url" : "http://jeditoolkit.com/LICENSE" | |||
}] | |||
} |