forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclean_latex.js
More file actions
77 lines (74 loc) · 2.7 KB
/
clean_latex.js
File metadata and controls
77 lines (74 loc) · 2.7 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
var allowedUnicode = /[→←“”’π½⅓¼…×βϕ≈é—]/;
function replaceUnicode(input) {
var inCode = false;
return input.replace(/\\(end|begin)\{lstlisting\}|([\x80-\uffff])/g, function(full, beginEnd, ch) {
if (beginEnd) {
inCode = beginEnd == "begin";
return full;
} else {
if (!allowedUnicode.test(ch)) {
console.error("Found unhandled unicode: " + ch);
process.exit(1);
}
if (inCode && /[“”]/.test(ch)) return '"';
if (inCode && /[‘’]/.test(ch)) return "'";
return ch;
}
});
}
var escaped = {"\\{{}": "{",
"\\}{}": "}",
"\\textbackslash{}": "\\",
"\\${}": "$",
"\\textless{}": "<",
"\\textgreater{}": ">",
"\\&{}": "&",
"\\_{}": "_",
"\\%{}": "%",
"\\#{}": "\\#",
"\\textasciicircum{}": "^",
"\\textasciitilde{}": "~",
"\\textbar{}": "|",
"\\textquotedbl{}": "\""};
var specials = Object.keys(escaped).map(function(k) {return k.replace(/[^\w\s]/g, "\\$&");}).join("|") + "|[^]";
var deEscapeRE = new RegExp("\\\\lstinline:::((?:" + specials + ")+?):::", "g");
var specialRE = new RegExp(specials, "g");
function cleanLstInline(str) {
return str.replace(deEscapeRE, function(m, content) {
return "\\lstinline`" + content.replace(specialRE, function(f) {
if (f.length > 1) return escaped[f];
else if (f == "\n") return " ";
else return f;
}) + "`";
});
}
var input = "";
process.stdin.on("data", function(chunk) {
input += chunk;
});
process.stdin.on("end", function() {
input = input.replace(/(\n\n\\end{Code})|(\n{3,})|(_why,)|\\chapter\{(Introduction|Exercise Hints)\}|\\hyperref\[((?:[^\]]|\\_\{\})+)\]|\\index\{([^|}]+?)\\textbar\{\}see\{([^}]+)}}|\\textasciicircum\{\}\{([^\}]+?)\}|(���)/g,
function(all, codeSpace, manyBlanks, why, simplechapter, link, seeFrom, seeTo, superscript, bogusChars) {
if (codeSpace)
return codeSpace.slice(1);
if (manyBlanks)
return "\n\n";
if (why)
return "\\_why,";
if (simplechapter)
return "\\chapter*{" + simplechapter + "}";
if (link)
return "\\hyperref[" + link.replace(/\\_\{\}/g, "_") + "]";
if (seeFrom)
return "\\index{" + seeFrom + "|see{" + seeTo + "}}";
if (superscript)
return "\\textsuperscript{" + superscript + "}";
if (bogusChars)
return "→";
});
input = cleanLstInline(input);
input = input.replace(/({\\hspace\*\{.+?\}\\itshape``)\s*([^]+?)\s*('')/g, "$1$2$3");
input = replaceUnicode(input);
process.stdout.write(input);
});
process.stdin.resume();