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) { 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) { for (let tag of tags) {
if (supported.indexOf(key) === -1) { let value = definition[tag] || null;
continue; 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 = '<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;
} }
let value = definition[key];
if (value !== null && typeof(value) !== 'string') { if (value !== null && typeof(value) !== 'string') {
value = value.join(', '); value = value.join(', ');
} }
if (mode === 'vocab_kana') { field = field.replace(`{${tag}}`, value || '');
if (key === 'expression') {
value = definition.reading;
} else if (key === 'reading') {
value = null;
}
}
if (mode !== 'kanji' && key === 'glossary') {
value = '<ol>';
for (let gloss of definition.glossary) {
value += `<li>${gloss}</li>`;
}
value += '</ol>';
}
field = field.replace(`{${key}}`, value || '');
} }
return field; return field;

View File

@ -116,7 +116,7 @@
</tbody> </tbody>
</table> </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>
<div id="kanji" class="tab-pane fade"> <div id="kanji" class="tab-pane fade">
@ -145,7 +145,7 @@
</tbody> </tbody>
</table> </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>
</div> </div>
</form> </form>