Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON: Simplify container tags + minor fixes #533

Merged
merged 4 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 45 additions & 57 deletions javascript/parseXmlJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ export const addArrayToObj = (obj, node, array) => {
obj["body"] = body;
};

const processContainer = (node, obj) => {
addBodyToObj(obj, node, displayTitle);
obj["tag"] = "SECTION";

const name = getChildrenByTagName(node, "NAME")[0];
recursiveProcessTextJson(name.nextSibling, obj);
};

const processTextFunctions = {
"#text": (node, obj) => {
// ignore the section/subsection tags at the end of chapter/section files
Expand All @@ -83,6 +91,38 @@ const processTextFunctions = {
}
},

// Container tags: tag containing other elements and a heading
SECTION: processContainer,

CHAPTER: processContainer,

MATTER: processContainer,

REFERENCES: processContainer,

SUBSECTION: processContainer,

WEBPREFACE: processContainer,

// e.g. section 4.4.4.4
SUBSUBSECTION: (node, obj) => {
subsubsection_count += 1;
heading_count += 1;
const name = getChildrenByTagName(node, "NAME")[0];

obj["id"] = `#sec${chapterIndex}.${subsubsection_count}`;

addBodyToObj(
obj,
node,
`${chapterIndex}.${subsubsection_count}\u00A0\u00A0\u00A0` +
name.firstChild.nodeValue
);
obj["tag"] = "SECTION";

recursiveProcessTextJson(name.nextSibling, obj);
},

AMP: (node, obj) => {
addBodyToObj(obj, node, "&");
obj["tag"] = "#text";
Expand Down Expand Up @@ -127,42 +167,18 @@ const processTextFunctions = {
recursiveProcessTextJson(node.firstChild, obj);
},

ABOUT: (node, obj) => {
addBodyToObj(obj, node, displayTitle);

let childNode = node.firstChild;
while (childNode.nodeName != "NAME") {
childNode = childNode.nextSibling;
}

recursiveProcessTextJson(childNode.nextSibling, obj);
},

REFERENCES: (node, obj) => processTextFunctions["ABOUT"](node, obj),

REFERENCE: (node, obj) => {
addBodyToObj(obj, node, false);
recursiveProcessTextJson(node.firstChild, obj);
},

WEBPREFACE: (node, obj) => processTextFunctions["ABOUT"](node, obj),

MATTER: (node, obj) => processTextFunctions["ABOUT"](node, obj),

br: (node, obj) => {
addBodyToObj(obj, node, false);
obj["tag"] = "BR";
},

BR: (node, obj) => processTextFunctions["br"](node, obj),

CHAPTER: (node, obj) => {
addBodyToObj(obj, node, displayTitle);

const name = getChildrenByTagName(node, "NAME")[0];
recursiveProcessTextJson(name.nextSibling, obj);
},

EM_NO_INDEX: (node, obj) => {
node.nodeName = "EM";
processTextJson(node, obj);
Expand Down Expand Up @@ -242,8 +258,12 @@ const processTextFunctions = {
METAPHRASE: (node, obj) => {
const childObj = {};
recursiveProcessTextJson(node.firstChild, childObj);
const arr = childObj["child"].map(x => x["body"]);
let arr = [];
arr.push("\u3008"); //langle
arr = arr.concat(childObj["child"].map(x => x["body"]));
arr.push("\u3009"); //rangle
addArrayToObj(obj, node, arr);
obj["tag"] = "#text";
},

LINK: (node, obj) => {
Expand Down Expand Up @@ -299,13 +319,6 @@ const processTextFunctions = {
SCHEMEINLINE: (node, obj) =>
processTextFunctions["JAVASCRIPTINLINE"](node, obj),

SECTION: (node, obj) => {
addBodyToObj(obj, node, displayTitle);

const name = getChildrenByTagName(node, "NAME")[0];
recursiveProcessTextJson(name.nextSibling, obj);
},

JAVASCRIPTINLINE: (node, obj) => {
const writeTo = [];
if (ancestorHasTag(node, "NAME")) {
Expand Down Expand Up @@ -389,31 +402,6 @@ const processTextFunctions = {
recursiveProcessTextJson(node.firstChild, obj);
},

SUBSECTION: (node, obj) => {
addBodyToObj(obj, node, displayTitle);

const name = getChildrenByTagName(node, "NAME")[0];
recursiveProcessTextJson(name.nextSibling, obj);
},

// e.g. section 4.4.4.4
SUBSUBSECTION: (node, obj) => {
subsubsection_count += 1;
heading_count += 1;
const name = getChildrenByTagName(node, "NAME")[0];

obj["id"] = `#sec${chapterIndex}.${subsubsection_count}`;

addBodyToObj(
obj,
node,
`${chapterIndex}.${subsubsection_count}\u00A0\u00A0\u00A0` +
name.firstChild.nodeValue
);

recursiveProcessTextJson(name.nextSibling, obj);
},

SUBHEADING: (node, obj) => {
heading_count += 1;
addBodyToObj(obj, node, false);
Expand Down
2 changes: 1 addition & 1 deletion javascript/processingFunctions/processExerciseJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const processExerciseHtml = (node, obj) => {
const displayName = referenceStore[labelName].displayName;

obj["title"] = "Exercise " + displayName;
obj["id"] = `#ex_${displayName}`;
obj["id"] = `#ex-${displayName}`;

recursiveProcessTextJson(node.firstChild, obj);

Expand Down
2 changes: 1 addition & 1 deletion javascript/processingFunctions/processFigureJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const processFigureJson = (node, obj) => {
obj["scale"] = `${scale_percentage}%`;
}
obj["src"] = `${src}`;
obj["id"] = `#fig_${displayName}`;
obj["id"] = `#fig-${displayName}`;
}

const snippet = node.getElementsByTagName("SNIPPET")[0];
Expand Down
9 changes: 2 additions & 7 deletions javascript/processingFunctions/processReferenceJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ export const setupReferencesJson = (node, filename) => {
footnote.footnote_count = i + 1;
}

//console.log("setting up " + chapterIndex);
const labels = node.getElementsByTagName("LABEL");

for (let i = 0; labels[i]; i++) {
const label = labels[i];
const referenceName = label.getAttribute("NAME");
const ref_type = referenceName.split(":")[0];
//const ref_name = referenceName.split(":")[1];

if (ifIgnore(label)) {
continue;
}
//console.log(referenceName + " processed");

if (referenceStore[referenceName]) {
console.log(chapterIndex);
Expand All @@ -67,7 +64,6 @@ export const setupReferencesJson = (node, filename) => {
if (ancestorHasTag(label, "SUBSUBSECTION")) {
subsubsection_count++;
displayName = `${chapterIndex}.${subsubsection_count}`;
// href = `${chapterIndex}.html#sec${chapterIndex}.${subsubsection_count}`;
href = `/interactive-sicp/${chapterIndex}#subsubsection_${subsubsection_count}`;
} else {
displayName = chapterIndex;
Expand All @@ -76,7 +72,7 @@ export const setupReferencesJson = (node, filename) => {
} else if (ref_type == "fig") {
fig_count++;
displayName = `${chapter_number}.${fig_count}`;
href = `/interactive-sicp/${chapterIndex}#fig_${displayName}`;
href = `/interactive-sicp/${chapterIndex}#fig-${displayName}`;
} else if (ref_type == "foot") {
// Retrieve count from the parent node, setup before this loop
foot_count = label.parentNode.footnote_count;
Expand Down Expand Up @@ -125,8 +121,7 @@ export const setupReferencesJson = (node, filename) => {

ex_count++;
const displayName = `${chapter_number}.${ex_count}`;
const href = `/interactive-sicp/${chapterIndex}#ex_${displayName}`;
//console.log(referenceName + " added");
const href = `/interactive-sicp/${chapterIndex}#ex-${displayName}`;
referenceStore[referenceName] = { href, displayName, chapterIndex };
}
};
Expand Down