add the synchronous version of Ox.doc() promised in its documentation

This commit is contained in:
rolux 2012-04-07 16:40:07 +02:00
parent 7474ac9783
commit bd4b5e6880

View file

@ -127,6 +127,72 @@ Ox.doc = (function() {
}).join('\n') }).join('\n')
}; };
} }
function parseSource(source, file) {
var blocks = [],
items = [],
section = '',
tokens = [];
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)
)) {
blocks.push(match[1]);
tokens.push([]);
} else if (tokens.length) {
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
.replace(re.script, encodeLinebreaks)
.replace(re.test, encodeLinebreaks)
.split('\n'),
tree = parseTree(lines);
if (re.item.test(tree.line)) {
// parse the tree's root node
item = parseNode(tree);
item.file = file || '';
if (section) {
item.section = section;
}
if (/^[A-Z]/.test(item.name)) {
// main item
// include leading whitespace
item.source = parseTokens(tokens[i]);
item.line = source.substr(0, item.source[0].offset)
.split('\n').length;
items.push(item);
} else {
// property of a function item
lastItem = items[items.length - 1];
lastItem.properties = lastItem.properties || [];
lastItem.properties.push(item);
// include leading linebreaks and whitespace
lastItem.source = Ox.merge(
lastItem.source, parseTokens(tokens[i], true)
);
}
} else {
section = tree.line.split(' ')[0]
}
});
return items;
}
function parseTest(str) { function parseTest(str) {
// fixme: we cannot properly handle tests where a string contains '\n ' // fixme: we cannot properly handle tests where a string contains '\n '
var lines = decodeLinebreaks(str).split('\n '); var lines = decodeLinebreaks(str).split('\n ');
@ -232,73 +298,20 @@ Ox.doc = (function() {
} }
return ret; return ret;
} }
return function(file, callback) { return function(/*source or file, callback*/) {
Ox.get(file, function(source) { var callback, file, ret, source
var blocks = [], if (arguments.length == 1) {
items = [], source = arguments[0]
section = '', ret = parseSource(source);
tokens = []; } else {
Ox.tokenize(source).forEach(function(token) { file = arguments[0];
var match; callback = arguments[1];
token.source = source.substr(token.offset, token.length); Ox.get(file, function(source) {
if (token.type == 'comment' && (match = callback(parseSource(source, file));
re.multiline.exec(token.source)|| re.singleline.exec(token.source)
)) {
blocks.push(match[1]);
tokens.push([]);
} else if (tokens.length) {
tokens[tokens.length - 1].push(token);
}
}); });
/* }
var blocks = Ox.map(Ox.tokenize(source), function(token) { Ox.print('>>>>>>>', arguments, ret)
// filter out tokens that are not comments return ret;
// 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
.replace(re.script, encodeLinebreaks)
.replace(re.test, encodeLinebreaks)
.split('\n'),
tree = parseTree(lines);
if (re.item.test(tree.line)) {
// parse the tree's root node
item = parseNode(tree);
item.file = file;
if (section) {
item.section = section;
}
if (/^[A-Z]/.test(item.name)) {
// main item
// include leading whitespace
item.source = parseTokens(tokens[i]);
item.line = source.substr(0, item.source[0].offset)
.split('\n').length;
items.push(item);
} else {
// property of a function item
lastItem = items[items.length - 1];
lastItem.properties = lastItem.properties || [];
lastItem.properties.push(item);
// include leading linebreaks and whitespace
lastItem.source = Ox.merge(
lastItem.source, parseTokens(tokens[i], true)
);
}
} else {
section = tree.line.split(' ')[0]
}
});
callback(items);
});
} }
}()); }());