-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathindex.js
171 lines (148 loc) · 5.08 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
const raml2obj = require('raml2obj');
const pjson = require('./package.json');
const nunjucks = require('nunjucks');
const markdown = require('nunjucks-markdown');
const marked = require('marked');
const Minimize = require('minimize');
const pretty = require('pretty');
const path = require('path');
const fs = require('fs');
/**
* Render the source RAML object using the config's processOutput function
*
* The config object should contain at least the following property:
* processRamlObj: function that takes the raw RAML object and returns a promise with the rendered HTML
*
* @param {(String|Object)} source - The source RAML file. Can be a filename, url, or an already-parsed RAML object.
* @param {Object} config
* @param {Object} options
* @param {Function} config.processRamlObj
* @returns a promise
*/
function render(source, config, options) {
config = config || {};
config.raml2HtmlVersion = pjson.version;
// Check if option is old boolean `validation` to keep backward compatibility
if (typeof options === 'boolean') {
options = {
validate: options,
};
}
if (options === undefined) {
options = {
validate: false,
};
}
return raml2obj
.parse(source, {
validate: options.validate,
extensionsAndOverlays: options.extensionsAndOverlays,
httpResolver: options.httpResolver,
fsResolver: options.fsResolver,
})
.then(ramlObj => {
if (config.processRamlObj) {
return config.processRamlObj(ramlObj, config, options).then(html => {
if (config.postProcessHtml) {
return config.postProcessHtml(html, config, options);
}
return html;
});
}
return ramlObj;
});
}
/**
* @param {String} [mainTemplate] - The filename of the main template, leave empty to use default templates
* @returns {{processRamlObj: Function, postProcessHtml: Function}}
*/
function getConfigForTemplate(mainTemplate) {
const templatesPath = path.dirname(fs.realpathSync(mainTemplate));
const templateFile = path.basename(fs.realpathSync(mainTemplate));
return {
processRamlObj(ramlObj, config, options) {
// Extend ramlObj with config and options so the templates can use those values
ramlObj.config = config;
ramlObj.options = options;
const renderer = new marked.Renderer();
renderer.table = function(thead, tbody) {
// Render Bootstrap style tables
return `<table class="table"><thead>${thead}</thead><tbody>${tbody}</tbody></table>`;
};
// Setup the Nunjucks environment with the markdown parser
const env = nunjucks.configure(templatesPath, { autoescape: false });
if (config.setupNunjucks) {
config.setupNunjucks(env);
}
markdown.register(env, md => marked(md, { renderer }));
ramlObj.isStandardType = function(type) {
if (typeof type === 'object') {
return false;
}
return type && type.indexOf('{') === -1 && type.indexOf('<') === -1;
};
// Render the main template using the raml object and fix the double quotes
let html = env.render(templateFile, ramlObj);
html = html.replace(/"/g, '"');
// Return the promise with the html
return new Promise(resolve => {
resolve(html);
});
},
postProcessHtml(html, config, options) {
if (options.pretty) {
return pretty(html, { ocd: true });
} else {
// Minimize the generated html and return the promise with the result
const minimize = new Minimize({ quotes: true });
return new Promise((resolve, reject) => {
minimize.parse(html, (error, result) => {
if (error) {
reject(new Error(error));
} else {
resolve(result);
}
});
});
}
},
};
}
/**
* @param {String} [theme] - The name of a raml2html template, leave empty if you want to use the mainTemplate option
* @param {Object} [programArguments] - An object containing all program aruments
* @returns {{processRamlObj: Function, postProcessHtml: Function}}
*/
function getConfigForTheme(theme, programArguments) {
if (!theme) {
theme = 'raml2html-default-theme';
}
try {
// See if the theme supplies its own config object (or function that creates this object), and return it
const config = require(theme);
// If it's a function then call it with the program arguments
if (typeof config === 'function') {
return config(programArguments);
}
// Otherwise we assume it's a config object (default behavior)
return config;
} catch (err) {
// Nope, forward to getConfigForTemplate
const templatesPath = path.dirname(
require.resolve(`${theme}/package.json`)
);
return getConfigForTemplate(path.join(templatesPath, 'index.nunjucks'));
}
}
module.exports = {
getConfigForTemplate,
getConfigForTheme,
render,
};
if (require.main === module) {
console.error(
"This script is meant to be used as a library. You probably want to run bin/raml2html if you're looking for a CLI."
);
process.exit(1);
}