From dfd52e1ecd6919094e0b45fde2ca03c30eaad01f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 25 Aug 2021 21:58:51 -0400 Subject: [PATCH] Hiragana & katakana helpers (#1908) * Add hiragana and katakana template helpers * Update documentation * Fix documentation --- docs/templates.md | 62 ++++++++++++++++++- .../sandbox/anki-template-renderer.js | 16 ++++- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/docs/templates.md b/docs/templates.md index f1777fde..be17c30a 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -539,8 +539,8 @@ Returns the type of a value.
Syntax: - {{#typeof value}}{{/get}}
- {{#typeof}}value{{/get}}
+ {{#typeof value}}{{/typeof}}
+ {{#typeof}}value{{/typeof}}
* _`value`_
The value to check. @@ -775,6 +775,64 @@ same as the system used for generating popup and search page dictionary entries.
+### `hiragana` + +Converts katakana text to hiragana. + +
+ Syntax: + + {{#hiragana value}}{{/hiragana}}
+ {{#hiragana}}value{{/hiragana}}
+ + * _`value`_
+ The text to convert. +
+
+ Example: + + ```handlebars + {{#hiragana "よみちゃん ヨミちゃん ヨミチャン"}}{{/hiragana}} + {{#hiragana}}よみちゃん ヨミちゃん ヨミチャン{{/hiragana}} + ``` + + Output: + ```html + よみちゃん よみちゃん よみちゃん + よみちゃん よみちゃん よみちゃん + ``` +
+ + +### `katakana` + +Converts hiragana text to katakana. + +
+ Syntax: + + {{#katakana text}}{{/katakana}}
+ {{#katakana}}text{{/katakana}}
+ + * _`text`_
+ The text to convert. +
+
+ Example: + + ```handlebars + {{#hiragana "よみちゃん ヨミちゃん ヨミチャン"}}{{/hiragana}} + {{#hiragana}}よみちゃん ヨミちゃん ヨミチャン{{/hiragana}} + ``` + + Output: + ```html + ヨミチャン ヨミチャン ヨミチャン + ヨミチャン ヨミチャン ヨミチャン + ``` +
+ + ## Legacy Helpers Yomichan has historically used Handlebars templates to generate the HTML used on the search page and results popup. diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index 43092ec2..8a257bcc 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -87,7 +87,9 @@ class AnkiTemplateRenderer { ['formatGlossary', this._formatGlossary.bind(this)], ['hasMedia', this._hasMedia.bind(this)], ['getMedia', this._getMedia.bind(this)], - ['pronunciation', this._pronunciation.bind(this)] + ['pronunciation', this._pronunciation.bind(this)], + ['hiragana', this._hiragana.bind(this)], + ['katakana', this._katakana.bind(this)] ]); this._templateRenderer.registerDataType('ankiNote', { modifier: ({marker, commonData}) => this._ankiNoteDataCreator.create(marker, commonData), @@ -591,4 +593,16 @@ class AnkiTemplateRenderer { return ''; } } + + _hiragana(context, ...args) { + const ii = args.length - 1; + const value = (ii > 0 ? args[0] : args[ii].fn(context)); + return this._japaneseUtil.convertKatakanaToHiragana(value); + } + + _katakana(context, ...args) { + const ii = args.length - 1; + const value = (ii > 0 ? args[0] : args[ii].fn(context)); + return this._japaneseUtil.convertHiraganaToKatakana(value); + } }