-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathcleanTex.js
81 lines (75 loc) · 2.13 KB
/
cleanTex.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
var fs = require('fs');
var walk = require('walk');
var mime = require('mime');
var options = {
followLinks: false
};
var walker = walk.walk('./pdf', options);
walker.on("file", function (root, fileStats, next) {
// remove all old files created by LaTex
if (root === './pdf') {
if (fileStats.name !== 'all.tex' && fileStats.name !== 'options.tex') {
return fs.unlink('./pdf/' + fileStats.name, function (err) {
if (err) console.log(err);
next();
});
}
next();
}
if (root === './pdf/sections_tex') {
if (mime.lookup(fileStats.name) === 'application/x-tex') {
return removeBrokenHyperref(fileStats.name, function(err){
if (err) console.log(err);
next();
});
}
next();
}
next();
});
/**
* Remove all broken hyperrefs in the latex files
*
* Example:
*
* \hyperref[tls\_event\_secureconnect]
* would cause an error and is therefore replaced with
* \hyperref[tls_event_secureconnect]
*
* @param {String} file The .tex file to look for broken refs
* @param {Function} cb Callback function
*/
var removeBrokenHyperref = function(file, cb) {
fs.readFile('./pdf/sections_tex/' + file, function(err, data) {
if (err) return cb(err);
data = data.toString();
// find all \hyperref[...]
var re = /hyperref\[\S+\]/gi;
var hyperrefArray = data.match(re);
// inside the found results look for \_ and replace with _
var re2 = /\\_/gi;
if (hyperrefArray) {
Object.keys(hyperrefArray).forEach(function(key, index) {
var value = hyperrefArray[key];
var myString = value.replace(re2, '_');
// replace string inside data with new string
data = data.replace(value, myString);
// save the new file when done
if (index === hyperrefArray.length - 1) {
fs.writeFile('./pdf/sections_tex/' + file, data, function(err) {
if (err) return cb(err);
cb(null);
});
}
});
} else {
cb(null);
}
});
};
walker.on("errors", function (root, nodeStatsArray, next) {
next();
});
walker.on("end", function () {
console.log("all done");
});