-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.js
117 lines (117 loc) · 4.21 KB
/
helper.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
const id = (x) => document.getElementById(x);
function createHTMLElement(tag, props, children) {
const element = document.createElement(tag);
Object.assign(element, props);
for (const child of children) {
if (child) {
element.append(child);
}
}
return element;
}
function jvoptionsUrl() {
var vars = "";
var options = [];
if (config["lujvo.cmevla"]) options.push("cme");
if (options.length) vars += "&lujvo=" + options.join(",");
return vars;
}
function isLujvo(s) {
try {
jvokaha(s);
return true;
} catch (e) {
return false;
}
}
function convertJSONToHTMLElement(json) {
const r = RAFSI.get(json.word) ?? [];
const entry = createHTMLElement("div", {"className": "entry"}, [
createHTMLElement("p", null, [
createHTMLElement("a", {
"href": "?q=" + json.word + jvoptionsUrl(),
"className": "w"
}, [
createHTMLElement("b", null, [json.word])
]),
" ",
r.length || isLujvo(json.word) ? createHTMLElement("i", {"className": "rafsi"}, [
r.join(" ") || [
createHTMLElement("span", {}, ["→ "]),
createHTMLElement("a", {
"href": "?q=" + encodeURIComponent(getVeljvo(json.word).join(" ")) + jvoptionsUrl()
}, [getVeljvo(json.word).join(" ")])
]].flat()) : null,
" ",
json.selmaho ? createHTMLElement("a", {"href": "?q=" + encodeURIComponent(json.selmaho) + jvoptionsUrl()}, [
createHTMLElement("code", {"className": "selmaho"}, [json.selmaho])
]) : null,
" ",
createHTMLElement("a", {
"href": "https://jbovlaste.lojban.org/dict/" + json.word.replace(/ /g, "%20"),
"target": "_blank",
}, [
json.score >= 1000 ? "official" :
json.score == -1 ? json.score : "+" + json.score,
"\u{a0}↗"
])
]),
createHTMLElement("p", null, replaceLinks(json.definition).els),
json.notes ? createHTMLElement("details", null, [
createHTMLElement("summary", null, [
"more info",
/ /.test(replaceLinks(json.notes).text) ? createHTMLElement("span", null, [
" • ",
createHTMLElement("a", {
"href": "?q=" + encodeURIComponent(replaceLinks(json.notes).text) + jvoptionsUrl()
}, ["open all links"])
]) : null
]),
createHTMLElement("p", null, replaceLinks(json.notes).els)
]) : null
]);
return entry;
}
function replaceLinks(str) {
var bits = str.replace(/\$/g, "💵$").split("💵");
var text = "";
for (var i = 0; i < bits.length; i++) {
if (i % 2 == 0 || i == bits.length - 1) {
if (i) {
bits[i] = bits[i].slice(1);
bits[i - 1] = bits[i - 1] + "$";
}
bits[i] = bits[i].replace(/\{/g, "📦{").replace(/\}/g, "}📦").split("📦").map((item) => {
if (/\{[a-g'i-pr-vx-z., ]+\}/i.test(item)) {
text += " " + item.slice(1, -1);
return createHTMLElement("a", {
"href": "?q=" + item.slice(1, -1) + jvoptionsUrl(),
}, item.slice(1, -1))
} else {
return item;
}
});
}
}
return {"els": bits.flat(), "text": text.trim()};
}
function load(res, page) {
const start = page * 100;
const end = (page + 1) * 100;
var nodes = [];
for (var i = start; i < end; i++) {
if (res[i]) {
nodes.push(convertJSONToHTMLElement(res[i][0]));
}
}
id("results").append(...nodes);
// latex
renderMathInElement(document.body, {
"delimiters": [
{"left": "$$", "right": "$$", "display": true },
{"left": "$", "right": "$", "display": false},
{"left": "\\(", "right": "\\)", "display": false},
{"left": "\\[", "right": "\\]", "display": true }
]
});
}