-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (40 loc) · 1.29 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
var asciidoctor = require('asciidoctor.js')();
require('asciidoctor-docbook.js')();
var fs = require('fs');
var nodePandoc = require('node-pandoc');
var path = require("path");
// Take file name as parameter and the file's name.
var filePath = process.argv[2];
var newFileName = process.argv[3];
if (typeof (filePath) == "undefined") {
console.log("Error - missing arguments.");
return;
}
// If epub's name is not specified use the name of asciidoc book.
if (typeof (newFileName) == "undefined") {
newFileName = path.basename(filePath, path.extname(filePath));
console.log(`New file will be named ${newFileName}.epub`);
}
fs.readFile(__dirname + '/' + filePath, (err, data) => {
if (err) throw err;
// console.log(data.toString());
var docbook = asciidoctor.convert(data, {
attributes: {
backend: 'docbook5',
doctype: 'book'
},
header_footer: true
});
// console.log(docbook);
// Check directory existence.
let directory = __dirname + '/' + 'output/';
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
// Arguments can be either a single String or in an Array.
let args = '-f docbook -t epub -o output/' + newFileName + '.epub --quiet';
// Call pandoc
nodePandoc(docbook, args, (err) => {
err ? console.log('Error during conversion!') : console.log('Done!');
});
});