forked from source-academy/sicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessFigureJson.js
executable file
·77 lines (65 loc) · 2.35 KB
/
processFigureJson.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
import { recursiveProcessTextJson, processTextJson } from "../parseXmlJson";
import { referenceStore } from "./processReferenceJson";
export const processFigureJson = (node, obj) => {
// 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: 70%
let scale_fraction = 0.7;
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("web_scale")
) {
scale_fraction = node
.getElementsByTagName("FIGURE")[0]
.getAttribute("web_scale");
}
// every outer FIGURE must have a LABEL
const label = node.getElementsByTagName("LABEL")[0];
// get href and displayed name from "referenceStore"
const referenceName = label.getAttribute("NAME");
const href = referenceStore[referenceName]
? referenceStore[referenceName].href
: "";
const displayName = referenceStore[referenceName]
? referenceStore[referenceName].displayName
: "";
if (src) {
const scale_fraction_number = parseFloat(scale_fraction);
const scale_percentage = scale_fraction_number * 100;
if (scale_fraction !== 1) {
obj["scale"] = `${scale_percentage}%`;
}
obj["src"] = `${src}`;
obj["id"] = `#fig-${displayName}`;
}
const snippet = node.getElementsByTagName("SNIPPET")[0];
if (snippet) {
const snippetObj = {};
obj["snippet"] = snippetObj;
processTextJson(snippet, snippetObj);
}
const table = node.getElementsByTagName("TABLE")[0];
if (table) {
const tableObj = {};
obj["table"] = tableObj;
processTextJson(table, tableObj);
}
const caption = node.getElementsByTagName("CAPTION")[0];
if (caption) {
const captionBody = {};
recursiveProcessTextJson(caption.firstChild, captionBody);
obj["captionHref"] = href;
obj["captionName"] = "Figure " + displayName + " ";
obj["captionBody"] = captionBody["child"];
}
};
export default processFigureJson;