This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjandoc.js
172 lines (151 loc) · 4.19 KB
/
jandoc.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
/*
* Expose the Jandoc API to JavaScript
*/
var procedure = require('./lib/procedure'),
argParse = require('./lib/argparser').parse
isSane = require('./lib/sanitize'),
cmdLine = require('child_process').exec;
/*
* Trim extraneous whitespace from strings.
*/
function trim(str) {
return str.replace(/^\s+/, '').replace(/\s+$/, '');
}
/*
* Shortcut for the hasOwnProperty check in for...in loops.
*/
function has(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
/*
* Converts an option key like 'baseHeaderLevel'
* into a bash flag like '--base-header-level'.
*/
function bashifyKey(key) {
var i, len = key.length, builder = '--', caps = /[A-Z]/;
for (i = 0; i < len; i += 1) {
if (caps.test(key[i])) {
builder += ('-' + key[i].toLowerCase());
} else {
builder += key[i];
}
}
return builder;
}
/*
* Pass an object to the jandoc function and each
* key will represent a command line flag.
*/
function buildArgString(options) {
var argString = '', key, type, val, i;
for (key in options) {
if (has(options, key) && isSane(key, options[key])) {
val = options[key];
type = typeof val;
/*
* Input and output are outliers because
* we've modified them. Deal with those
* specifically.
*/
if (key === 'input') {
argString += (' -d ' + val);
} else if (key === 'output') {
argString += (' -o ' + val);
} else {
/*
* Procedure to deal with boolean options and
* non-object options.
*/
if (type === 'boolean') {
argString += (' ' + bashifyKey(key));
} else if (type !== 'object') {
argString += (' ' + bashifyKey(key) + ' ' + val);
} else {
/*
* The epubEmbedFont variable is an array. Deal
* with it as such.
*/
for (i = 0; i < val.length; i += 1) {
argString += (' --epub-embed-font ' + val[i]);
}
}
}
}
}
/*
* Cut off extraneous whitespace and return the argument string.
*/
return trim(argString);
}
/*
* Define a function that initializes the jandoc
* functionality with a command line string.
*/
function callCommand(str, callback) {
var cleanArgs = trim(str).split(/\s+/g), // turns args into an array
args = argParse(cleanArgs, [ // put args into a symlinked object
['-v', '--version'],
['-h', '--help'],
['-d', '--input-data'],
['-o', '--output-location'],
['-t', '-w', '--to', '--write'],
['-f', '-r', '--from', '--read']
]);
if (callback) {
pandoc = procedure(cleanArgs, args, callback); // initialize with both items
} else {
pandoc = procedure(cleanArgs, args); // initialize with both items
}
}
/*
* Define a function that converts an options
* object into a command line string and passes
* that string to callCommand.
*
* We'll call it the jandoc function and expose
* it to the user.
*/
function jandoc(options, callback) {
var argString = buildArgString(options);
callCommand(argString, callback);
}
/* (also make a sync version)
*/
jandoc.sync = function (options) {
var argString = buildArgString(options);
callCommand(argString);
}
/*
* Allow the user to access the command line
* version through jandoc.cmd.
*/
jandoc.cmd = function (str, callback) {
callCommand(str, callback);
}
/* (also make a sync version)
*/
jandoc.cmdSync = callCommand;
/*
* Expose the jandoc function.
*/
module.exports = (function () {
/*
* When the module gets required, do an asynchronous check to see if
* Pandoc exists on the system.
*/
cmdLine('which pandoc', function (err, stdout) {
/*
* If 'which pandoc' throws an error, die.
*/
if (err || !stdout) {
console.error('ERROR: Could not find Pandoc on your system.\n Please make sure Haskell and Pandoc are installed before running Jandoc.');
process.exit(1);
}
});
/*
* Ignore the Pandoc check and return the jandoc function. This way,
* if Pandoc does exist, the user won't get held up by the check. If
* it doesn't, you'll get an error at some point.
*/
return jandoc;
}());