forked from st3inum/notebook-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notebookgen.js
204 lines (179 loc) · 5.5 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
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
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')
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
function repl(filePath) {
const data = fs_1.default.readFileSync(filePath);
const _text = data.toString('utf-8');
const text = _text
.replace(/({verbatim})/g,'{lstlisting}')
.replace(/\\subsubsection{([^}]*.)}\\label{[^}]*.}/g, '$1')
.replace(/(\\subsection)/g, '\\subsubsection')
.replace(/(\\section)/g, '\\subsection');
if (text !== _text) {
fs_1.default.writeFileSync(filePath, text);
return 0;
}
return 1;
}
function repe(filePath) {
const data = fs_1.default.readFileSync(filePath);
const _text = data.toString('utf-8');
const text = _text.replace(/(```)[a-zA-Z]{1,}/g,'```').replace(/!\[.*\.png\)/g,'');
if (text !== _text) {
fs_1.default.writeFileSync(filePath, text);
return 0;
}
return 1;
}
function renam(str)
{
let tem='';
for (var i = 0; i < str.length; i++) {
if(str[i]!=='_')tem+=str[i];
else tem+=' ';
}
return tem;
}
function sleep(miliseconds) {
var currentTime = new Date().getTime();
while (currentTime + miliseconds >= new Date().getTime()) {
}
}
const section = ['\\section{', '\\subsection{', '\\subsubsection{']
const extensions = {
'.cc': 'C++',
'.cpp': '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
}
let f = path.resolve(_path, file)
let stat = fs.lstatSync(f)
if (stat.isDirectory()) {
ans += '\n' + section[depth] + renam(file) + '}\n' + walk(f, depth + 1)
} else if (path.extname(f) in extensions) {
if (path.extname(f) !== '.tex') {
ans += '\n' + section[depth] + renam(file.split('.')[0]) + '}\n'
ans += `\\begin{lstlisting}[language=${extensions[path.extname(f)]}]\n` + fs.readFileSync(f) + '\\end{lstlisting}\n'
} else {
repe(f);
if(repl(f))
{
ans += '\n' + section[depth] + renam(file.split('.')[0]) + '}\n'
}
ans += fs.readFileSync(f)
}
}
else if(path.extname(f)=='.md'){
repe(f);
var exec = require('child_process').exec;
var cmd = 'pandoc '+f+' -o '+path.parse(f).name+'.tex';
exec(cmd, function(error, stdout, stderr) {});
sleep(100);
f=path.parse(f).name+'.tex';
if(repl(f))
{
ans += '\n' + section[depth] + renam(file.split('.')[0]) + '}\n'
}
ans += fs.readFileSync(f)
cmd = 'rm '+f;
exec(cmd, function(error, stdout, stderr) {});
}
})
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
}
function normalizeUnixStyle (currentPath) {
if (os.type() === 'Windows_NT') {
return currentPath.replace(/\\/g, '/')
}
return currentPath
}
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
.replace(`\${author}`, options.author)
.replace(`\${initials}`, options.initials)
.replace(`\${fontSize}`, options.size)
.replace(`\${columns}`, options.columns)
.replace(`\${paper}`, options.paper)
.replace(`\${image}`, options.image)
template += walk(_path, 0)
template += '\\end{multicols}\n'
template += '\\end{document}'
// fs_1.default.writeFileSync('./notebook.tex', template);
pdflatex(template).pipe(fs.createWriteStream(options.output))
}