forked from source-academy/sicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindBadTags.js
229 lines (204 loc) · 4.32 KB
/
findBadTags.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import fs from "fs";
import util from "util";
import path from "path";
import { DOMParser as dom } from "xmldom";
const readdir = util.promisify(fs.readdir);
const open = util.promisify(fs.open);
const readFile = util.promisify(fs.readFile);
const inputDir = path.join(__dirname, "../xml");
const validTags = new Set([
// symbols
"APOS",
"SPACE",
"WJ",
"AACUTE_LOWER",
"AACUTE_UPPER",
"AGRAVE_LOWER",
"AGRAVE_UPPER",
"ACIRC_LOWER",
"EACUTE_LOWER",
"EACUTE_UPPER",
"EGRAVE_LOWER",
"EGRAVE_UPPER",
"ECIRC_LOWER",
"OUML_LOWER",
"OUML_UPPER",
"UUML_LOWER",
"UUML_UPPER",
"CCEDIL_LOWER",
"ELLIPSIS",
"AMP",
"DOLLAR",
"SECT",
"EMDASH",
"ENDASH",
"LaTeX",
"TeX",
"BREAK",
// tagsToRemove (skip over when found)
"#comment",
"COMMENT",
"WEB_ONLY",
"PDF_ONLY",
"EDIT",
"EXCLUDE",
"HISTORY",
"NAME",
"EXPECTED",
"JAVASCRIPT_RUN",
"JAVASCRIPT_TEST",
"JAVASCRIPT_OUTPUT",
"ORDER",
"SCHEME",
"SOLUTION",
// ignoreTags (treat tag as meaningless wrapper)
"CHAPTERCONTENT",
"JAVASCRIPT",
"NOBR",
"SECTIONCONTENT",
"span",
"SPLIT",
"SPLITINLINE",
// part of processTextFunctionsDefaultLatex
"#text",
"ABOUT",
"REFERENCES",
"WEBPREFACE",
"MATTER",
"B",
"BR",
"BLOCKQUOTE",
"CHAPTER",
"CITATION",
"EM",
"em", // maybe remove this?
"EPIGRAPH",
"EXERCISE",
"FIGURE",
"FOOTNOTE",
"H2",
"INDEX",
"IMAGE",
"LABEL",
"LINK",
"LATEX",
"TREETAB",
"LATEXINLINE",
"MATTERSECTION",
"OL",
"P",
"TEXT",
"QUOTE",
"REF",
"REFERENCE",
"SC",
"SECTION",
"SUBSUBSECTION",
"SCHEMEINLINE",
"JAVASCRIPTINLINE",
"SNIPPET",
"SUBHEADING",
"SUBINDEX",
"SUBSECTION",
"TABLE",
"TT",
"UL",
// Didn't check what these are for
"SUBSUBSUBSECTION",
"SCHEMEOUTPUT",
"CODEINDEX",
// Processed seperately
"LI",
"REQUIRES",
"EXAMPLE",
// Epigraph
"ATTRIBUTION",
"AUTHOR",
"TITLE",
"DATE",
// image
"CAPTION",
// Table
"TD",
"TR",
"tr",
"td",
"kbd"
]);
const recursiveCheckChildrenForTagName = (node, tagName) => {
let child = node.firstChild;
let found = false;
while (child) {
if (child.nodeName === tagName) {
console.log("Found invalid tag: " + tagName + ".");
found = true;
}
if (recursiveCheckChildrenForTagName(child, tagName)) {
found = true;
}
child = child.nextSibling;
}
return found;
};
const recursiveCheckChildrenForValidTag = node => {
let child = node.firstChild;
let found = false;
while (child) {
if (!validTags.has(child.nodeName)) {
console.log(child.nodeName + " found.");
found = true;
}
if (recursiveCheckChildrenForValidTag(child)) {
found = true;
}
child = child.nextSibling;
}
return found;
};
async function recursiveCheckXmlForTag(filepath, tagName) {
let files;
const fullPath = path.join(inputDir, filepath);
files = await readdir(fullPath);
const promises = [];
files.forEach(file => {
if (file.match(/\.xml$/)) {
promises.push(checkXmlForTag(filepath, file, tagName));
} else if (fs.lstatSync(path.join(fullPath, file)).isDirectory()) {
promises.push(
recursiveCheckXmlForTag(path.join(filepath, file), tagName)
);
}
});
await Promise.all(promises);
}
async function checkXmlForTag(filepath, filename, tagName) {
const fullFilepath = path.join(inputDir, filepath, filename);
const fileToRead = await open(fullFilepath, "r");
const data = await readFile(fileToRead, { encoding: "utf-8" });
const doc = new dom().parseFromString(data);
let found = false;
if (tagName == "") {
found = recursiveCheckChildrenForValidTag(doc.documentElement);
} else {
found = recursiveCheckChildrenForTagName(doc.documentElement, tagName);
}
if (found) {
console.log("Found in " + filepath + "\n");
}
}
async function main() {
const args = process.argv;
let tagName = "";
if (args.length > 2 && args[2] != "") {
tagName = args[2];
console.log("\nLooking for tag: " + tagName + "\n");
} else {
console.log("\nLooking for all invalid tags.\n");
}
recursiveCheckXmlForTag("", tagName);
}
// babel-node ./javascript/findBadTags <tagname>
// Can provide 0 or 1 arguments.
// If no arguments provided, checks for all invalid tags.
// If argument provided, checks for all instances of the provided tag.
main();