-
Notifications
You must be signed in to change notification settings - Fork 2
/
docs.coffee
130 lines (115 loc) · 3.25 KB
/
docs.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
125
126
127
128
129
130
assert = require 'assert'
declapi = require './index.js'
_ = require 'lodash'
genDocsForActions = (actions) ->
assert typeof actions is "object"
"""
<div>
#{glueConcat((genDocsForAction(n, a) for n, a of actions))}
</div>
"""
printAction = (actionName, action, result) ->
"""#{actionName}(#{printParamlist(action)})""" + (
if result? then ' -> ' + printParam(result.name, result.info)
else
)
glueList = (list) -> _.reduce(list, (l, r) -> "#{l}, #{r}")
glueConcat = (list) -> _.reduce(list, (l, r) -> "#{l}#{r}")
glueType = (list) -> _.reduce(list, (l, r) -> "#{l}|#{r}")
printParamlist = (action) ->
if _.keys(action.params).length is 0 then return ''
"""
#{glueList((printParam(name, p) for name, p of action.params))}
"""
printParam = (name, param) ->
str = "#{name} : #{printType(param)}"
if param.optional then str = "[#{str}]"
return str
printType = (object) ->
unless object? and object.type? then return ''
if Array.isArray object.type
"""#{glueType(object.type)}"""
else
"""#{object.type}"""
genRestDocs = (rest) ->
"""
<pre class="api-action-rest">
#{rest.type} #{rest.url}
</pre>
"""
genDocsForAction = (actionName, action) ->
assert typeof actionName is "string"
assert typeof action is "object"
assert action.description?
result = (
if action.result? and _.keys(action.result).length > 0
name = _.keys(action.result)[0]
{name, info: action.result[name]}
)
"""
<div class="api-action">
<h3 class="api-action-name">#{actionName}</h3>
<div class="api-action-body">
<pre class="api-action-signature">#{printAction(actionName, action, result)}</pre>
<p class="api-action-description">#{action.description}</p>
#{unless _.keys(action.params).length is 0 then '<h4>Parameters</h3>' else ''}
#{genDocsForParams(action.params)}
""" + (
if result?
"""
<h4>Result</h3>
<ul>
#{genDocsForParam(result.name, result.info)}
</ul>
"""
else ''
) + (
if action.rest?
"""
<h4>HTTP-Request</h4>
#{genRestDocs(action.rest)}
"""
else ''
)+ """
</div>
</div>
"""
genDocsForParams = (params) ->
if _.keys(params).length is 0 then return ''
"""
<ul>
#{glueConcat((genDocsForParam(n, p) for n, p of params))}
</ul>
"""
genDocsForParam = (paramName, param) ->
info = []
info.push(printType param) if param.type?
info.push('optional') if param.optional
"""
<li class="api-param">
<code class="api-param-name">#{paramName}</code>
<span class="api-param-description">
#{if info.length > 0 then '('+glueList(info)+')' else ''}
#{genDocForParamDescription(param)}
</span>
#{if param.properties?
'<ul class="action-param-properties">' +
glueConcat((genDocsForParam(n, p) for n, p of param.properties)) +
'</ul>'
else ''}
#{if param.items?
'<ul class="action-param-items">' +
glueConcat((genDocsForParam(n, p) for n, p of param.items)) +
'</ul>'
else ''}
</li>
"""
genDocForParamDescription = (param) ->
unless param.description? then return ''
"""<span class="api-param-description">: #{param.description}"""
module.exports = {
genDocsForActions
genDocsForAction
genDocsForParams
genDocsForParam
}