-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmake-docs.js
105 lines (82 loc) · 3.26 KB
/
make-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
/* global require */
var builtins = require('./church_builtins.js');
var annotations = builtins.__annotations__;
annotations.eval = {
name: 'eval',
desc: 'Evaluate a list representing a Church s-expression, e.g., <code>(eval (list + 1 2))</code> returns 3',
params: [{name: 'lst'}]
}
var _ = require('underscore');
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
/*
functionName
description
arg_1 - type_1 - desc_1
arg_2 - type_2 - desc_2
...
arg_3 - type_3 - desc_3
*/
// TODO: transform names like 'x ...' into 'x1 x2 ...'
function renderFunction(functionName, props) {
var description = props.desc || "";
var args = props.params || [];
var renderArg = _.template("<tr><td><code>{{name}}</code><td>{{type}}</td><td>{{desc}}</td></tr>");
var argsTable = _.template("<table>\n{{tbody}}\n</table>",
{tbody: args.map(function(arg) {
arg = _.defaults(arg, {type: '', desc: ''});
return renderArg(arg);
}).join('\n')});
var templateString = ['<div class="function">',
'<code class="function-name">({{functionName}} {{argList}})</code>',
'<code class="aliases">{{aliases}}</code>',
'<div class="description">{{description}}</div>',
'{{table}}',
'</div>'].join('\n');
// compute canonical scheme name from js name
functionName = functionName
.replace(/wrapped_(.+)/, function(m, p1) { return p1 })
.replace(/is_(.+)/, function(m, p1) { return p1 + "?"})
.replace('_to_', '->')
.replace(/_/g, '-');
// must be an array
var aliases = props.alias;
// strip out the canonical scheme name from the list of aliases
aliases = _(aliases).without(functionName)
if (aliases.length > 0) {
aliases = Array.isArray(aliases) ? aliases.join(",") : aliases;
aliases = "<br /> Aliases: " + aliases + "";
} else {
aliases = "";
}
var tableString = _.template(templateString,
{functionName: functionName,
aliases: aliases,
argList: _(args).pluck('name').join(' '),
description: description,
table: argsTable});
return {name: functionName,
string: tableString};
}
function renderAllFunctions() {
var rendered = _(annotations).map(function(props, functionName) {
return renderFunction(functionName, props);
});
var entries = rendered.sort(function(x, y) {
var xn = x.name.toLowerCase();
var yn = y.name.toLowerCase();
if (xn < yn) {
return -1;
}
if (xn > yn) {
return 1;
}
return 0
});
return _(entries).pluck('string').join('\n');
}
var res = renderAllFunctions();
console.log('<link rel="stylesheet" type="text/css" href="css/ref.css" />')
console.log('<p><h2>Work in progress: email [email protected] with corrections (or send a pull request on Github)</h2></p><p></p>')
console.log(res);