-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.html.tail
165 lines (147 loc) · 5.34 KB
/
template.html.tail
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
" />
<script type="text/javascript">
let data = dictionary.content;
const MAX_MATCHES = 1000;
function search(searchString, results) {
let matches = findMatches(searchString);
let listItems = [];
let previousWordIndex = null;
for (let match of matches) {
let wordIndex = match[0];
if (wordIndex == previousWordIndex) {
continue;
}
previousWordIndex = wordIndex;
let matchEntry = getDictionaryEntry(wordIndex);
let listItem = document.createElement("li");
let wordItem = document.createElement("b");
wordItem.append(matchEntry[0]);
listItem.append(wordItem);
for (let definitionLine of matchEntry[1].split("\n")) {
let definitionPara = document.createElement("p");
definitionPara.append(definitionLine);
listItem.append(definitionPara);
}
listItems.push(listItem);
if (listItems.length >= MAX_MATCHES) {
break;
}
}
results.replaceChildren(...listItems);
return false;
}
function findMatches(searchString) {
searchString = searchString.normalize("NFKD");
let low = findLow(searchString);
let high = findHigh(searchString);
let matches = []
for (let i = low; i < high; i++) {
matches.push(getSuffixArrayEntry(i));
}
matches.sort((a, b) => (a[1] - b[1]) || (a[0] - b[0]));
return matches;
}
function findLow(searchString) {
let low = 0;
let high = getSuffixArrayLen();
while (low < high) {
let mid = Math.floor((high - low)/2) + low
if (getSuffix(mid) < searchString) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
function findHigh(searchString) {
let low = 0;
let high = getSuffixArrayLen();
while (low < high) {
let mid = Math.floor((high - low)/2) + low
if (getSuffix(mid).slice(0, searchString.length) <= searchString) {
low = mid + 1;
} else {
high = mid;
}
}
return high;
}
function getSuffix(index) {
let wordSuffixIndex = getSuffixArrayEntry(index);
let wordIndex = wordSuffixIndex[0];
let suffixIndex = wordSuffixIndex[1];
return getSearchString(wordIndex).slice(suffixIndex);
}
function getDictionaryLen() {
return unpackDouble(0);
}
function getPackedDictionaryLen() {
return unpackDouble(2);
}
function getPackedSearchStringsLen() {
return unpackDouble(4);
}
function getSuffixArrayLen() {
return unpackDouble(6);
}
const HEADER_SIZE = 8;
function getDictionaryEntry(index) {
let dictionaryIndexStart = HEADER_SIZE;
let dictionaryStart = HEADER_SIZE + 4 * (getDictionaryLen() + 1);
let entryStart = dictionaryStart + unpackDouble(dictionaryIndexStart + 2 * index);
let entryEnd = dictionaryStart + unpackDouble(dictionaryIndexStart + 2 * index + 2);
let wordLen = unpackSingle(entryStart);
let definitionStart = entryStart + 1 + wordLen;
let word = data.slice(entryStart + 1, definitionStart);
let definition = data.slice(definitionStart, entryEnd);
return [word, definition];
}
function getSearchString(index) {
let searchStringsIndexStart = HEADER_SIZE + 2 * (getDictionaryLen() + 1);
let searchStringsStart = HEADER_SIZE + 4 * (getDictionaryLen() + 1) + getPackedDictionaryLen();
let stringStart = searchStringsStart + unpackDouble(searchStringsIndexStart + 2 * index);
let stringEnd = searchStringsStart + unpackDouble(searchStringsIndexStart + 2 * (index+1));
return data.slice(stringStart, stringEnd);
}
function getSuffixArrayEntry(index) {
let suffixArrayStart = HEADER_SIZE + 4 * (getDictionaryLen() + 1) + getPackedDictionaryLen() + getPackedSearchStringsLen();
let entryStart = suffixArrayStart + 3 * index;
let wordIndex = unpackDouble(entryStart);
let suffixIndex = unpackSingle(entryStart + 2);
return [wordIndex, suffixIndex];
}
const SINGLE_UTF16_LOWER_LIMIT = 0x20;
const SINGLE_UTF16_UPPER_LIMIT = 0xd800;
const SINGLE_UTF16_RANGE = SINGLE_UTF16_UPPER_LIMIT - SINGLE_UTF16_LOWER_LIMIT;
function unpackSingle(index) {
return data.charCodeAt(index) - SINGLE_UTF16_LOWER_LIMIT;
}
function unpackDouble(index) {
return unpackSingle(index) * SINGLE_UTF16_RANGE + unpackSingle(index+1);
}
</script>
<style type="text/css">
body {
max-width: 40em;
margin: auto;
width: fit-content;
word-break: break-word;
}
body > * {
display: block;
margin: auto;
width: fit-content;
}
</style>
</head>
<body>
<a href="" download="dic.html" lang="en">Download for offline use!</a>
<form onsubmit="return search(searchbox.value, results)">
<input id="searchbox" type="search" oninput="if(searchbox.value) { search(searchbox.value, results) }">
<button type="submit">🔍</button>
</form>
<ul id="results">
</ul>
</body>
</html>