forked from pin3da/notebook-generator
-
Notifications
You must be signed in to change notification settings - Fork 30
/
codes2pdf.js
96 lines (86 loc) · 2.67 KB
/
codes2pdf.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
const fs = require('fs')
const path = require('path')
const spawn = require('child_process').spawn
const through2 = require('through2')
const tmp = require('tmp')
const section = ['\\section{', '\\subsection{', '\\subsubsection{']
const extensions = ['.cc', '.cpp', '.c', '.java', '.py', '.tex']
function walk (_path, depth) {
let ans = ''
depth = Math.min(depth, section.length - 1)
fs.readdirSync(_path).forEach(function (file) {
if (file.startsWith('.')) {
return // hidden directory
}
let f = path.resolve(_path, file)
let stat = fs.lstatSync(f)
if (stat.isDirectory()) {
ans += '\n' + section[depth] + file + '}\n' + walk(f, depth + 1)
} else if (extensions.indexOf(path.extname(f)) !== -1) {
ans += '\n' + section[depth] + file.split('.')[0] + '}\n'
if (path.extname(f) !== '.tex') {
ans += '\\begin{lstlisting}\n' + fs.readFileSync(f) + '\\end{lstlisting}\n'
} else {
ans += fs.readFileSync(f)
}
}
})
return ans
}
/**
* pdf must be generated twice in order to generate the table of contents.
* According to some tests, in windows it must be generated 3 times.
* */
function genpdf (ans, texPath, tmpobj, iter) {
let tex = spawn('pdflatex', [
'-interaction=nonstopmode',
texPath
], {
cwd: tmpobj.name,
env: process.env
})
tex.on('error', function (err) {
console.error(err)
})
tex.on('exit', function (code, signal) {
let outputFile = texPath.split('.')[0] + '.pdf'
fs.access(outputFile, function (err) {
if (err) {
return console.error('Not generated ' + code + ' : ' + signal)
}
if (iter === 0) {
let s = fs.createReadStream(outputFile)
s.pipe(ans)
s.on('close', function () {
tmpobj.removeCallback()
})
} else {
genpdf(ans, texPath, tmpobj, iter - 1)
}
})
})
}
function pdflatex (doc) {
let tmpobj = tmp.dirSync({ unsafeCleanup: true })
let texPath = path.join(tmpobj.name, '_notebook.tex')
let ans = through2()
ans.readable = true
let input = fs.createWriteStream(texPath)
input.end(doc)
input.on('close', function () {
let iters = process.platform === 'win32' ? 2 : 1
genpdf(ans, texPath, tmpobj, iters)
})
return ans
}
module.exports = function (_path, output, author, initials) {
let template = fs.readFileSync(path.join(__dirname, 'template_header.tex')).toString()
template = template
.replace(`\${author}`, author)
.replace(`\${initials}`, initials)
template += walk(_path, 0)
template += '\\end{multicols}'
template += '\\end{document}'
output = output || './notebook.pdf'
pdflatex(template).pipe(fs.createWriteStream(output))
}