From 838fd211c6737ce7e2b6802a43837cf4300b60d2 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 1 Aug 2020 16:23:33 -0400 Subject: Pitch accent Anki field templates (#701) * Template helper updates * Add pitch data to exported field formatting data * Reuse note data * Add no-op * Set up pitch accent templates * Refactor version update functions * Implement upgrade process for new Anki templates * Consistency * Update README and anki.js to have matching markers --- ext/bg/js/anki-note-builder.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'ext/bg/js/anki-note-builder.js') diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index 7fe8962a..2405543e 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -15,6 +15,10 @@ * along with this program. If not, see . */ +/* global + * DictionaryDataUtil + */ + class AnkiNoteBuilder { constructor({anki, audioSystem, renderTemplate}) { this._anki = anki; @@ -39,9 +43,10 @@ class AnkiNoteBuilder { } }; + const data = this.createNoteData(definition, mode, context, options); const formattedFieldValuePromises = []; for (const [, fieldValue] of modeOptionsFieldEntries) { - const formattedFieldValuePromise = this.formatField(fieldValue, definition, mode, context, options, templates, null); + const formattedFieldValuePromise = this.formatField(fieldValue, data, templates, null); formattedFieldValuePromises.push(formattedFieldValuePromise); } @@ -55,10 +60,14 @@ class AnkiNoteBuilder { return note; } - async formatField(field, definition, mode, context, options, templates, errors=null) { - const data = { + createNoteData(definition, mode, context, options) { + const pitches = DictionaryDataUtil.getPitchAccentInfos(definition); + const pitchCount = pitches.reduce((i, v) => i + v.pitches.length, 0); + return { marker: null, definition, + pitches, + pitchCount, group: options.general.resultOutputMode === 'group', merge: options.general.resultOutputMode === 'merge', modeTermKanji: mode === 'term-kanji', @@ -67,6 +76,9 @@ class AnkiNoteBuilder { compactGlossaries: options.general.compactGlossaries, context }; + } + + async formatField(field, data, templates, errors=null) { const pattern = /\{([\w-]+)\}/g; return await AnkiNoteBuilder.stringReplaceAsync(field, pattern, async (g0, marker) => { data.marker = marker; -- cgit v1.2.3