Skip to content

Commit 0f53ae6

Browse files
authored
Rewrite search (#926)
* added rewrited trie implementation, manipulation functions, and search data, maintianing funcitons and writing funciton * integrate rewriteSeatch to index and parseXmlJson * added testcase for the index search * debugged and fix the issue not not parsing index nodes within snippets * some cleaning * fixed all the errors indicated in the failedTest.txt * support urls to exercise * commented out testcode, deleted temp files * add test for corrrectness of urls, and run and passed the test * fix multiple bugs for text search * support order, open close, and simplify the frontend's parsing job
1 parent ca6ce37 commit 0f53ae6

File tree

5 files changed

+639
-2
lines changed

5 files changed

+639
-2
lines changed

javascript/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ import { setupSnippetsJs } from "./processingFunctions/processSnippetJs";
3636
import { getAnswers } from "./processingFunctions/processExercisePdf";
3737

3838
// json (for cadet frontend)
39+
import {testIndexSearch} from "./searchRewriteTest";
3940
import { parseXmlJson } from "./parseXmlJson";
41+
import {writeRewritedSearchData} from "./searchRewrite";
4042
import { setupSnippetsJson } from "./processingFunctions/processSnippetJson";
4143
import { createTocJson } from "./generateTocJson";
4244
import { setupReferencesJson } from "./processingFunctions/processReferenceJson";
@@ -360,7 +362,10 @@ async function main() {
360362
await recursiveXmlToHtmlInOrder("setupSnippet");
361363
console.log("setup snippets and references done\n");
362364

363-
recursiveXmlToHtmlInOrder("parseXml");
365+
await recursiveXmlToHtmlInOrder("parseXml");
366+
writeRewritedSearchData();
367+
// this is meant to be temp; also, will remove the original "generateSearchData" after the updation at the frontend is completed.
368+
//testIndexSearch();
364369
}
365370
}
366371

javascript/parseXmlJson.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ import {
1212
recursivelyProcessTextSnippetJson
1313
} from "./processingFunctions";
1414

15+
import {getIdForExerciseJson} from "./processingFunctions/processExerciseJson";
16+
1517
import {
1618
generateSearchData
1719
} from "./generateSearchData";
1820

21+
import {parseAndInsertToIndexTrie, parseAndInsertToIdToContentMap} from "./searchRewrite";
22+
1923
let paragraph_count = 0;
2024
let heading_count = 0;
2125
let footnote_count = 0;
@@ -120,6 +124,31 @@ const processLatex = (node, obj, inline) => {
120124
obj["body"] = math;
121125
obj["tag"] = "LATEX";
122126
};
127+
let latest_exercise_json_id = undefined;
128+
const tagsWithIds = {
129+
"#document": () => "",
130+
SUBSUBSECTION: () => subsubsection_count>0? `#sec${chapterIndex}.${subsubsection_count}` :"",
131+
TEXT:() => "#p" + paragraph_count,
132+
SUBHEADING: () => `#h${heading_count}`,
133+
SUBSUBHEADING: () => `#h${heading_count}`,
134+
SECTION: () => `#h${heading_count}`,
135+
FOOTNOTE: () => `#footnote-link-${footnote_count}`,
136+
DISPLAYFOOTNOTE: () => `#footnote-${display_footnote_count}`,
137+
//SNIPPET: () => `${snippet_count}`,
138+
139+
EXERCISE: () => latest_exercise_json_id,
140+
DISPLAYFOOTNOTE: () => `#footnote-${display_footnote_count}`,
141+
};
142+
const findParentID = (node) => {
143+
let parent = node.parentNode;
144+
while (parent) {
145+
if(tagsWithIds[parent.nodeName]) {
146+
return `${chapterIndex}` + tagsWithIds[parent.nodeName]();
147+
} else {
148+
parent = parent.parentNode;
149+
}
150+
}
151+
}
123152

124153
const processTextFunctions = {
125154
// Text tags: tag that is parsed as text
@@ -132,6 +161,10 @@ const processTextFunctions = {
132161
}
133162
}
134163
},
164+
INDEX: (node, obj) => {
165+
const id = findParentID(node);
166+
parseAndInsertToIndexTrie(node, {id});
167+
},
135168

136169
AMP: (_node, obj) => {
137170
processText("&", obj);
@@ -181,6 +214,7 @@ const processTextFunctions = {
181214
},
182215

183216
EXERCISE: (node, obj) => {
217+
latest_exercise_json_id = getIdForExerciseJson(node);
184218
exercise_count += 1;
185219
processExerciseJson(node, obj, chapArrIndex, exercise_count);
186220
},
@@ -310,6 +344,8 @@ const processTextFunctions = {
310344
},
311345

312346
SNIPPET: (node, obj) => {
347+
const indexNodes = node.getElementsByTagName("INDEX");
348+
313349
if (node.getAttribute("HIDE") == "yes") {
314350
return;
315351
} else if (node.getAttribute("LATEX") == "yes") {
@@ -349,6 +385,10 @@ const processTextFunctions = {
349385
obj["body"] = obj["body"].replace(matchStr, newStr);
350386
}
351387
}
388+
389+
for (let i = 0; i < indexNodes.length; i++) {
390+
processTextJson(indexNodes[i], {});
391+
}
352392

353393
return;
354394
}
@@ -358,6 +398,9 @@ const processTextFunctions = {
358398
obj["latex"] = false;
359399
obj["id"] = snippet_count;
360400
processSnippetJson(node, obj);
401+
for (let i = 0; i < indexNodes.length; i++) {
402+
processTextJson(indexNodes[i], {});
403+
}
361404
},
362405

363406
SUBINDEX: (node, obj) => {
@@ -520,7 +563,8 @@ export const parseXmlJson = (doc, arr, filename) => {
520563
} else {
521564
displayTitle = chapterIndex + "\u00A0\u00A0" + chapterTitle;
522565
}
523-
566+
567+
latest_exercise_json_id = undefined;
524568
paragraph_count = 0;
525569
footnote_count = 0;
526570
display_footnote_count = 0;
@@ -571,6 +615,7 @@ export const parseXmlJson = (doc, arr, filename) => {
571615
recursiveProcessTextJson(name.nextSibling, arr, title);
572616
}
573617

618+
parseAndInsertToIdToContentMap(arr,chapterIndex);
574619
generateSearchData(doc, filename);
575620

576621
};

javascript/processingFunctions/processExerciseJson.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,23 @@ const processExerciseJson = (node, obj) => {
3535
}
3636
};
3737

38+
export const getIdForExerciseJson = (node) => {
39+
const label = node.getElementsByTagName("LABEL")[0];
40+
let labelName = "";
41+
42+
if (!label) {
43+
labelName = "ex:unlabeled" + unlabeledEx;
44+
} else {
45+
labelName = label.getAttribute("NAME");
46+
}
47+
48+
if (!referenceStore[labelName]) {
49+
missingExerciseWarning(labelName);
50+
return undefined;
51+
}
52+
53+
const displayName = referenceStore[labelName].displayName;
54+
return `#ex-${displayName}`;
55+
}
56+
3857
export default processExerciseJson;

0 commit comments

Comments
 (0)