forked from Mermade/widdershins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiddershins.js
138 lines (127 loc) · 3.9 KB
/
widdershins.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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const url = require('url');
const yaml = require('js-yaml');
const fetch = require('node-fetch');
const converter = require('./index.js');
var argv = require('yargs')
.usage('widdershins [options] {input-file|url} [[-o] output markdown]')
.demand(1)
.strict()
.boolean('yaml')
.alias('y','yaml')
.describe('yaml','Load spec in yaml format, default json')
.boolean('aggressive')
.alias('a','aggressive')
.describe('aggressive','Use alternative dereffing logic')
.boolean('code')
.alias('c','code')
.describe('code','Turn generic code samples off')
.string('includes')
.boolean('discovery')
.alias('d','discovery')
.describe('discovery','Include schema.org WebAPI discovery data')
.string('environment')
.alias('e','environment')
.describe('environment','Load config/override options from file')
.number('headings')
.describe('Levels of headings to expand in TOC')
.default('headings','2')
.alias('i','includes')
.describe('includes','List of files to include, comma separated')
.boolean('lang')
.alias('l','lang')
.describe('lang','Automatically generate list of languages for code samples')
.boolean('noschema')
.alias('n','noschema')
.describe('noschema','Do not expand schema definitions')
.string('outfile')
.alias('o','outfile')
.describe('outfile','File to write output markdown to')
.boolean('raw')
.alias('r','raw')
.describe('raw','Output raw schemas not example values')
.boolean('search')
.alias('s','search')
.default('search',true)
.describe('search','Whether to enable search or not, default true')
.boolean('summary')
.describe('summary','Use summary instead of operationId for TOC')
.string('theme')
.alias('t','theme')
.describe('theme','Syntax-highlighter theme to use')
.string('user_templates')
.alias('u','user_templates')
.describe('user_templates','directory to load override templates from')
.boolean('verbose')
.describe('verbose','Increase verbosity')
.help('h')
.alias('h', 'help')
.version()
.argv;
var options = {};
function doit(s) {
var api = {};
try {
api = yaml.safeLoad(s,{json:true});
}
catch(ex) {
console.error('Failed to parse YAML/JSON, falling back to API Blueprint');
console.error(ex.message);
api = s;
}
converter.convert(api,options,function(err,output){
var outfile = argv.outfile||argv._[1];
if (outfile) {
fs.writeFileSync(path.resolve(outfile),output,'utf8');
}
else {
console.log(output);
}
});
}
options.codeSamples = !argv.code;
if (argv.lang) {
options.language_tabs = [];
}
if (argv.theme) options.theme = argv.theme;
options.user_templates = argv.user_templates;
options.inline = argv.inline;
options.sample = !argv.raw;
options.discovery = argv.discovery;
options.aggressive = argv.aggressive;
options.verbose = argv.verbose;
options.tocSummary = argv.summary;
options.headings = argv.headings;
if (argv.search === false) options.search = false;
if (argv.includes) options.includes = argv.includes.split(',');
if (argv.noschema) options.schema = false;
if (argv.environment) {
var e = fs.readFileSync(path.resolve(argv.environment),'utf8');
var env = {};
try {
env = yaml.safeLoad(e,{json:true});
}
catch (ex) {
console.error(ex.message);
}
options = Object.assign({},options,env);
}
var input = argv._[0];
var up = url.parse(input);
if (up.protocol && up.protocol.startsWith('http')) {
fetch(input)
.then(function (res) {
return res.text();
}).then(function (body) {
doit(body);
}).catch(function (err) {
console.error(err.message);
});
}
else {
let s = fs.readFileSync(input,'utf8');
doit(s);
}