-
Notifications
You must be signed in to change notification settings - Fork 0
/
furigana.js
41 lines (30 loc) · 1.22 KB
/
furigana.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
document.addEventListener("DOMContentLoaded", () => {
const input = document.getElementById("input").innerHTML;
const output = document.getElementById("output");
function generateRuby(input) {
let result = "";
const sentStart = input.indexOf("<sent>");
const sentEnd = input.indexOf("</sent>");
if (sentStart === -1 || sentEnd === -1) {
return "Error: Invalid input format";
}
const content = input.substring(sentStart + 6, sentEnd);
const parts = content.split("<nota></nota>");
if (parts.length !== 2) {
return "Error: Invalid input format";
}
const chinesePart = parts[0].split("&");
const pinyinPart = parts[1].split("&");
if (chinesePart.length !== pinyinPart.length) {
return "Error: Mismatched Chinese and Pinyin parts";
}
for (let i = 0; i < chinesePart.length; i++) {
if (chinesePart[i]) {
result += `<ruby>${chinesePart[i]}<rt>${pinyinPart[i]}</rt></ruby>`;
}
}
return result;
}
const rubyContent = generateRuby(input);
output.innerHTML = rubyContent;
});