make Ox.doc accept multiple files

This commit is contained in:
rolux 2012-04-09 10:39:02 +02:00
parent 4f63e29082
commit 1651b3f565

View file

@ -2,10 +2,7 @@
/*@
Ox.doc <f> Generates documentation for annotated JavaScript
(file, callback) -> <u> undefined
file <s> JavaScript file
callback <f> Callback function
doc <[o]> Array of doc objects
(source) -> <[o]> Array of doc objects
arguments <[o]|u> Arguments (array of doc objects)
Present if the <code>type</code> of the item is
<code>"function"</code>.
@ -31,11 +28,15 @@ Ox.doc <f> Generates documentation for annotated JavaScript
Present if the <code>type</code> of the item is
<code>"function"</code>.
type <s> Type of the item
(source) <a> Array of documentation objects
(file, callback) -> <u> undefined
(files, callback) -> <u> undefined
source <s> JavaScript source code
> Ox.doc("//@ My.FOO <n> Magic constant\nMy.FOO = 23;")
[{"name": "Ox.foo", "summary": "just some string", "type": "string"}]
file <s> JavaScript file
files <[s]> Array of javascript files
callback <f> Callback function
doc <[o]> Array of doc objects
# > Ox.doc("//@ My.FOO <n> Magic constant\nMy.FOO = 23;")
# [{"name": "Ox.foo", "summary": "just some string", "type": "string"}]
@*/
Ox.doc = (function() {
@ -135,8 +136,9 @@ Ox.doc = (function() {
Ox.tokenize(source).forEach(function(token) {
var match;
token.source = source.substr(token.offset, token.length);
if (token.type == 'comment' && (match =
re.multiline.exec(token.source)|| re.singleline.exec(token.source)
if (token.type == 'comment' && (
match = re.multiline.exec(token.source)
|| re.singleline.exec(token.source)
)) {
blocks.push(match[1]);
tokens.push([]);
@ -144,18 +146,6 @@ Ox.doc = (function() {
tokens[tokens.length - 1].push(token);
}
});
/*
var blocks = Ox.map(Ox.tokenize(source), function(token) {
// filter out tokens that are not comments
// or don't match the doc comment pattern
var match;
token.source = source.substr(token.offset, token.length);
return token.type == 'comment' && (match =
re.multiline(token.source) || re.singleline(token.source)
) ? match[1] : null;
}),
items = [];
*/
blocks.forEach(function(block, i) {
var item, lastItem,
lines = block
@ -263,7 +253,7 @@ Ox.doc = (function() {
function parseType(str) {
// returns {types: [""]}
// or {types: [""], default: ""}
// or {types: [""], parent: ""}
// or {types: [""], super: ""}
var isArray,
ret = {types: []},
split,
@ -273,7 +263,7 @@ Ox.doc = (function() {
split = str.split(':');
str = split[0];
if (split.length == 2) {
ret.parent = split[1];
ret.super = split[1];
}
}
str.split('|').forEach(function(str) {
@ -298,19 +288,18 @@ Ox.doc = (function() {
}
return ret;
}
return function(/*source or file, callback*/) {
var callback, file, ret, source
if (arguments.length == 1) {
source = arguments[0]
ret = parseSource(source);
} else {
file = arguments[0];
callback = arguments[1];
return function(/* source | file, callback | files, callback*/) {
var source = arguments.length == 1 ? arguments[0] : void 0,
files = arguments.length == 2 ? Ox.toArray(arguments[0]) : void 0,
callback = arguments[1],
counter = 0, items = [];
files && files.forEach(function(file) {
Ox.get(file, function(source) {
callback(parseSource(source, file));
items = Ox.merge(items, parseSource(source, file));
++counter == files.length && callback(items);
});
}
return ret;
});
return source ? parseSource(source) : void 0;
}
}());
@ -450,6 +439,8 @@ Ox.tokenize <f> Tokenizes JavaScript
or <code>"whitespace"</code>
source <s> JavaScript source code
@*/
// FIXME: constant/method/object/property is of interest
// for syntax highlighting, but may not belong here
Ox.tokenize = (function() {
// see https://github.com/mozilla/narcissus/blob/master/lib/jslex.js