forked from source-academy/sicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessFigurePdf.js
124 lines (111 loc) · 3.57 KB
/
processFigurePdf.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
import { recursiveProcessTextLatex } from "../parseXmlLatex";
import { processSnippetPdf } from ".";
import { processTablePdf } from ".";
export const processFigurePdf = (node, writeTo) => {
let src = node.getAttribute("src");
if (!src && node.getElementsByTagName("FIGURE")[0]) {
src = node.getElementsByTagName("FIGURE")[0].getAttribute("src");
}
// =================== Process scale parameter of includegraphics ========================
let scale = "0.65";
if (node.getAttribute("scale")) {
scale = node.getAttribute("scale");
} else if (
node.getElementsByTagName("FIGURE")[0] &&
node.getElementsByTagName("FIGURE")[0].getAttribute("scale")
) {
scale = node.getElementsByTagName("FIGURE")[0].getAttribute("scale");
} else {
// Keep default scale at 0.65
}
// =================== Process scale parameter of includegraphics ========================
// =================== Process alignment ========================
let center = true;
if (node.getAttribute("CENTER")) {
center = node.getAttribute("CENTER") === "yes";
} else if (
node.getElementsByTagName("FIGURE")[0] &&
node.getElementsByTagName("FIGURE")[0].getAttribute("CENTER")
) {
center =
node.getElementsByTagName("FIGURE")[0].getAttribute("CENTER") === "yes";
} else {
// Keep default centering === true
}
// =================== Process alignment ========================
writeTo.push(
"\\begin{figure}" +
"[tp]" + // (ancestorHasTag(node, "EXERCISE") ? "[H]" : "[tp]") +
"\n" +
(center ? "\\centering " : "")
);
if (src) {
writeTo.push(generateImage2(src, scale) + "\n");
} else {
// console.log(node.toString());
const images = node.getElementsByTagName("IMAGE");
for (let i = 0; i < images.length; i++) {
writeTo.push(
"\\subcaptionbox{}{" +
generateImage2(images[i].getAttribute("src"), scale) +
"}\n"
);
}
}
const pdfOnly = node.getElementsByTagName("PDF_ONLY")[0];
if (pdfOnly) {
recursiveProcessTextLatex(pdfOnly.firstChild, writeTo);
}
const snippet = node.getElementsByTagName("SNIPPET")[0];
if (snippet) {
processSnippetPdf(snippet, writeTo);
}
const table = node.getElementsByTagName("TABLE")[0];
if (table) {
processTablePdf(table, writeTo);
}
const caption = node.getElementsByTagName("CAPTION")[0];
if (caption) {
writeTo.push(
"\\caption{\\def\\inlinecodesize{\\protect\\inlineexercisecodesize}"
);
recursiveProcessTextLatex(caption.firstChild, writeTo);
writeTo.push("}\n");
}
const label = node.getElementsByTagName("LABEL")[0];
if (label) {
writeTo.push("\\label{" + label.getAttribute("NAME") + "}\n");
}
if (node.getAttribute("CONTINUED").toLowerCase() === "yes") {
writeTo.push("\\addtocounter{figure}{-1}\n");
}
writeTo.push("\\end{figure}");
};
export const generateImage2 = (imagePath, scale) => {
return (
"\n\\adjustbox{max width=32pc}{" +
"\\includegraphics[scale=" +
scale +
"]{{" +
imagePath
.replace(/\.gif$/, ".png")
.replace(/\.svg$/, ".svg.pdf")
.replace(/\.(?=[^.]*$)/, "}.")
.replace(/_/g, "\\string_") +
"}}"
);
};
//export const generateImage = (imagePath) => generateImage(imagePath, "0.65");
export const generateImage = imagePath => {
return (
"\n\\adjustbox{max width=32pc}{" +
"\\includegraphics[scale=0.65]{{" +
imagePath
.replace(/\.gif$/, ".png")
.replace(/\.svg$/, ".svg.pdf")
.replace(/\.(?=[^.]*$)/, "}.")
.replace(/_/g, "\\string_") +
"}}"
);
};
export default processFigurePdf;