forked from jgnewman/jandoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjandoc.js
235 lines (216 loc) · 5.79 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Expose the Jandoc API to JavaScript
*/
var procedure = require('./lib/procedure'),
argParse = require('./lib/argparser').parse,
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.
*
* Options -
* input: DIR/FILE PATH
* output: DIR/FILE PATH
* read: FILE TYPE STRING
* write: FILE TYPE STRING
* dataDir: DIR PATH
* strict: BOOLEAN
* parseRaw: BOOLEAN
* smart: BOOLEAN // refers to smart quotes
* oldDashes: BOOLEAN
* baseHeaderLevel: NUMBER
* indentedCodeClasses: STRING
* nomalize: BOOLEAN
* preserveTabs: BOOLEAN
* tabStop: NUMBER
* standalone: BOOLEAN
* template: FILE PATH
* variable: OBJECT
* printDefaultTemplate: FILE PATH
* noWrap: BOOLEAN
* columns: NUMBER
* toc: BOOLEAN
* noHighlight: BOOLEAN
* highlightStyle: STRING
* includeInHeader: FILE PATH
* includeBeforeBody: FILE PATH
* includeAfterBody: FILE PATH
* selfContained: BOOLEAN
* offline: BOOLEAN
* html5: BOOLEAN
* ascii: BOOLEAN
* referenceLinks: BOOLEAN
* atxHeaders: BOOLEAN
* chapters: BOOLEAN
* numberSections: BOOLEAN
* noTexLigatures: BOOLEAN
* listings: BOOLEAN
* incremental: BOOLEAN
* slideLevel: NUMBER
* sectionDivs: BOOLEAN
* emailObfuscation: STRING (none || javascript || references)
* idPrefix: STRING
* titlePrefix: STRING
* css: URL PATH
* referenceOdt: FILE PATH
* referenceDocx: FILE PATH
* epubStylesheet: FILE PATH
* epubCoverImage: FILE PATH
* epubMetadata: FILE PATH
* epubEmbedFont: ARRAY
* latexEngine: PROGRAM NAME
* bibliography: FILE PATH
* csl: FILE PATH
* citationAbbreviations: FILE PATH
* natbib: BOOLEAN
* biblatex: BOOLEAN
* latexmathml: URL PATH
* asciimathml: URL PATH
* mathml: URL PATH
* mimetex: URL PATH
* webtex: URL PATH
* jsmath: URL PATH
* mathjax: URL PATH
* gladtex: BOOLEAN
* dumpArgs: BOOLEAN
* ignoreArgs: BOOLEAN
* version: BOOLEAN
* help: BOOLEAN
*/
function buildArgString(options) {
var argString = '', key, type, val, i;
for (key in options) {
if (has(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 variable option is an object. Deal with
* it as such.
*/
if (key === 'variable') {
for (i in val) {
if (has(val, i)) {
argString += (' --variable ' + i + '=' + val[i]);
}
}
/*
* The epubEmbedFont variable is an array. Deal
* with it as such.
*/
} else if (key === 'epubEmbedFont') {
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']
]);
procedure.init(cleanArgs, args, callback); // 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);
}
/*
* Allow the user to access the command line
* version through jandoc.cmd.
*/
jandoc.cmd = 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;
}());