Created
May 30, 2014 05:56
-
-
Save ekoneko/2e6c5298013974fcdab5 to your computer and use it in GitHub Desktop.
解析文档注释
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
/** | |
* 从代码中匹配出文档注释 | |
*/ | |
(function () { | |
'use strict'; | |
/** | |
* 读取文件返回数据 | |
* | |
* @ param path | |
* @ param callback | |
*/ | |
this.exec = function (path, callback) { | |
var fs = require('fs'), | |
commentDocument = [], | |
FSM = { | |
enterComment: function (data, lineIndex) { | |
var line = data[lineIndex].trim(), | |
text, | |
comment = {}; | |
if (data.length === lineIndex + 2) { | |
return FSM.parseEnd(); | |
} | |
if (line[0] === '*') { | |
text = /\*\s*(.+)$/.exec(line); | |
if (!text|| text.length < 2) { | |
return FSM.enterComment(data, lineIndex + 1); | |
} | |
text = text[1]; | |
if (text[0] === '@') { | |
return FSM.inComment(data, lineIndex, comment); | |
} else { | |
comment.title = text; | |
return FSM.inComment(data, lineIndex + 1, comment); | |
} | |
} | |
return FSM.outComment(data, lineIndex + 1); | |
}, | |
inComment: function (data, lineIndex, comment) { | |
var line = data[lineIndex].trim(), | |
text; | |
if (data.length === lineIndex + 2) { | |
commentDocument.push(comment); | |
return FSM.parseEnd(); | |
} | |
if (line.substr(-2) === '*/') { | |
return FSM.leaveComment(data, lineIndex + 1, comment); | |
} | |
text = /\*\s*(@(\w+)\s*(.*))/.exec(line); | |
if (!text|| text.length < 2) { | |
return FSM.inComment(data, lineIndex + 1, comment); | |
} | |
comment[text[2]] = text[3]; | |
return FSM.inComment(data, lineIndex + 1, comment); | |
}, | |
outComment: function (data, lineIndex) { | |
var line = data[lineIndex].trim(); | |
if (data.length === lineIndex + 2) { | |
return FSM.parseEnd(); | |
} | |
if (line.substr(0, 3) === '/**') { | |
return FSM.enterComment(data, lineIndex + 1); | |
} | |
return FSM.outComment(data, lineIndex + 1); | |
}, | |
leaveComment: function (data, lineIndex, comment) { | |
var line = data[lineIndex].trim(); | |
if (data.length === lineIndex + 2) { | |
commentDocument.push(comment); | |
return FSM.parseEnd(); | |
} | |
if (line.length === 0) { | |
return FSM.leaveComment(data, lineIndex + 1, comment); | |
} | |
line = line.replace(/[\{\}]/g, ''); | |
if (line.indexOf('class') !== -1 || | |
line.indexOf('function') !== -1 || | |
commentDocument.length > 0) { | |
comment.object = line; | |
} | |
commentDocument.push(comment); | |
return FSM.outComment(data, lineIndex + 1); | |
}, | |
parseEnd: function () { | |
callback(null, commentDocument, path); | |
} | |
}; | |
fs.readFile(path, function (err, data) { | |
if (err) { | |
return callback(err); | |
} | |
data = data.toString().split(/[\r\n]/); | |
data = data.filter(function (w) { | |
return !!w; | |
}); | |
FSM.outComment(data, 0); | |
}); | |
}; | |
}.call(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment