summaryrefslogtreecommitdiff
path: root/modules/unicode
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2020-05-21 11:48:51 +0200
committerTom Smeding <tom.smeding@gmail.com>2020-05-21 11:48:51 +0200
commit11d61f3c6f82bd36b365c75146d5c698abd40e8b (patch)
tree153347d77e71c5cebc5300a3bab2ab25bded170b /modules/unicode
parente6d6f325b463b0874f1c76c4450e0be71a9176bf (diff)
unicode: Parse direct codepoint numbers
Diffstat (limited to 'modules/unicode')
-rw-r--r--modules/unicode/index.html35
-rw-r--r--modules/unicode/unicode.js19
2 files changed, 48 insertions, 6 deletions
diff --git a/modules/unicode/index.html b/modules/unicode/index.html
index 1065575..1832fe8 100644
--- a/modules/unicode/index.html
+++ b/modules/unicode/index.html
@@ -15,6 +15,9 @@ body {
max-height: 300px;
overflow-y: scroll;
}
+.invisible {
+ display: none;
+}
</style>
<script>
function do_lookup(override) {
@@ -34,7 +37,7 @@ function do_lookup(override) {
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
- handleResponse(JSON.parse(xhr.responseText));
+ handleResponse(JSON.parse(xhr.responseText), input);
} else {
alert("Request failed: " + xhr.responseText);
}
@@ -47,13 +50,24 @@ function do_lookup(override) {
xhr.send();
}
-function handleResponse(json) {
+function handleResponse(json, input) {
var elem;
/* elem = document.getElementById("json");
elem.innerHTML = "";
elem.appendChild(document.createTextNode(JSON.stringify(json))); */
+ var index_container = document.getElementById("index_container");
+ if ("index" in json) {
+ index_container.classList.remove("invisible");
+ document.getElementById("index_input").innerHTML = input;
+ elem = document.getElementById("index");
+ elem.innerHTML = "";
+ elem.appendChild(makeCharacterDiv(json["index"]));
+ } else if (!index_container.classList.contains("invisible")) {
+ index_container.classList.add("invisible");
+ }
+
var keys = ["chars", "search"];
for (var i = 0; i < keys.length; i++) {
@@ -62,14 +76,18 @@ function handleResponse(json) {
elem = document.getElementById(keys[i]);
elem.innerHTML = "";
for (var j = 0; j < json[keys[i]].length; j++) {
- var div = document.createElement("div");
- div.classList.add("character");
- populateCharacter(div, json[keys[i]][j]);
- elem.appendChild(div);
+ elem.appendChild(makeCharacterDiv(json[keys[i]][j]));
}
}
}
+function makeCharacterDiv(row) {
+ var div = document.createElement("div");
+ div.classList.add("character");
+ populateCharacter(div, row);
+ return div;
+}
+
function populateCharacter(div, row) {
var span = document.createElement("span");
span.setAttribute("style", "display: inline-block; width: 7em; font-weight: bold;");
@@ -105,6 +123,11 @@ window.addEventListener("load", function() {
<!-- <pre id="json"></pre><br> -->
+ <div id="index_container" class="invisible">
+ <h3>Codepoint <span id="index_input"></span></h3>
+ <div id="index" class="table"></div>
+ </div>
+
<h3>Characters <span id="chars_num"></span></h3>
<div id="chars" class="table"></div>
diff --git a/modules/unicode/unicode.js b/modules/unicode/unicode.js
index d8ba227..a937796 100644
--- a/modules/unicode/unicode.js
+++ b/modules/unicode/unicode.js
@@ -82,6 +82,24 @@ function searchDescription(text) {
return result;
}
+function recogniseIndex(text) {
+ let m;
+
+ m = text.match(/^([0-9]+)$/); // 1234
+ if (m) return lookupCode(parseInt(m[1], 10));
+
+ m = text.match(/^(?:0[Xx]|[Uu]\+)([0-9a-fA-F]+)$/); // 0x34ab / U+34ab
+ if (m) return lookupCode(parseInt(m[1], 16));
+
+ m = text.match(/^&#([0-9]+);$/); // &#1234;
+ if (m) return lookupCode(parseInt(m[1], 10));
+
+ m = text.match(/^&#[Xx]([0-9a-fA-F]+);$/); // &#x34ab;
+ if (m) return lookupCode(parseInt(m[1], 16));
+
+ return null;
+}
+
module.exports = function (app, io, moddir) {
const dataFilePath = path.join(moddir, "UnicodeData.txt");
if (fs.existsSync(dataFilePath)) {
@@ -120,6 +138,7 @@ module.exports = function (app, io, moddir) {
}
res.json({
+ index: recogniseIndex(req.params.query) || undefined,
chars: chars,
search: searchDescription(req.params.query),
});