forked from source-academy/sicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessFigureHtml.js
executable file
·103 lines (89 loc) · 3.13 KB
/
processFigureHtml.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
import {
recursiveProcessTextHtml,
processTextHtml,
toIndexFolder
} from "../parseXmlHtml";
import { referenceStore } from "./processReferenceHtml";
export const processFigureHtml = (node, writeTo) => {
// console.log("processing FIGURE");
// console.log(node);
// FIGURE can have FIGURE inside; src of image can be
// in either the outer or the inner FIGURE
// (inner FIGURE should be called IMAGE, but isn't)
// get src of image; if src is null, there is no image
let src = node.getAttribute("src");
if (!src && node.getElementsByTagName("FIGURE")[0]) {
src = node.getElementsByTagName("FIGURE")[0].getAttribute("src");
}
// get scale_factor, default: 100%
let scale_fraction = null;
if (node.getAttribute("split_scale")) {
scale_fraction = node.getAttribute("split_scale");
} else if (node.getAttribute("web_scale")) {
scale_fraction = node.getAttribute("web_scale");
} else if (
node.getElementsByTagName("FIGURE") &&
node.getElementsByTagName("FIGURE")[0] &&
node.getElementsByTagName("FIGURE")[0].getAttribute("split_scale")
) {
scale_fraction = node
.getElementsByTagName("FIGURE")[0]
.getAttribute("split_scale");
} else if (
node.getElementsByTagName("FIGURE") &&
node.getElementsByTagName("FIGURE")[0] &&
node.getElementsByTagName("FIGURE")[0].getAttribute("web_scale")
) {
scale_fraction = node
.getElementsByTagName("FIGURE")[0]
.getAttribute("web_scale");
}
// every outer FIGURE must have a LABEL
const label = node.getElementsByTagName("LABEL")[0];
// handle case where figure does not have label (figure in original sicp)
if (src && !label) {
writeTo.push(`
<img src="${toIndexFolder}${src}">`);
return;
}
// get href and displayed name from "referenceStore"
const referenceName = label.getAttribute("NAME");
// console.log("reference name is " + referenceName);
const href = referenceStore[referenceName]
? referenceStore[referenceName].href
: "";
// console.log("lookup successful");
const displayName = referenceStore[referenceName]
? referenceStore[referenceName].displayName
: "";
writeTo.push(`<FIGURE>`);
if (src) {
if (scale_fraction) {
const scale_fraction_number = parseFloat(scale_fraction);
const scale_percentage = scale_fraction_number * 100;
writeTo.push(`
<img style="width: ${scale_percentage}%" id="fig_${displayName}" src="${toIndexFolder}${src}">`);
} else {
writeTo.push(`
<img id="fig_${displayName}" src="${toIndexFolder}${src}">`);
}
}
const snippet = node.getElementsByTagName("SNIPPET")[0];
if (snippet) {
processTextHtml(snippet, writeTo);
}
const table = node.getElementsByTagName("TABLE")[0];
if (table) {
processTextHtml(table, writeTo);
}
const caption = node.getElementsByTagName("CAPTION")[0];
if (caption) {
writeTo.push(`
<div class="chapter-text-CAPTION">
<b><a class="caption" href="./${href}">Figure ${displayName} </a></b>`);
recursiveProcessTextHtml(caption.firstChild, writeTo);
writeTo.push("</div>");
}
writeTo.push(`</FIGURE>`);
};
export default processFigureHtml;