forked from pin3da/notebook-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotebookgen.js
130 lines (116 loc) · 3.63 KB
/
notebookgen.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
const fs = require("fs");
const path = require("path");
const spawn = require("child_process").spawn;
const through2 = require("through2");
const tmp = require("tmp");
const os = require("os");
const section = ["\\section{", "\\subsection{", "\\subsubsection{"];
const extensions = {
".cc": "C++",
".cpp": "C++",
".hpp": "C++",
".c": "c",
".java": "java",
".py": "python",
".tex": "tex",
".go": "golang",
};
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
}
const f = path.resolve(_path, file);
const stat = fs.lstatSync(f);
if (stat.isDirectory()) {
ans += "\n" + section[depth] + file + "}\n" + walk(f, depth + 1);
} else if (path.extname(f) in extensions) {
ans += "\n" + section[depth] + file.split(".")[0] + "}\n";
if (path.extname(f) !== ".tex") {
ans +=
`\\begin{minted}{${extensions[path.extname(f)]}}\n` +
fs.readFileSync(f) +
"\\end{minted}\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) {
const tex = spawn("latexmk", ["-xelatex", "-shell-escape", texPath], {
cwd: tmpobj.name,
env: process.env,
});
tex.on("error", function (err) {
console.error(err);
});
tex.on("exit", function (code, signal) {
const outputFile = texPath.split(".")[0] + ".pdf";
fs.access(outputFile, function (err) {
if (err) {
return console.error("Not generated " + code + " : " + signal);
}
const s = fs.createReadStream(outputFile);
s.pipe(ans);
s.on("close", function () {
tmpobj.removeCallback();
});
});
});
}
function latexmk(doc) {
const tmpobj = tmp.dirSync({ unsafeCleanup: true });
const texPath = path.join(tmpobj.name, "_notebook.tex");
const ans = through2();
ans.readable = true;
const input = fs.createWriteStream(texPath);
input.end(doc);
input.on("close", function () {
genpdf(ans, texPath, tmpobj);
});
return ans;
}
// function normalizeUnixStyle(currentPath) {
// if (os.type() === "Windows_NT") {
// return currentPath.replace(/\\/g, "/");
// }
// return currentPath;
// }
function templateParameter(parameter) {
return `\${${parameter}}`;
}
module.exports = function (_path, options) {
options.output = options.output || "./notebook.pdf";
options.author = options.author || "";
options.initials = options.initials || "";
if (!options.size.endsWith("pt")) options.size += "pt";
// if (options.image) {
// options.image = normalizeUnixStyle(path.resolve(options.image));
// options.image =
// "\\centering{\\includegraphics[width=3.5cm]{" + options.image + "}}";
// } else {
// options.image = "";
// }
let template = fs
.readFileSync(path.join(__dirname, "template_header.tex"))
.toString();
template = template
.replaceAll(templateParameter("author"), options.author)
.replaceAll(templateParameter("initials"), options.initials)
.replaceAll(templateParameter("fontSize"), options.size)
.replaceAll(templateParameter("columns"), options.columns)
.replaceAll(templateParameter("paper"), options.paper)
.replaceAll(templateParameter("image"), options.image);
template += walk(_path, 0);
template += "\\end{multicols}\n";
template += "\\end{document}";
latexmk(template).pipe(fs.createWriteStream(options.output));
};