diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-11-28 14:30:50 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-28 14:30:50 -0500 |
commit | 008ffdb6bffc2855957be948a24c9e07730501d6 (patch) | |
tree | be6642b030011f0007aad1025da3c5accd0d9a54 /ext/bg | |
parent | 94d63f4f87a61f7e42ea44d8100ea11d5ccdbce0 (diff) |
Frequencies marker (#1074)
* Update japanese.js tests
* Simplify fallback/early exit
* Add overloads to furigana and furiganaPlain handlebars helper functions
* Expose unique expression/reading arrays (and subsequently counts)
* Add {frequencies} marker
Diffstat (limited to 'ext/bg')
-rw-r--r-- | ext/bg/data/anki-field-templates-upgrade-v6.handlebars | 17 | ||||
-rw-r--r-- | ext/bg/data/default-anki-field-templates.handlebars | 17 | ||||
-rw-r--r-- | ext/bg/js/anki-note-builder.js | 10 | ||||
-rw-r--r-- | ext/bg/js/settings/anki-controller.js | 1 | ||||
-rw-r--r-- | ext/bg/js/template-renderer.js | 31 |
5 files changed, 66 insertions, 10 deletions
diff --git a/ext/bg/data/anki-field-templates-upgrade-v6.handlebars b/ext/bg/data/anki-field-templates-upgrade-v6.handlebars index 33a2f949..809423ce 100644 --- a/ext/bg/data/anki-field-templates-upgrade-v6.handlebars +++ b/ext/bg/data/anki-field-templates-upgrade-v6.handlebars @@ -6,3 +6,20 @@ {{~/each~}} {{~/if~}} {{/inline}} + +{{#*inline "frequencies"}} + {{~#if (op ">" definition.frequencies.length 0)~}} + <ul style="text-align: left;"> + {{~#each definition.frequencies~}} + <li> + {{~#if (op "!==" ../definition.type "kanji")~}} + {{~#if (op "||" (op ">" ../uniqueExpressions.length 1) (op ">" ../uniqueReadings.length 1))~}}( + {{~#furigana expression reading~}}{{~/furigana~}} + ) {{/if~}} + {{~/if~}} + {{~dictionary}}: {{frequency~}} + </li> + {{~/each~}} + </ul> + {{~/if~}} +{{/inline}} diff --git a/ext/bg/data/default-anki-field-templates.handlebars b/ext/bg/data/default-anki-field-templates.handlebars index 0b0e9ca6..1024e2e6 100644 --- a/ext/bg/data/default-anki-field-templates.handlebars +++ b/ext/bg/data/default-anki-field-templates.handlebars @@ -306,4 +306,21 @@ {{~/if~}} {{/inline}} +{{#*inline "frequencies"}} + {{~#if (op ">" definition.frequencies.length 0)~}} + <ul style="text-align: left;"> + {{~#each definition.frequencies~}} + <li> + {{~#if (op "!==" ../definition.type "kanji")~}} + {{~#if (op "||" (op ">" ../uniqueExpressions.length 1) (op ">" ../uniqueReadings.length 1))~}}( + {{~#furigana expression reading~}}{{~/furigana~}} + ) {{/if~}} + {{~/if~}} + {{~dictionary}}: {{frequency~}} + </li> + {{~/each~}} + </ul> + {{~/if~}} +{{/inline}} + {{~> (lookup . "marker") ~}} diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index 33cd3a0b..b6114e4b 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -108,9 +108,19 @@ class AnkiNoteBuilder { _createNoteData(definition, mode, context, resultOutputMode, glossaryLayoutMode, compactTags) { const pitches = DictionaryDataUtil.getPitchAccentInfos(definition); const pitchCount = pitches.reduce((i, v) => i + v.pitches.length, 0); + const uniqueExpressions = new Set(); + const uniqueReadings = new Set(); + if (definition.type !== 'kanji') { + for (const {expression, reading} of definition.expressions) { + uniqueExpressions.add(expression); + uniqueReadings.add(reading); + } + } return { marker: null, definition, + uniqueExpressions: [...uniqueExpressions], + uniqueReadings: [...uniqueReadings], pitches, pitchCount, group: resultOutputMode === 'group', diff --git a/ext/bg/js/settings/anki-controller.js b/ext/bg/js/settings/anki-controller.js index d9b1591e..957f86d9 100644 --- a/ext/bg/js/settings/anki-controller.js +++ b/ext/bg/js/settings/anki-controller.js @@ -83,6 +83,7 @@ class AnkiController { 'dictionary', 'document-title', 'expression', + 'frequencies', 'furigana', 'furigana-plain', 'glossary', diff --git a/ext/bg/js/template-renderer.js b/ext/bg/js/template-renderer.js index 5dd33814..c1995acd 100644 --- a/ext/bg/js/template-renderer.js +++ b/ext/bg/js/template-renderer.js @@ -117,13 +117,13 @@ class TemplateRenderer { return this._escape(dump); } - _furigana(context, options) { - const definition = options.fn(context); - const segs = jp.distributeFurigana(definition.expression, definition.reading); + _furigana(context, ...args) { + const {expression, reading} = this._getFuriganaExpressionAndReading(context, ...args); + const segs = jp.distributeFurigana(expression, reading); let result = ''; for (const seg of segs) { - if (seg.furigana) { + if (seg.furigana.length > 0) { result += `<ruby>${seg.text}<rt>${seg.furigana}</rt></ruby>`; } else { result += seg.text; @@ -133,20 +133,31 @@ class TemplateRenderer { return result; } - _furiganaPlain(context, options) { - const definition = options.fn(context); - const segs = jp.distributeFurigana(definition.expression, definition.reading); + _furiganaPlain(context, ...args) { + const {expression, reading} = this._getFuriganaExpressionAndReading(context, ...args); + const segs = jp.distributeFurigana(expression, reading); let result = ''; for (const seg of segs) { - if (seg.furigana) { - result += ` ${seg.text}[${seg.furigana}]`; + if (seg.furigana.length > 0) { + if (result.length > 0) { result += ' '; } + result += `${seg.text}[${seg.furigana}]`; } else { result += seg.text; } } - return result.trimLeft(); + return result; + } + + _getFuriganaExpressionAndReading(context, ...args) { + const options = args[args.length - 1]; + if (args.length >= 3) { + return {expression: args[0], reading: args[1]}; + } else { + const {expression, reading} = options.fn(context); + return {expression, reading}; + } } _kanjiLinks(context, options) { |