forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_latex.js
More file actions
77 lines (70 loc) · 2.52 KB
/
pre_latex.js
File metadata and controls
77 lines (70 loc) · 2.52 KB
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
// Script to pre-process the asciidoc sources for generating LaTeX.
// Adjusts some stylistic things to satisfy No Starch's style, and
// fixes up svg images to point at pdf versions of the images.
var fs = require("fs"), child = require("child_process");
var infile, nostarch = false;
for (var i = 2; i < process.argv.length; i++) {
var arg = process.argv[i];
if (arg == "--nostarch") nostarch = true;
else infile = arg;
}
var instream;
if (infile == "-") {
instream = process.stdin;
instream.resume();
} else {
instream = fs.createReadStream(infile);
}
var titleCaseSmallWords = "a an the at by for in of on to up and as but it or nor if console.log".split(" ");
var input = "";
instream.on("data", function(chunk) {
input += chunk;
});
instream.on("end", function() {
if (infile != "-")
input = ":docid: " + infile.match(/^\d{2}_(.*?)\.txt/)[1] + "\n" + input;
process.stdout.write(input.replace(/\n===? (.*?) ===?|”([.,:;])|\nimage::img\/(.+?)\.(\w+)(\[.*\])?|link:[^\.]+\.html#(.*?)\[|!!(hint)!![^]+?!!hint!!(?:\n|$)/g,
function(match, title, quoted, imgName, imgType, imgOpts, link, solution) {
if (title) { // Section title, must be converted to title case
if (!nostarch) return match;
var kind = /^\n(=*)/.exec(match)[1];
return "\n" + kind + " " + title.split(" ").map(function(word) {
if (titleCaseSmallWords.indexOf(word) == -1)
return word[0].toUpperCase() + word.slice(1);
else
return word;
}).join(" ") + " " + kind;
} else if (quoted) { // Move punctuation into quotes
if (!nostarch) return match;
return quoted + "”";
} else if (imgName) { // Image file
return "\nimage::" + convertImage(imgName, imgType) + (!imgOpts ? "" : nostarch ? imgOpts : calcWidth(imgOpts));
} else if (link) {
return "link:" + link + "[";
} else if (solution) {
return "";
}
}));
});
function calcWidth(opts) {
var replaced = false;
opts = opts.replace(/width=\"([\d.]+)cm\"/, function(m, w) {
replaced = true;
return "width=\"" + (Number(w)/11).toFixed(2) + "\\\\textwidth\"";
});
if (!replaced) return opts.slice(0, opts.length - 1) + ",width=\"0.95\\\\textwidth\"]";
else return opts;
}
function convertImage(name, type) {
var oldName = "img/" + name + "." + type;
if (type == "svg") {
var newName = "img/generated/" + name + ".pdf";
try {
var newAge = fs.statSync(newName).atime;
} catch (e) {
newAge = 0;
}
return newName;
}
return oldName;
}