aboutsummaryrefslogtreecommitdiff
path: root/anki-card-template/card.js
diff options
context:
space:
mode:
Diffstat (limited to 'anki-card-template/card.js')
-rw-r--r--anki-card-template/card.js67
1 files changed, 39 insertions, 28 deletions
diff --git a/anki-card-template/card.js b/anki-card-template/card.js
index d86ac87..03cd1ae 100644
--- a/anki-card-template/card.js
+++ b/anki-card-template/card.js
@@ -1,5 +1,8 @@
-function parseSentence(input) {
+HTMLElement.prototype.parse = function() {
+ if (this.classList.contains("parsed")) return; // ignore already parsed elements
+ var input = this.innerHTML;
var bold = false; // currently bold
+ var italic = false; // currently italic
var mode = "normal"; // normal, kanji, reading
var out = ""; // output html
@@ -8,30 +11,43 @@ function parseSentence(input) {
var reading = ""; // current kanji reading
for (var i = 0; i < input.length; i++) {
- // escape characters preceded by \
- if (input[i] == "\\") {
- var escaped = input[i+1];
- if (escaped == "n") escaped = "<br>"; // newline
- out += escaped; i++; continue;
+ if (this.classList.contains("parse-format")) {
+ // escape characters preceded by \
+ if (input[i] == "\\") {
+ var escaped = input[i+1];
+ if (escaped == "n") escaped = "<br>"; // newline
+ if (escaped == "t") escaped = "\t"; // tab
+ out += escaped; i++; continue;
+ }
+ // parse *test* into <b>test</b>
+ if (input[i] == "*") { bold = !bold; out += `<${bold ? "" : "/"}b>`; continue; }
+ // parse _test_ into <i>test</i>
+ if (input[i] == "_") { italic = !italic; out += `<${italic ? "" : "/"}i>`; continue; }
}
- // parse *test* into <b>test</b>
- if (input[i] == "*") { bold = !bold; out += `<${bold ? "" : "/"}b>`; continue; }
- // parse [kanji](reading) into ruby text
- // [kanji](reading) is only visible on card back
- // {kanji}(reading) is always visible
- if (mode == "normal" && input[i] == "[") // hidden reading kanji open
+ if (this.classList.contains("parse-furigana")) {
+ // parse [kanji](reading) into ruby text
+ // [kanji](reading) is only visible on card back
+ // {kanji}(reading) is always visible
+ if (mode == "normal" && input[i] == "[") // hidden reading kanji open
{ kanji = ""; mode = "kanji"; alwaysvisisble = false; continue; }
- if (mode == "normal" && input[i] == "{") // always visible reading kanji open
+ if (mode == "normal" && input[i] == "{") // always visible reading kanji open
{ kanji = ""; mode = "kanji"; alwaysvisisble = true; continue; }
- if (mode == "kanji" && input[i] == "]") continue; // hidden reading kanji close
- if (mode == "kanji" && input[i] == "}") continue; // always visible reading kanji close
- if (mode == "kanji" && kanji.length > 0 && input[i] == "(") // reading open
+ if (mode == "kanji" && input[i] == "]") continue; // hidden reading kanji close
+ if (mode == "kanji" && input[i] == "}") continue; // always visible reading kanji close
+ if (mode == "kanji" && kanji.length > 0 && input[i] == "(") // reading open
{ reading = ""; mode = "reading"; continue; }
- if (mode == "reading" && input[i] == ")") { // reading close
- mode = "normal";
- out += `<ruby>${kanji}<rt class="${alwaysvisisble ? 'visible' : 'hidden'}">${reading}</rt></ruby>`;
- continue;
+ if (mode == "reading" && input[i] == ")") { // reading close
+ mode = "normal";
+ out += `<ruby>${kanji}<rt class="${alwaysvisisble ? 'visible' : 'hidden'}">${reading}</rt></ruby>`;
+ continue;
+ }
+ }
+
+ if (this.classList.contains("parse-brackets")) {
+ if (i == 0) { out += `<span class="kanji">` }
+ if (input[i] == '\u3010') { out += `</span><span class="reading">${input[i]}`; continue; }
+ if (input[i] == '\u3011') { out += `${input[i]}</span>`; continue; }
}
// add current character to selected mode buffer
@@ -40,7 +56,8 @@ function parseSentence(input) {
if (mode == "reading") reading += input[i];
}
- return out;
+ this.innerHTML = out;
+ this.classList.add("parsed");
}
function layout() {
@@ -56,13 +73,7 @@ function layout() {
}
function run() {
- // parse all elements with class parse
- for (var el of document.getElementsByClassName("parse")) {
- if (el.classList.contains("parsed")) continue; // ignore already parsed elements
- el.innerHTML = parseSentence(el.innerHTML); // parse
- el.classList.remove("parse"); // mark as parsed
- el.classList.add("parsed");
- }
+ for (var el of document.getElementsByClassName("parse")) el.parse();
// toggle spoiler by clicking
for (var el of document.getElementsByClassName("spoiler")) {