From b784e5b11a596a456eb8879d394fde64bc63aaef Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Sep 2021 18:19:53 -0400 Subject: Katakana to hiragana conversion options (#1965) * Refactor convertKatakanaToHiragana * Add keepProlongedSoundMarks option * Test keepProlongedSoundMarks option * Refactor * Add keepProlongedSoundMarks option to hiragana handlebars helper * Update documentation --- docs/templates.md | 11 +++++++-- ext/js/language/sandbox/japanese-util.js | 27 ++++++++++++++-------- ext/js/templates/sandbox/anki-template-renderer.js | 9 +++++--- test/test-japanese-util.js | 8 ++++--- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/docs/templates.md b/docs/templates.md index 638c79a6..778b8e2d 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -786,11 +786,14 @@ Converts katakana text to hiragana.
Syntax: - {{#hiragana value}}{{/hiragana}}
- {{#hiragana}}value{{/hiragana}}
+ {{#hiragana value [keepProlongedSoundMarks=true|false]}}{{/hiragana}}
+ {{#hiragana [keepProlongedSoundMarks=true|false]}}value{{/hiragana}}
* _`value`_
The text to convert. + * _`keepProlongedSoundMarks`_ _(optional)_
+ Whether or not the `ー` character should be kept or converted to a vowel character. + Defaults to `false` if not specified.
Example: @@ -798,12 +801,16 @@ Converts katakana text to hiragana. ```handlebars {{#hiragana "よみちゃん ヨミちゃん ヨミチャン"}}{{/hiragana}} {{#hiragana}}よみちゃん ヨミちゃん ヨミチャン{{/hiragana}} + {{#hiragana}}ローマ字{{/hiragana}} + {{#hiragana keepProlongedSoundMarks=true}}ローマ字{{/hiragana}} ``` Output: ```html よみちゃん よみちゃん よみちゃん よみちゃん よみちゃん よみちゃん + ろうま字 + ろーま字 ```
diff --git a/ext/js/language/sandbox/japanese-util.js b/ext/js/language/sandbox/japanese-util.js index 9b58d255..a7542e79 100644 --- a/ext/js/language/sandbox/japanese-util.js +++ b/ext/js/language/sandbox/japanese-util.js @@ -297,20 +297,27 @@ const JapaneseUtil = (() => { return this._wanakana !== null; } - convertKatakanaToHiragana(text) { + convertKatakanaToHiragana(text, keepProlongedSoundMarks=false) { let result = ''; const offset = (HIRAGANA_CONVERSION_RANGE[0] - KATAKANA_CONVERSION_RANGE[0]); for (let char of text) { const codePoint = char.codePointAt(0); - if (codePoint === KATAKANA_SMALL_KA_CODE_POINT || codePoint === KATAKANA_SMALL_KE_CODE_POINT) { - // No change - } else if (codePoint === KANA_PROLONGED_SOUND_MARK_CODE_POINT) { - if (result.length > 0) { - const char2 = getProlongedHiragana(result[result.length - 1]); - if (char2 !== null) { char = char2; } - } - } else if (isCodePointInRange(codePoint, KATAKANA_CONVERSION_RANGE)) { - char = String.fromCodePoint(codePoint + offset); + switch (codePoint) { + case KATAKANA_SMALL_KA_CODE_POINT: + case KATAKANA_SMALL_KE_CODE_POINT: + // No change + break; + case KANA_PROLONGED_SOUND_MARK_CODE_POINT: + if (!keepProlongedSoundMarks && result.length > 0) { + const char2 = getProlongedHiragana(result[result.length - 1]); + if (char2 !== null) { char = char2; } + } + break; + default: + if (isCodePointInRange(codePoint, KATAKANA_CONVERSION_RANGE)) { + char = String.fromCodePoint(codePoint + offset); + } + break; } result += char; } diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index b721fadc..ae89aade 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -607,13 +607,16 @@ class AnkiTemplateRenderer { _hiragana(context, ...args) { const ii = args.length - 1; - const value = (ii > 0 ? args[0] : args[ii].fn(context)); - return this._japaneseUtil.convertKatakanaToHiragana(value); + const options = args[ii]; + const {keepProlongedSoundMarks} = options.hash; + const value = (ii > 0 ? args[0] : options.fn(context)); + return this._japaneseUtil.convertKatakanaToHiragana(value, keepProlongedSoundMarks === true); } _katakana(context, ...args) { const ii = args.length - 1; - const value = (ii > 0 ? args[0] : args[ii].fn(context)); + const options = args[ii]; + const value = (ii > 0 ? args[0] : options.fn(context)); return this._japaneseUtil.convertHiraganaToKatakana(value); } } diff --git a/test/test-japanese-util.js b/test/test-japanese-util.js index f3b53844..cf1ee87e 100644 --- a/test/test-japanese-util.js +++ b/test/test-japanese-util.js @@ -130,11 +130,13 @@ function testConvertKatakanaToHiragana() { ['ヒラガナひらがな', 'ひらがなひらがな'], ['chikaraちからチカラ力', 'chikaraちからちから力'], ['katakana', 'katakana'], - ['hiragana', 'hiragana'] + ['hiragana', 'hiragana'], + ['カーナー', 'かあなあ'], + ['カーナー', 'かーなー', true] ]; - for (const [string, expected] of data) { - assert.strictEqual(jp.convertKatakanaToHiragana(string), expected); + for (const [string, expected, keepProlongedSoundMarks=false] of data) { + assert.strictEqual(jp.convertKatakanaToHiragana(string, keepProlongedSoundMarks), expected); } } -- cgit v1.2.3