-
Notifications
You must be signed in to change notification settings - Fork 2
/
boka.html
127 lines (118 loc) · 2.48 KB
/
boka.html
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
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="res/favicon.ico">
<script>
const kPre = [
["\n", "\n"],
[/\s/, " "],
];
const kWord = [
["ai", "I"],
["en", "and"],
["agen", "again"],
["nau", "now"],
];
const kLetter = [
["M", "u"],
["dZ", "j"],
["dZ", "th"],
["4", "r"],
["4", "l"],
["C", "h"],
["p\\", "f"],
["S", "sh"],
["S", "ss"],
["b", "b"],
["b", "v"],
];
function walk(a, f, q) {
if (!Array.isArray(a)) return f(a);
console.assert(a.length > 0);
let n = a.length - 1;
let s = a[n];
if (n == 0) ++n;
for (let i = 0; i < n; ++i) {
if (q && i) f(s);
a[i] = walk(a[i], f, q);
}
return a;
}
function flatten(a) {
let b = [];
walk(a, function(x) { b.push(x); return x; }, true);
return b;
}
function apply(k, v, a) {
walk(a, function(x) {
let b = x.split(k);
if (b.length == 1) return x;
b.push(v);
return b;
});
}
function code(msg, map) {
let a = [msg];
for (let i = 0; i < map.length; ++i) apply(map[i][0], map[i][1], a);
return flatten(a);
}
function build(f) {
let m = [];
for (let x of kPre) m.push(x);
for (let x of kWord) {
let y = f(x);
m.push([new RegExp("^" + y[0] + "$"), y[1]]);
}
for (let x of kLetter) m.push(f(x));
return m;
}
function fill(out, values, f) {
if (out.firstChild) out.removeChild(out.firstChild);
let wrap = document.createElement("div");
for (let i = 0; i < values.length; ++i) {
let chunk = document.createElement("span");
chunk.classList.add(i % 2 ? "known" : "unknown");
chunk.innerText = values[i];
wrap.appendChild(chunk);
}
out.appendChild(wrap);
}
function setup() {
for (let k of [["d", build(x => x)], ["e", build(x => [x[1], x[0]])]]) {
let i = document.getElementById("in" + k[0]);
let o = document.getElementById("out" + k[0]);
let f = function() { fill(o, code(i.value, k[1])); };
i.addEventListener("change", f);
i.addEventListener("keyup", f);
i.addEventListener("paste", f);
f();
}
}
window.addEventListener("load", setup);
</script>
<style>
body {display: flex}
body div {flex-grow: 1}
body .pad {flex-grow: 0.1}
h1 {text-align: center;}
textarea {width: 100%}
.known {background-color: #09F8}
.unknown {background-color: #F308}
</style>
</head>
<body>
<div>
<h1>Encode</h1>
<textarea id="ine" rows="10"></textarea>
<p></p>
<div id="oute"></div>
</div>
<div class="pad"></div>
<div>
<h1>Decode</h1>
<textarea id="ind" rows="10"></textarea>
<p></p>
<div id="outd"></div>
</div>
</body>
</html>