-
Notifications
You must be signed in to change notification settings - Fork 2
/
pandoc.js
118 lines (92 loc) · 2.37 KB
/
pandoc.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
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var mkdirp = require('mkdirp');
var extensions = /\.(md|markdown)$/i;
var reInclude = /(?:^|\n)@include\s(.*)\r*\n/;
// eventually
var includeCache = {};
function markitdown(inputFile, overwrite, outPath, options) {
var outputFile = path.join(outPath, inputFile.replace(extensions, '.html'));
var args = [
// inputFile,
// '-o', outputFile,
//
//'--toc',
'-t', 'html5',
'--tab-stop', '4',
'--standalone',
'--highlight-style', 'pygments',
'--section-divs',
// '-T', 'node-dcl',
// '-c', 'test.css'
];
if (options.head) {
args.push('--include-in-header', options.head);
}
if (options.header) {
args.push('--include-before-body', options.header);
}
if (options.footer) {
args.push('--include-after-body', options.footer);
}
if (options.title) {
args.push('-T', options.title);
}
var pandoc = spawn('pandoc', args);
var outstream = (outputFile)
? fs.createWriteStream(outputFile)
: process.stdout
;
var instream = fs.createReadStream(inputFile, { encoding: 'utf8' });
console.warn(inputFile);
instream.on('data', function(data) {
// var include = data.match(reInclude);
// if (include) {
// include = path.join(path.dirname(inputFile), include[1]);
// }
// var rs= fs.createReadStream(include, { encoding: 'utf8' });
// rs.pipe(pandoc.stdin);
// rs.on('end', function() {
pandoc.stdin.write(data);
// });
});
instream.on('end', function() {
pandoc.stdin.end();
});
pandoc.stdout.pipe(outstream);
pandoc.stderr.pipe(process.stderr);
}
// todo consolidate into one options object
function checkPath(inputPath, overwrite, outPath, prefix, options) {
// temp
overwrite = true;
prefix = prefix || '';
outPath = outPath || '';
inputPath.forEach(function(ipath) {
ipath = path.join(prefix, ipath);
fs.stat(ipath, function(err, stats) {
if (err) {
console.warn(err);
return;
}
if (stats.isDirectory()) {
fs.readdir(ipath, function(err, files) {
checkPath(files, overwrite, outPath, ipath, options);
});
}
else {
if (extensions.test(ipath)) {
mkdirp(path.join(outPath, prefix), function(err) {
if (err) {
console.warn(err);
return;
}
markitdown(ipath, overwrite, outPath, options);
})
}
}
});
});
};
module.exports = checkPath;