Web/main/actaddmanual/index.js

68 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2020-02-23 17:13:23 +01:00
const replacements = new Map();
replacements.set(/<imp>([^<]*)<r>/g, function (content) {
2021-03-19 12:02:31 +01:00
return `<span class="imp">${content}</span>`;
2020-02-23 17:13:23 +01:00
});
replacements.set(/<item>([^<]*)<r>/g, function (content) {
2021-03-19 12:02:31 +01:00
return `<span class="item">${content}</span>`;
2020-02-23 17:13:23 +01:00
});
replacements.set(/<n>/g, function (_content) {
2021-03-19 12:02:31 +01:00
return "<br>";
2020-02-23 17:13:23 +01:00
});
replacements.set(/<i>([^<]*)<r>/g, function (content) {
2021-03-19 12:02:31 +01:00
return `<em>${content}</em>`;
2020-02-23 17:13:23 +01:00
});
replacements.set(/<tifisgrin>([^<]*)<r>/g, function (content) {
2021-03-19 12:02:31 +01:00
return `<span class="tifisgrin">${content}</span>`;
2020-02-23 17:13:23 +01:00
});
$.ajax({
2021-04-01 16:38:45 +02:00
url: "https://cdn.jsdelivr.net/gh/Ellpeck/ActuallyAdditions@1.12.2/src/main/resources/assets/actuallyadditions/lang/en_US.lang",
2020-02-23 17:13:23 +01:00
cache: false,
success: populateManual
});
function populateManual(lang) {
let startIndex = lang.indexOf("#Booklet Chapters");
let data = lang.substring(startIndex + 18);
let entries = $("#entries");
2021-11-08 18:38:15 +01:00
let sidebar = $("#sidebar-content");
2020-02-23 17:13:23 +01:00
for (let topic of data.split("\n\n")) {
let lines = topic.split("\n");
2023-08-07 14:04:06 +02:00
let t = "<div class=\"entry rounded\">";
2020-02-23 17:13:23 +01:00
// first line is name
let name = lines[0].substring(lines[0].indexOf("=") + 1);
2023-08-07 14:04:06 +02:00
let id = lines[0].match(/chapter\.([^.]*)\.name/)[1];
2020-02-23 17:13:23 +01:00
sidebar.append(`<a href=#${id}>${name}</a>`);
2023-02-12 12:18:45 +01:00
t += `<h1 id="${id}">${name}</h1>`;
2020-02-23 17:13:23 +01:00
// following lines are content
for (let i = 1; i < lines.length; i++) {
let text = lines[i].substring(lines[i].indexOf("=") + 1);
t += `<p>${replaceFormatting(text)}</p>`;
}
t += "</div>";
entries.append(t);
}
2023-06-13 17:36:14 +02:00
// force-scroll to proper anchor
if (location.hash.startsWith("#")) {
let element = $(location.hash);
2023-06-13 17:29:52 +02:00
if (element.length) {
2023-08-07 14:04:06 +02:00
$("html, body").animate({
2023-06-13 17:29:52 +02:00
scrollTop: element.offset().top
}, 0);
}
}
2020-02-23 17:13:23 +01:00
}
function replaceFormatting(text) {
for (let [k, v] of replacements.entries()) {
2021-03-19 12:02:31 +01:00
const finalV = v;
2020-02-23 17:13:23 +01:00
text = text.replace(k, function (_substring, group) {
2021-03-19 12:02:31 +01:00
return finalV(group);
2020-02-23 17:13:23 +01:00
});
}
return text;
2023-02-12 12:18:45 +01:00
}