-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateHTML.js
154 lines (118 loc) · 3.55 KB
/
generateHTML.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var __ = require('underscore');
var fs = require('fs');
var m = require("mustache");
var c = require('./contract.face');
var s = require('github-flavored-markdown');
var moduleTemplateData = {
name: 'contract',
doc: 'the contract library is great.',
categories: [
{ hasHeader: true,
name: 'basic',
doc: 'the basic stuff',
hasTypes: true,
hasValues: true,
types: [
{ name: 'contractObject', doc: 'contract are represented with object.', type: 'c.object' },
{ name: 'objectContractObject', doc: 'object contract have extra values.', type: 'c.object' }
],
values: [
{ name: 'c.string', type: 'contractObject', doc: 'the string type' },
{ name: 'c.any', type: 'contractObject', doc: 'accepts any value' }
]},
{ hasHeader: true,
name: 'wrappers',
doc: 'the stuff that needs wrapping',
hasTypes: false,
hasValues: true,
values: [
{ name: 'c.tuple', type: 'contractObject', doc: 'the string type' },
{ name: 'c.any', type: 'contractObject', doc: 'accepts any value' }
]}]
};
var showdown = new s.converter();
function renderDoc(strings) {
return showdown.makeHtml(strings.join("\n"));
}
function renderValue(item, name) {
return {
name: item.name || name,
type: item.toString(),
doc: renderDoc(item.theDoc)
};
}
function renderType(type, name) {
var result = {
name: type.contractName,
doc: renderDoc(type.theDoc)
};
if (!type.fieldContracts) {
result.type = type.toString();
} else {
result.isObject = true;
result.fields = __.map(type.fieldContracts, renderValue);
}
return result;
}
function renderCategories(mod) {
function filterForCat(list, cat) {
var result = {};
__.each(list, function(v, n) {
if (v.category === cat)
result[n] = v;
});
return result;
}
function addTypesValues(r, types, values) {
r.types = __.map(types, renderType);
r.values = __.map(values, renderValue);
r.hasTypes = !__.isEmpty(r.types);
r.hasValues = !__.isEmpty(r.values);
}
var result = __.map(mod.categories, function(cat) {
var result = {
hasHeader: true,
name: c.name,
doc: renderDoc(c.doc)
};
addTypesValues(result,
filterForCat(mod.types, cat.name),
filterForCat(mod.values, cat.name));
return result;
});
var otherValues = filterForCat(mod.values, false);
var otherTypes = filterForCat(mod.types, false);
if (otherValues || otherTypes) {
var other = { hasHeader: false };
addTypesValues(other, otherTypes, otherValues);
result.unshift(other);
}
return result;
}
function renderModule(name) {
var mod = c.documentationTable[name];
return {
name: name,
doc: renderDoc(mod.doc),
categories: renderCategories(mod)
};
}
function generateHTML() {
fs.readFile('module.mustache', 'ascii', function(err, template) {
if (err) {
console.error("Could not open file: %s", err);
process.exit(1);
}
console.log(renderModule('Contracts'));
var html = m.to_html(template,
//moduleTemplateData
renderModule('Contracts')
);
console.log(html);
fs.writeFileSync("output.html", html, "ascii");
});
}
generateHTML();