-
Notifications
You must be signed in to change notification settings - Fork 1
/
genhtmlentity.ts
75 lines (59 loc) · 1.63 KB
/
genhtmlentity.ts
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
// @ts-ignore
import * as fs from "node:fs/promises";
interface EntityDefinition {
characters: string;
}
const entities: Record<string, EntityDefinition> =
await fetch("https://html.spec.whatwg.org/entities.json").then(r => r.json());
const cleaned = Object.fromEntries(
Object.entries(entities)
.map(([k, v]) => [k.replace(/^&(.*?);?$/g, "$1"), v])
);
function strcmp(a: string, b: string): number {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}
const encoder = new TextEncoder();
const parts: string[] = [];
parts.push(`
struct htmlEntity {
const char *pzName;
const char *pzUtf8;
};
typedef struct htmlEntity htmlEntity;
static const htmlEntity htmlEntities[] = {
`.trim());
for (const [name, { characters }] of Object.entries(cleaned).sort((a, b) => strcmp(a[0], b[0]))) {
const hex = encoder.encode(characters);
const escaped = Array.from(hex).map((h) => "\\x" + h.toString(16).padStart(2, "0")).join("");
parts.push(`\t{"${name}", "${escaped}"},`);
}
parts.push(`
{0, 0}
};
#define MAX_ENTITY_NAME_LENGTH ${Math.max(...Object.keys(cleaned).map((k) => k.length))}
#define NUM_ENTITIES ${Object.keys(cleaned).length}
`);
const input = await fs.readFile("fts5html.c", "utf-8");
let emit = true;
const outputParts: string[] = [];
for (let line of input.split("\n")) {
if (line.includes("/* START OF HTML ENTITIES */")) {
outputParts.push(line);
emit = false;
}
if (emit) {
outputParts.push(line);
}
if (line.includes("/* END OF HTML ENTITIES */")) {
emit = true;
outputParts.push(parts.join("\n"));
outputParts.push(line);
}
}
await fs.writeFile("fts5html.c", outputParts.join("\n"));