forked from Mermade/widdershins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semoasa.js
91 lines (75 loc) · 2.6 KB
/
semoasa.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
'use strict';
const path = require('path');
const yaml = require('js-yaml');
const dot = require('dot');
dot.templateSettings.strip = false;
dot.templateSettings.varname = 'data';
const common = require('./common.js');
const dereference = require('reftools/lib/dereference.js').dereference;
const oas_descs = require('./resources/oas_descs.js');
let templates = {};
function preProcessor(api) {
return api;
}
function convert(api, options, callback) {
let templates;
api = preProcessor(api);
let defaults = {};
defaults.includes = [];
defaults.search = true;
defaults.theme = 'darkula';
options = Object.assign({},defaults,options);
let header = {};
header.title = (api.info ? api.info.title : 'Semoasa documentation');
header.language_tabs = ['json','yaml'];
header.toc_footers = [];
if (api.externalDocs) {
if (api.externalDocs.url) {
header.toc_footers.push('<a href="' + api.externalDocs.url + '">' + (api.externalDocs.description ? api.externalDocs.description : 'External Docs') + '</a>');
}
}
header.includes = options.includes;
header.search = options.search;
header.highlight_theme = options.theme;
if (typeof templates === 'undefined') {
templates = dot.process({ path: path.join(__dirname, 'templates', 'semoasa') });
}
if (options.user_templates) {
templates = Object.assign(templates, dot.process({ path: options.user_templates }));
}
let data = {};
if (options.verbose) console.log('starting deref',api.info.title);
data.api = dereference(api,api,{verbose:options.verbose,$ref:'x-widdershins-oldRef'});
if (options.verbose) console.log('finished deref');
data.options = options;
data.header = header;
data.templates = templates;
data.translations = {};
templates.translations(data);
data.oas2_descs = oas_descs.oas2_descs;
data.oas3_descs = oas_descs.oas3_descs;
data.utils = {};
data.utils.yaml = yaml;
data.utils.getSample = common.getSample;
data.utils.schemaToArray = common.schemaToArray;
data.utils.slugify = common.slugify;
data.utils.linkCase = function(s) {
return s[0].toLowerCase()+s.substr(1);
};
data.utils.join = function(s) {
return s.split('\r').join('').split('\n').join(' ').trim();
};
let content;
try {
content = '---\n'+yaml.safeDump(header)+'\n---\n\n'+
templates.main(data);
}
catch (ex) {
return callback(ex);
}
content = common.removeDupeBlankLines(content);
callback(null,content);
}
module.exports = {
convert : convert
};