-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
144 lines (125 loc) · 4.45 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
/** @module pandocjs */
const os = require('os');
const fs = require('fs-extra');
const path = require('path');
const cp = require('child_process');
const gr = require('github-release-packager');
const pandoctypes = require('./lib/pandoc-types');
gr.UpdateBinarySync();
/**
* @param {'Input'|'Output'} kind
* @param {string} format
*/
function validateFormat (kind, format) {
var formatList = Object.keys(pandoctypes[`${kind}Formats`]);
if (!formatList.includes(format)) {
throw Error(`Unsupported ${kind.toLowerCase()} format [${format}]. Supported formats are: ${formatList.join(', ')}`);
}
}
/**
* @param {import('./lib/pandoc-types').PandocOptions} options A
* {@link module:pandoc-types~PandocOptions} object.
* @returns {string} An entire command line string.
*/
function validateOptions (options) {
var result = [`"${gr.GetExecutableSync('pandoc')}"`];
Object.keys(options).forEach(optionKey => {
if (options[optionKey] === undefined) {
return; // eslint-disable-line no-useless-return
}
var literal = pandoctypes.Literals[optionKey];
if (typeof literal !== 'string') {
throw Error(`Literal for option '${optionKey}' could not be found.`);
}
var optionValue = options[optionKey];
var optionType = typeof optionValue;
switch (optionType) {
case 'boolean':
result.push(literal);
break;
case 'string':
if (`${optionValue}`.indexOf(' ') >= 0) {
result.push(`${literal}="${optionValue}"`);
} else {
result.push(`${literal}=${optionValue}`);
}
break;
case 'object':
var optionValues = [];
if (!Array.isArray(optionValue)) {
optionValues = [optionValue];
} else {
optionValues = optionValue;
}
optionValues.forEach(optionValue => {
var keyValues = [];
Object.keys(optionValue).sort().forEach(key => {
var keyValue = optionValue[key];
if (`${keyValue}`.indexOf(' ') >= 0) {
keyValues.push(`"${keyValue}"`);
} else {
keyValues.push(`${keyValue}`);
}
});
result.push(`${literal}=${keyValues.join(':')}`);
});
break;
default:
console.warn(`Unknown option type '${optionType}' of value '${optionValue}' in option [${optionKey}] (literal '${literal}').`);
break;
}
});
return result.join(' ');
}
/**
* @param {string} inputFile The file to read from.
* @param {import('./lib/pandoc-types').InputFormat} inputFormat Format expected
* in `inputFile`.
* @param {string} outputFile The file to write to.
* @param {import('./lib/pandoc-types').OutputFormat} outputFormat Format to
* apply in `outputFile`.
* @param {import('./lib/pandoc-types').PandocOptions=} options A
* {@link module:pandoc-types~PandocOptions} object.
*/
exports.ConvertFileSync = (inputFile, inputFormat, outputFile, outputFormat, options) => {
validateFormat('Input', inputFormat);
validateFormat('Output', outputFormat);
options = options || {};
options.output = outputFile;
options.to = outputFormat;
options.from = inputFormat;
var cmdLine = validateOptions(options);
cmdLine += ` "${inputFile}"`;
var result = cp.execSync(cmdLine).toString();
if (result && typeof result === 'string' && result.length > 0) {
throw Error(result);
}
};
/**
* @param {string} input The content to convert and save.
* @param {import('./lib/pandoc-types').InputFormat} inputFormat Format expected
* in `input`.
* @param {string} outputFile The file to write to.
* @param {import('./lib/pandoc-types').OutputFormat} outputFormat Format to
* apply in `outputFile`.
* @param {import('./lib/pandoc-types').PandocOptions=} options A
* {@link module:pandoc-types~PandocOptions} object.
*/
exports.SaveSync = (input, inputFormat, outputFile, outputFormat, options) => {
validateFormat('Input', inputFormat);
validateFormat('Output', outputFormat);
options = options || {};
options.output = outputFile;
options.to = outputFormat;
options.from = inputFormat;
var cmdLine = validateOptions(options);
var tempPath = fs.mkdtempSync(path.join(os.tmpdir(), 'temp-pandoc-'));
var inputFile = path.join(tempPath, inputFormat);
fs.writeFileSync(inputFile, input);
cmdLine += ` "${inputFile}"`;
var result = cp.execSync(cmdLine).toString();
if (result && typeof result === 'string' && result.length > 0) {
throw Error(result);
}
fs.removeSync(tempPath);
};