-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer.html
93 lines (76 loc) · 2.36 KB
/
answer.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
<div class="hidden">
<span id="kanji" class="hidden">{{Kanji}}</span>
<ruby>
<rb>{{Kanji}}</rb><rp>《</rp>
<rt id="hiragana">{{Hiragana}}</rt><rp>》</rp>
</ruby>
</div>
<div id="output"></div>
<div class="english">
{{Translation}}
<br>
<a class="small">{{Hiragana}}</a>
<span id="debug" class="hidden"></span>
</div>
<script>
//variables to work with
var kanji = document.getElementById("kanji").innerHTML;
var hiragana = document.getElementById("hiragana").innerHTML;
var positions = [];
var lastpos = 0; //ignore this thing, I just need global variable
//structure lookup
//no furigana -> returns hiragana version position
//kanji -> returns -1
for (var i = 0; i < kanji.length; i++){
positions.push( hiragana.search(kanji.charAt(i)) );
//console.log("Search result for " + kanji.charAt(i) + " is " + hiragana.search(kanji.charAt(i)));
}
//console.log("Looking up completed with these results " + positions);
//ruby generating
var output = "<ruby>";
for(var i = 0; i < positions.length; i++){
//returns character without furigana, done pretty shitty way
if (positions[i] != -1){
output += "<rb>" + kanji.charAt(i) + "</rb><rt></rt>";
lastpos = i;
}
//returns kanji with furigana generated, done even more shitty way, really shitty way, please don't laugh at me
else{
output += "<rb>" + getkanjiwithhiragana(i) + "</rb><rp>《</rp><rt>" + gethiraganaoverkanji(i) + "</rt><rp>》</rp>";
i+= kanjilength(i) - 1;
}
}
//output return
output += "</ruby>";
//console.log(output);
document.getElementById("output").innerHTML = output;
//i need all the hiragana over specific kanji set
function gethiraganaoverkanji(position){
var allthehiraganaiwant = "";
var cyclesofforloop = Math.abs(positions[kanjilength(position)]-position); //i don't know why but it works with absolute value
if(isNaN(cyclesofforloop)){
return hiragana;
}
for(var i = 0; i < cyclesofforloop;i++){
allthehiraganaiwant+=hiragana.charAt(i+position);
}
return allthehiraganaiwant;
}
//i need all the kanji with furigana
function getkanjiwithhiragana(position){
var allthekanjiiwant = "";
for(var i = 0; i < kanjilength(position); i++){
allthekanjiiwant += kanji.charAt(i+position)
}
return allthekanjiiwant;
}
//gets kanji set length
//nice recursive function
function kanjilength(position){
if(positions[position] == -1){
return 1 + kanjilength(position+1);
}else{
return 0;
}
}
</script>