forked from dracupid/nodoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
104 lines (89 loc) · 2.74 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
parser = require './parser'
_ = require 'lodash'
fs = require 'nofs'
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)->
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)->
aliasTag = getTag(comment, 'alias')
if aliasTag.length
alias = aliasTag.reduce (str, a)->
str += a.description + ' '
, ''
if alias then comment.name += " (alias: #{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 source code file
* @param {Object=} opts Options, optional
* ```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 formatted 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 typeof opts.moduleName isnt 'undefined' then return opts.moduleName
baseName = path.basename srcPath, path.extname(srcPath)
dirName = path.dirname(srcPath).split(path.sep).slice(-1)[0]
return if baseName is 'index' then dirName else baseName
_.assign 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
}