-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.coffee
124 lines (106 loc) · 3.7 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
114
115
116
117
118
119
120
121
122
123
124
parser = require './parser'
_ = require 'underscore'
fs = require 'fs'
path = require 'path'
defaultTemplate = '' + fs.readFileSync path.join __dirname, 'template/markdown.tpl'
removeTag = (comment, tagName) ->
comment.tags = comment.tags.filter (tag) ->
tag.tagName isnt tagName
comment
getTag = (comment, tagName) ->
tag = comment.tags.filter (tag) ->
tag.tagName is tagName
tag
hasTag = (comment, tagName) ->
not not getTag(comment, tagName).length
###*
* Remove tags not to be shown
* @param {Array} comments comments array
* @return {Array} filtered array
* @private
###
commentFilter = (comments) ->
cos = comments.filter (comment) ->
not (hasTag(comment, 'private') or hasTag(comment, 'nodoc'))
cos.forEach (comment) ->
if hasTag comment, 'noPrefix'
comment.name = comment.name.split('.').slice(-1)[0]
removeTag comment, 'noPrefix'
defStr = ''
getTag(comment, 'param').forEach (param) ->
return defStr += ' ' if not param.type and not param.name
[type, defaultVal] = param.type.split '='
defStr +=
if defaultVal
"#{param.name} = #{defaultVal}, "
else
"#{param.name}, "
if defStr
comment.sign = "(#{defStr.slice(0, -2)})"
if defStr.trim() is '' then removeTag comment, 'param'
else if hasTag comment, 'return'
comment.sign = '()'
aliasTag = getTag comment, 'alias'
if aliasTag.length
alias = aliasTag.reduce (str, a) ->
str += a.description + ' '
, ''
comment.alias = alias.trim() if alias
removeTag comment, 'alias'
prefixTag = getTag(comment, 'prefix')[0]
if prefixTag
comment.name = (prefixTag.description or '') + comment.name
removeTag comment, 'prefix'
cos
###*
* Generate formatted markdown API document from source code
* @alias render
* @param {string} srcPath Path of the source code file
* @param {Object=} opts Options
* ```javascript
* {
* moduleName: '', // module name of the file, or it will be auto set to file name, of parent directory name for `index` file.
* moduleDesc: '', // module decription
* template: '', // custom template
* tplData: {}, // addition template data
* cwd: process.cwd() // current working directory
* language: '' // specify the language, or it will be auto recognized by extname
* rule: {} // specific parser rule, items vary from parsers
* }
* ```
* @return {Promise} Resolve markdown
* @example
* ```javascript
* nodoc.generate('./src/index.coffee').then(function(md){
* console.log(md);
* });
* ```
###
generate = (srcPath, opts = {}) ->
_.defaults opts,
moduleName: undefined
moduleDesc: ''
tplData: {}
template: defaultTemplate
parser.parseFile srcPath, opts
.then (comments) ->
moduleName = do ->
if opts.moduleName? then return opts.moduleName
baseName = path.basename srcPath, path.extname(srcPath)
dirName = path.dirname(srcPath).split(path.sep).slice(-1)[0]
if baseName is 'index' then dirName else baseName
_.extend opts.tplData, {
moduleDesc: opts.moduleDesc
moduleName
comments: commentFilter comments
srcPath
}
_.template(opts.template + '')(opts.tplData).replace(/(\r\n|\n)(\ |\r\n|\n)*(\r\n|\n)/g, '\n\n')
module.exports = {
###*
* Parser module, see below for details.
###
parser
generate
render: generate
}