forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·166 lines (152 loc) · 4.63 KB
/
cli.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
#!/usr/bin/env node
// Simple CLI for KaTeX.
// Reads TeX from stdin, outputs HTML to stdout.
/* eslint no-console:0 */
const katex = require("./");
const fs = require("fs");
const options = require("nomnom")
.option("displayMode", {
full: "display-mode",
abbr: "d",
flag: true,
default: false,
help: "If true the math will be rendered in display " +
"mode, which will put the math in display style " +
"(so \\int and \\sum are large, for example), and " +
"will center the math on the page on its own line.",
})
.option("throwOnError", {
full: "no-throw-on-error",
abbr: "t",
flag: true,
default: true,
transform: function(t) {
return !t;
},
help: "If true, KaTeX will throw a ParseError when it " +
"encounters an unsupported command. If false, KaTeX " +
"will render the unsupported command as text in the " +
"color given by errorColor.",
})
.option("errorColor", {
full: "error-color",
abbr: "c",
metavar: "color",
default: "#cc0000",
transform: function(color) {
return "#" + color;
},
help: "A color string given in the format 'rgb' or 'rrggbb'. " +
"This option determines the color which unsupported " +
"commands are rendered in.",
})
.option("colorIsTextColor", {
full: "color-is-text-color",
abbr: "b",
flag: true,
default: false,
help: "Makes \\color behave like LaTeX's 2-argument \\textcolor, " +
"instead of LaTeX's one-argument \\color mode change.",
})
.option("unicodeTextInMathMode", {
full: "unicode-text-in-math-mode",
abbr: "u",
flag: true,
default: false,
help: "Add support for unicode text characters in math mode.",
})
.option("maxSize", {
full: "max-size",
abbr: "s",
metavar: "size",
default: 0,
help: "If non-zero, all user-specified sizes, e.g. in " +
"\\rule{500em}{500em}, will be capped to maxSize ems. " +
"Otherwise, elements and spaces can be arbitrarily large",
})
.option("macros", {
full: "macro",
abbr: "m",
metavar: "macro:expansion",
list: true,
default: [],
help: "A custom macro. Each macro is a property with a name " +
"like \\name which maps to a string that " +
"describes the expansion of the macro.",
})
.option("macroFile", {
full: "macro-file",
abbr: "f",
metavar: "path",
default: null,
help: "Read macro definitions from the given file.",
})
.option("inputFile", {
full: "input",
abbr: "i",
metavar: "path",
default: null,
help: "Read LaTeX input from the given file.",
})
.option("outputFile", {
full: "output",
abbr: "o",
metavar: "path",
default: null,
help: "Write html output to the given file.",
})
.parse();
function readMacros() {
if (options.macroFile) {
fs.readFile(options.macroFile, "utf-8", function(err, data) {
if (err) {throw err;}
splitMacros(data.toString().split('\n'));
});
} else {
splitMacros([]);
}
}
function splitMacros(macroStrings) {
// Override macros from macro file (if any)
// with macros from command line (if any)
macroStrings = macroStrings.concat(options.macros);
const macros = {};
for (const m of macroStrings) {
const i = m.search(":");
if (i !== -1) {
macros[m.substring(0, i).trim()] = m.substring(i + 1).trim();
}
}
options.macros = macros;
readInput();
}
function readInput() {
let input = "";
if (options.inputFile) {
fs.readFile(options.inputFile, "utf-8", function(err, data) {
if (err) {throw err;}
input = data.toString();
writeOutput(input);
});
} else {
process.stdin.on("data", function(chunk) {
input += chunk.toString();
});
process.stdin.on("end", function() {
writeOutput(input);
});
}
}
function writeOutput(input) {
const output = katex.renderToString(input, options) + "\n";
if (options.outputFile) {
fs.writeFile(options.outputFile, output, function(err) {
if (err) {
return console.log(err);
}
});
} else {
console.log(output);
}
}
readMacros();