From 0eccd89f147f3ec94d38a4ddb8e20ce1016ae570 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 10 Jul 2016 18:16:51 -0700 Subject: [PATCH] Support additional replacement fields --- ext/bg/js/yomichan.js | 60 +++++++++++++++++++++++++++---------------- ext/bg/options.html | 4 +-- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 19c66186..cd4bd4c1 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -153,35 +153,51 @@ class Yomichan { } formatField(field, definition, mode) { - const supported = ['character', 'expression', 'glossary', 'kunyomi', 'onyomi', 'reading']; + const tags = [ + 'character', + 'expression', + 'glossary', + 'glossary-list', + 'kunyomi', + 'onyomi', + 'reading', + 'tags', + ]; - for (let key in definition) { - if (supported.indexOf(key) === -1) { - continue; + for (let tag of tags) { + let value = definition[tag] || null; + switch (tag) { + case 'expression': + if (mode === 'vocab_kana' && definition.reading) { + value = definition.reading; + } + break; + case 'reading': + if (mode === 'vocab_kana') { + value = null; + } + break; + case 'glossary-list': + if (definition.glossary) { + value = '
    '; + for (let gloss of definition.glossary) { + value += `
  1. ${gloss}
  2. `; + } + value += '
'; + } + break; + case 'tags': + if (definition.tags) { + value = definition.tags.map((t) => t.name); + } + break; } - let value = definition[key]; if (value !== null && typeof(value) !== 'string') { value = value.join(', '); } - if (mode === 'vocab_kana') { - if (key === 'expression') { - value = definition.reading; - } else if (key === 'reading') { - value = null; - } - } - - if (mode !== 'kanji' && key === 'glossary') { - value = '
    '; - for (let gloss of definition.glossary) { - value += `
  1. ${gloss}
  2. `; - } - value += '
'; - } - - field = field.replace(`{${key}}`, value || ''); + field = field.replace(`{${tag}}`, value || ''); } return field; diff --git a/ext/bg/options.html b/ext/bg/options.html index a0174c40..86739c58 100644 --- a/ext/bg/options.html +++ b/ext/bg/options.html @@ -116,7 +116,7 @@ -

Supported field tags: {expression}, {reading}, {glossary}

+

Allowed tags: {expression}, {reading}, {glossary}, {glossary-list}, {tags}

@@ -145,7 +145,7 @@ -

Supported field tags: {character}, {onyomi}, {kunyomi}, {glossary}

+

Allowed tags: {character}, {onyomi}, {kunyomi}, {glossary}, {glossary-list}