Support additional replacement fields

This commit is contained in:
Alex Yatskov 2016-07-10 18:16:51 -07:00
parent 1476ed4b71
commit 0eccd89f14
2 changed files with 40 additions and 24 deletions

View File

@ -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;
}
let value = definition[key];
if (value !== null && typeof(value) !== 'string') {
value = value.join(', ');
}
if (mode === 'vocab_kana') {
if (key === 'expression') {
for (let tag of tags) {
let value = definition[tag] || null;
switch (tag) {
case 'expression':
if (mode === 'vocab_kana' && definition.reading) {
value = definition.reading;
} else if (key === 'reading') {
}
break;
case 'reading':
if (mode === 'vocab_kana') {
value = null;
}
}
if (mode !== 'kanji' && key === 'glossary') {
break;
case 'glossary-list':
if (definition.glossary) {
value = '<ol>';
for (let gloss of definition.glossary) {
value += `<li>${gloss}</li>`;
}
value += '</ol>';
}
break;
case 'tags':
if (definition.tags) {
value = definition.tags.map((t) => t.name);
}
break;
}
field = field.replace(`{${key}}`, value || '');
if (value !== null && typeof(value) !== 'string') {
value = value.join(', ');
}
field = field.replace(`{${tag}}`, value || '');
}
return field;

View File

@ -116,7 +116,7 @@
</tbody>
</table>
<p>Supported field tags: <code>{expression}</code>, <code>{reading}</code>, <code>{glossary}</code></p>
<p>Allowed tags: <code>{expression}</code>, <code>{reading}</code>, <code>{glossary}</code>, <code>{glossary-list}</code>, <code>{tags}</code></p>
</div>
<div id="kanji" class="tab-pane fade">
@ -145,7 +145,7 @@
</tbody>
</table>
<p>Supported field tags: <code>{character}</code>, <code>{onyomi}</code>, <code>{kunyomi}</code>, <code>{glossary}</code></p>
<p>Allowed tags: <code>{character}</code>, <code>{onyomi}</code>, <code>{kunyomi}</code>, <code>{glossary}</code>, <code>{glossary-list}</code></p>
</div>
</div>
</form>