-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocs.js
156 lines (139 loc) · 4.62 KB
/
docs.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var _, assert, declapi, genDocForParamDescription, genDocsForAction, genDocsForActions, genDocsForParam, genDocsForParams, genRestDocs, glueConcat, glueList, glueType, printAction, printParam, printParamlist, printType;
assert = require('assert');
declapi = require('./index.js');
_ = require('lodash');
genDocsForActions = function(actions) {
var a, n;
assert(typeof actions === "object");
return "<div>\n " + (glueConcat((function() {
var results;
results = [];
for (n in actions) {
a = actions[n];
results.push(genDocsForAction(n, a));
}
return results;
})())) + "\n</div>";
};
printAction = function(actionName, action, result) {
return (actionName + "(" + (printParamlist(action)) + ")") + (result != null ? ' -> ' + printParam(result.name, result.info) : void 0);
};
glueList = function(list) {
return _.reduce(list, function(l, r) {
return l + ", " + r;
});
};
glueConcat = function(list) {
return _.reduce(list, function(l, r) {
return "" + l + r;
});
};
glueType = function(list) {
return _.reduce(list, function(l, r) {
return l + "|" + r;
});
};
printParamlist = function(action) {
var name, p;
if (_.keys(action.params).length === 0) {
return '';
}
return "" + (glueList((function() {
var ref, results;
ref = action.params;
results = [];
for (name in ref) {
p = ref[name];
results.push(printParam(name, p));
}
return results;
})()));
};
printParam = function(name, param) {
var str;
str = name + " : " + (printType(param));
if (param.optional) {
str = "[" + str + "]";
}
return str;
};
printType = function(object) {
if (!((object != null) && (object.type != null))) {
return '';
}
if (Array.isArray(object.type)) {
return "" + (glueType(object.type));
} else {
return "" + object.type;
}
};
genRestDocs = function(rest) {
return "<pre class=\"api-action-rest\">\n " + rest.type + " " + rest.url + "\n</pre>";
};
genDocsForAction = function(actionName, action) {
var name, result;
assert(typeof actionName === "string");
assert(typeof action === "object");
assert(action.description != null);
result = ((action.result != null) && _.keys(action.result).length > 0 ? (name = _.keys(action.result)[0], {
name: name,
info: action.result[name]
}) : void 0);
return ("<div class=\"api-action\">\n<h3 class=\"api-action-name\">" + actionName + "</h3>\n<div class=\"api-action-body\">\n <pre class=\"api-action-signature\">" + (printAction(actionName, action, result)) + "</pre>\n <p class=\"api-action-description\">" + action.description + "</p>\n " + (_.keys(action.params).length !== 0 ? '<h4>Parameters</h3>' : '') + "\n " + (genDocsForParams(action.params))) + (result != null ? "<h4>Result</h3>\n <ul>\n " + (genDocsForParam(result.name, result.info)) + "\n </ul>" : '') + (action.rest != null ? "<h4>HTTP-Request</h4>\n" + (genRestDocs(action.rest)) : '') + "</div>\n</div>";
};
genDocsForParams = function(params) {
var n, p;
if (_.keys(params).length === 0) {
return '';
}
return "<ul>\n " + (glueConcat((function() {
var results;
results = [];
for (n in params) {
p = params[n];
results.push(genDocsForParam(n, p));
}
return results;
})())) + "\n</ul>";
};
genDocsForParam = function(paramName, param) {
var info, n, p;
info = [];
if (param.type != null) {
info.push(printType(param));
}
if (param.optional) {
info.push('optional');
}
return "<li class=\"api-param\">\n <code class=\"api-param-name\">" + paramName + "</code>\n <span class=\"api-param-description\">\n " + (info.length > 0 ? '(' + glueList(info) + ')' : '') + "\n " + (genDocForParamDescription(param)) + "\n </span>\n " + (param.properties != null ? '<ul class="action-param-properties">' + glueConcat((function() {
var ref, results;
ref = param.properties;
results = [];
for (n in ref) {
p = ref[n];
results.push(genDocsForParam(n, p));
}
return results;
})()) + '</ul>' : '') + "\n " + (param.items != null ? '<ul class="action-param-items">' + glueConcat((function() {
var ref, results;
ref = param.items;
results = [];
for (n in ref) {
p = ref[n];
results.push(genDocsForParam(n, p));
}
return results;
})()) + '</ul>' : '') + "\n</li>";
};
genDocForParamDescription = function(param) {
if (param.description == null) {
return '';
}
return "<span class=\"api-param-description\">: " + param.description;
};
module.exports = {
genDocsForActions: genDocsForActions,
genDocsForAction: genDocsForAction,
genDocsForParams: genDocsForParams,
genDocsForParam: genDocsForParam
};