forked from dracupid/nodoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
113 lines (100 loc) · 2.84 KB
/
index.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
path = require 'path'
_ = require 'lodash'
fs = require 'nofs'
nameMap = require '../language/name'
parsers = {}
getLangName = (name)->
if not name
throw new Error 'Language name is required!'
newName = nameMap[name]
if not newName
throw new Error 'Language Not Support: ' + name
else
newName
###*
* Create a new parser or override an old ones
* @param {string|Array} name parser's name/language (and aliases)
* @param {Object} parser parser object, see below
* @prefix parser.
###
setParser = (name, parser)->
name = Array::concat.call [], name
name.forEach (n)->
parsers[n] = parser
getParser = (language)->
language = getLangName language.toLowerCase()
parsers[language] or parsers[language] = require './' + language
###*
* Parse source code directly.
* @param {string} source source code
* @param {string} language specify source language
* @param {Object=} opts option, optional
* @return {Array} parsed comments object **array**
* @prefix parser.
* @example
* ```javascript
* nodoc.parser.parse("This is source code with comments", "coffee").then(function(comments){
* console.log(comments);
* })
* ```
###
parse = (source, language, opts = {})->
_.defaults opts,
rule: null
getParser(language).parse source + ''
_parseFile = (filePath, opts, sync)->
_.defaults opts,
rule: null
language: ''
cwd: process.cwd()
language = opts.language or path.extname(filePath)[1...]
filePath = path.join opts.cwd, filePath
if sync
parse fs.readFileSync(filePath), language, opts
else
fs.readFileP filePath
.then (source)->
parse source, language, opts
###*
* Parse source code from file. Use Promise instead of callback
* @param {string} filePath souce file path
* @param {Object=} opts options
* @return {Promise} resolve parsed comment object **array**
* @prefix parser.
* @example
* ```javascript
* nodoc.parser.parseFile("index.coffee", {cwd: './src'}).then(function(comments){
* console.log(comments);
* });
* ```
###
parseFile = (filePath, opts = {})->
_parseFile filePath, opts, false
###*
* Synchronous version of parseFile
* @prefix parser.
* @return {Object} parsed comment object **array**
###
parseFileSync = (filePath, opts = {})->
_parseFile filePath, opts, true
###*
* Set parser's rule
* @param {string} language parser's name/language
* @param {Object} rule parser's rule object
* @prefix parser.
* @example
* ```javascript
* nodec.parser.setRule('coffee', {
* commentReg: /#?([\s\S]+?)#\s+([\w\.]+)/g
* });
* ```
###
setRule = (language, rule)->
getParser(language).setRule rule
module.exports = {
parse
parseFileSync
parseFile
setParser
setRule
}