From acc013a1a8051d34322f0f5f91d7bdecc0a18843 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 28 Jan 2024 07:22:47 -0500 Subject: JapaneseUtil refactor (#555) * Copy functions from JapaneseUtil * Remove JapaneseUtil * Update usages of JapaneseUtil functions --- ext/js/templates/sandbox/anki-template-renderer.js | 26 ++++++++++------------ 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'ext/js/templates/sandbox') diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index ef2c1610..52087336 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -22,7 +22,7 @@ import {DictionaryDataUtil} from '../../dictionary/dictionary-data-util.js'; import {PronunciationGenerator} from '../../display/sandbox/pronunciation-generator.js'; import {StructuredContentGenerator} from '../../display/sandbox/structured-content-generator.js'; import {CssStyleApplier} from '../../dom/sandbox/css-style-applier.js'; -import {JapaneseUtil} from '../../language/sandbox/japanese-util.js'; +import {convertHiraganaToKatakana, convertKatakanaToHiragana, distributeFurigana, getKanaMorae, getPitchCategory, isMoraPitchHigh} from '../../language/japanese.js'; import {AnkiTemplateRendererContentManager} from './anki-template-renderer-content-manager.js'; import {TemplateRendererMediaProvider} from './template-renderer-media-provider.js'; import {TemplateRenderer} from './template-renderer.js'; @@ -42,16 +42,14 @@ export class AnkiTemplateRenderer { this._pronunciationStyleApplier = new CssStyleApplier('/data/pronunciation-style.json'); /** @type {RegExp} */ this._structuredContentDatasetKeyIgnorePattern = /^sc([^a-z]|$)/; - /** @type {JapaneseUtil} */ - this._japaneseUtil = new JapaneseUtil(null); /** @type {TemplateRenderer} */ this._templateRenderer = new TemplateRenderer(); /** @type {AnkiNoteDataCreator} */ - this._ankiNoteDataCreator = new AnkiNoteDataCreator(this._japaneseUtil); + this._ankiNoteDataCreator = new AnkiNoteDataCreator(); /** @type {TemplateRendererMediaProvider} */ this._mediaProvider = new TemplateRendererMediaProvider(); /** @type {PronunciationGenerator} */ - this._pronunciationGenerator = new PronunciationGenerator(this._japaneseUtil); + this._pronunciationGenerator = new PronunciationGenerator(); /** @type {?(Map[])} */ this._stateStack = null; /** @type {?import('anki-note-builder').Requirement[]} */ @@ -171,7 +169,7 @@ export class AnkiTemplateRenderer { /** @type {import('template-renderer').HelperFunction} */ _furigana(args, context, options) { const {expression, reading} = this._getFuriganaExpressionAndReading(args, context, options); - const segments = this._japaneseUtil.distributeFurigana(expression, reading); + const segments = distributeFurigana(expression, reading); let result = ''; for (const {text, reading: reading2} of segments) { @@ -190,7 +188,7 @@ export class AnkiTemplateRenderer { /** @type {import('template-renderer').HelperFunction} */ _furiganaPlain(args, context, options) { const {expression, reading} = this._getFuriganaExpressionAndReading(args, context, options); - const segments = this._japaneseUtil.distributeFurigana(expression, reading); + const segments = distributeFurigana(expression, reading); let result = ''; for (const {text, reading: reading2} of segments) { @@ -512,13 +510,13 @@ export class AnkiTemplateRenderer { /** @type {import('template-renderer').HelperFunction} */ _isMoraPitchHigh(args) { const [index, position] = /** @type {[index: number, position: number]} */ (args); - return this._japaneseUtil.isMoraPitchHigh(index, position); + return isMoraPitchHigh(index, position); } /** @type {import('template-renderer').HelperFunction} */ _getKanaMorae(args) { const [text] = /** @type {[text: string]} */ (args); - return this._japaneseUtil.getKanaMorae(`${text}`); + return getKanaMorae(`${text}`); } /** @type {import('template-renderer').HelperFunction} */ @@ -555,7 +553,7 @@ export class AnkiTemplateRenderer { const isVerbOrAdjective = DictionaryDataUtil.isNonNounVerbOrAdjective(wordClasses); const pitches = DictionaryDataUtil.getPronunciationsOfType(pronunciations, 'pitch-accent'); for (const {position} of pitches) { - const category = this._japaneseUtil.getPitchCategory(reading, position, isVerbOrAdjective); + const category = getPitchCategory(reading, position, isVerbOrAdjective); if (category !== null) { categories.add(category); } @@ -666,7 +664,7 @@ export class AnkiTemplateRenderer { */ _createStructuredContentGenerator(data) { const contentManager = new AnkiTemplateRendererContentManager(this._mediaProvider, data); - const instance = new StructuredContentGenerator(contentManager, this._japaneseUtil, document); + const instance = new StructuredContentGenerator(contentManager, document); this._cleanupCallbacks.push(() => contentManager.unloadAll()); return instance; } @@ -735,7 +733,7 @@ export class AnkiTemplateRenderer { if (typeof downstepPosition !== 'number') { return ''; } if (!Array.isArray(nasalPositions)) { nasalPositions = []; } if (!Array.isArray(devoicePositions)) { devoicePositions = []; } - const morae = this._japaneseUtil.getKanaMorae(reading); + const morae = getKanaMorae(reading); switch (format) { case 'text': @@ -756,7 +754,7 @@ export class AnkiTemplateRenderer { const ii = args.length; const {keepProlongedSoundMarks} = options.hash; const value = (ii > 0 ? args[0] : this._computeValue(options, context)); - return typeof value === 'string' ? this._japaneseUtil.convertKatakanaToHiragana(value, keepProlongedSoundMarks === true) : ''; + return typeof value === 'string' ? convertKatakanaToHiragana(value, keepProlongedSoundMarks === true) : ''; } /** @@ -765,7 +763,7 @@ export class AnkiTemplateRenderer { _katakana(args, context, options) { const ii = args.length; const value = (ii > 0 ? args[0] : this._computeValue(options, context)); - return typeof value === 'string' ? this._japaneseUtil.convertHiraganaToKatakana(value) : ''; + return typeof value === 'string' ? convertHiraganaToKatakana(value) : ''; } /** -- cgit v1.2.3