Skip to content

Commit 2fc09f9

Browse files
authored
Add single frequency handlebars (#2045)
* Add single frequency handlebars * Add handlebars update * Add documentation for single frequency handlebars * Write tests
1 parent da75c86 commit 2fc09f9

9 files changed

Lines changed: 631 additions & 18 deletions
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{{<<<<<<<}}
2+
{{#*inline "frequencies"}}
3+
{{~#if (op ">" definition.frequencies.length 0)~}}
4+
<ul style="text-align: left;">
5+
{{~#each definition.frequencies~}}
6+
<li>
7+
{{~#if (op "!==" ../definition.type "kanji")~}}
8+
{{~#if (op "||" (op ">" ../uniqueExpressions.length 1) (op ">" ../uniqueReadings.length 1))~}}(
9+
{{~furigana expression reading~}}
10+
) {{/if~}}
11+
{{~/if~}}
12+
{{~dictionaryAlias}}: {{frequency~}}
13+
</li>
14+
{{~/each~}}
15+
</ul>
16+
{{~/if~}}
17+
{{/inline}}
18+
{{=======}}
19+
{{#*inline "frequencies"}}
20+
{{~#if (op ">" definition.frequencies.length 0)~}}
21+
<ul style="text-align: left;">
22+
{{~#each definition.frequencies~}}
23+
{{~#unless (op "&&" ../selectedDictionary (op "!=" ../selectedDictionary dictionary))~}}
24+
<li>
25+
{{~#if (op "!==" ../definition.type "kanji")~}}
26+
{{~#if (op "||" (op ">" ../uniqueExpressions.length 1) (op ">" ../uniqueReadings.length 1))~}}(
27+
{{~furigana expression reading~}}
28+
) {{/if~}}
29+
{{~/if~}}
30+
{{~dictionaryAlias}}: {{frequency~}}
31+
</li>
32+
{{~/unless~}}
33+
{{~/each~}}
34+
</ul>
35+
{{~/if~}}
36+
{{/inline}}
37+
{{>>>>>>>}}
38+
39+
{{#*inline "single-frequency-number"}}
40+
{{~#if (op ">" definition.frequencyNumbers.length 0)~}}
41+
{{~#each definition.frequencyNumbers~}}
42+
{{~#unless (op "&&" ../selectedDictionary (op "!=" ../selectedDictionary dictionary))~}}
43+
{{frequency~}}
44+
{{~/unless~}}
45+
{{~/each~}}
46+
{{/if}}
47+
{{/inline}}

ext/data/templates/default-anki-field-templates.handlebars

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,19 +358,31 @@
358358
{{~#if (op ">" definition.frequencies.length 0)~}}
359359
<ul style="text-align: left;">
360360
{{~#each definition.frequencies~}}
361-
<li>
362-
{{~#if (op "!==" ../definition.type "kanji")~}}
363-
{{~#if (op "||" (op ">" ../uniqueExpressions.length 1) (op ">" ../uniqueReadings.length 1))~}}(
364-
{{~furigana expression reading~}}
365-
) {{/if~}}
366-
{{~/if~}}
367-
{{~dictionaryAlias}}: {{frequency~}}
368-
</li>
361+
{{~#unless (op "&&" ../selectedDictionary (op "!=" ../selectedDictionary dictionary))~}}
362+
<li>
363+
{{~#if (op "!==" ../definition.type "kanji")~}}
364+
{{~#if (op "||" (op ">" ../uniqueExpressions.length 1) (op ">" ../uniqueReadings.length 1))~}}(
365+
{{~furigana expression reading~}}
366+
) {{/if~}}
367+
{{~/if~}}
368+
{{~dictionaryAlias}}: {{frequency~}}
369+
</li>
370+
{{~/unless~}}
369371
{{~/each~}}
370372
</ul>
371373
{{~/if~}}
372374
{{/inline}}
373375

376+
{{#*inline "single-frequency-number"}}
377+
{{~#if (op ">" definition.frequencyNumbers.length 0)~}}
378+
{{~#each definition.frequencyNumbers~}}
379+
{{~#unless (op "&&" ../selectedDictionary (op "!=" ../selectedDictionary dictionary))~}}
380+
{{frequency~}}
381+
{{~/unless~}}
382+
{{~/each~}}
383+
{{/if}}
384+
{{/inline}}
385+
374386
{{#*inline "frequency-harmonic-rank"}}
375387
{{~#if (op "===" definition.frequencyHarmonic -1) ~}}
376388
9999999

ext/js/data/anki-note-data-creator.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function getPublicContext(context) {
171171

172172
/**
173173
* @param {import('dictionary').TermDictionaryEntry|import('dictionary').KanjiDictionaryEntry} dictionaryEntry
174-
* @returns {number[]}
174+
* @returns {import('anki-templates').FrequencyNumber[]}
175175
*/
176176
function getFrequencyNumbers(dictionaryEntry) {
177177
let previousDictionary;
@@ -187,13 +187,13 @@ function getFrequencyNumbers(dictionaryEntry) {
187187
if (frequencyMatch !== null) {
188188
const frequencyParsed = Number.parseInt(frequencyMatch[0], 10);
189189
if (frequencyParsed > 0) {
190-
frequencies.push(frequencyParsed);
190+
frequencies.push({dictionary: dictionary, frequency: frequencyParsed});
191191
continue;
192192
}
193193
}
194194
}
195195
if (frequency > 0) {
196-
frequencies.push(frequency);
196+
frequencies.push({dictionary: dictionary, frequency: frequency});
197197
}
198198
}
199199
return frequencies;
@@ -212,7 +212,7 @@ function getFrequencyHarmonic(dictionaryEntry) {
212212

213213
let total = 0;
214214
for (const frequency of frequencies) {
215-
total += 1 / frequency;
215+
total += 1 / frequency.frequency;
216216
}
217217
return Math.floor(frequencies.length / total);
218218
}
@@ -230,7 +230,7 @@ function getFrequencyAverage(dictionaryEntry) {
230230

231231
let total = 0;
232232
for (const frequency of frequencies) {
233-
total += frequency;
233+
total += frequency.frequency;
234234
}
235235
return Math.floor(total / frequencies.length);
236236
}
@@ -441,6 +441,7 @@ function getTermDefinition(dictionaryEntry, context, resultOutputMode, dictionar
441441
const termTags = createCachedValue(getTermTags.bind(null, dictionaryEntry, type));
442442
const expressions = createCachedValue(getTermExpressions.bind(null, dictionaryEntry));
443443
const frequencies = createCachedValue(getTermFrequencies.bind(null, dictionaryEntry));
444+
const frequencyNumbers = createCachedValue(getFrequencyNumbers.bind(null, dictionaryEntry));
444445
const frequencyHarmonic = createCachedValue(getFrequencyHarmonic.bind(null, dictionaryEntry));
445446
const frequencyAverage = createCachedValue(getFrequencyAverage.bind(null, dictionaryEntry));
446447
const pitches = createCachedValue(getTermPitches.bind(null, dictionaryEntry));
@@ -483,6 +484,7 @@ function getTermDefinition(dictionaryEntry, context, resultOutputMode, dictionar
483484
get termTags() { return getCachedValue(termTags); },
484485
get definitions() { return getCachedValue(commonInfo).definitions; },
485486
get frequencies() { return getCachedValue(frequencies); },
487+
get frequencyNumbers() { return getCachedValue(frequencyNumbers); },
486488
get frequencyHarmonic() { return getCachedValue(frequencyHarmonic); },
487489
get frequencyAverage() { return getCachedValue(frequencyAverage); },
488490
get pitches() { return getCachedValue(pitches); },

ext/js/data/anki-template-util.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ export function getDynamicTemplates(options, dictionaryInfo) {
122122
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary.name);
123123
if (!dictionary.enabled) { continue; }
124124
const totalTerms = currentDictionaryInfo?.counts?.terms?.total;
125-
if (!totalTerms || totalTerms === 0) { continue; }
126-
dynamicTemplates += `
125+
if (totalTerms && totalTerms > 0) {
126+
dynamicTemplates += `
127127
{{#*inline "single-glossary-${getKebabCase(dictionary.name)}"}}
128128
{{~> glossary selectedDictionary='${escapeDictName(dictionary.name)}'}}
129129
{{/inline}}
@@ -144,6 +144,18 @@ export function getDynamicTemplates(options, dictionaryInfo) {
144144
{{~> glossary-plain-no-dictionary selectedDictionary='${escapeDictName(dictionary.name)}' noDictionaryTag=true}}
145145
{{/inline}}
146146
`;
147+
}
148+
const totalMeta = currentDictionaryInfo?.counts?.termMeta;
149+
if (totalMeta && totalMeta.freq && totalMeta.freq > 0) {
150+
dynamicTemplates += `
151+
{{#*inline "single-frequency-number-${getKebabCase(dictionary.name)}"}}
152+
{{~> single-frequency-number selectedDictionary='${escapeDictName(dictionary.name)}'}}
153+
{{/inline}}
154+
{{#*inline "single-frequency-${getKebabCase(dictionary.name)}"}}
155+
{{~> frequencies selectedDictionary='${escapeDictName(dictionary.name)}'}}
156+
{{/inline}}
157+
`;
158+
}
147159
}
148160
return dynamicTemplates;
149161
}
@@ -159,8 +171,13 @@ export function getDynamicFieldMarkers(dictionaries, dictionaryInfo) {
159171
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary.name);
160172
if (!dictionary.enabled) { continue; }
161173
const totalTerms = currentDictionaryInfo?.counts?.terms?.total;
162-
if (!totalTerms || totalTerms === 0) { continue; }
163-
markers.push(`single-glossary-${getKebabCase(dictionary.name)}`);
174+
if (totalTerms && totalTerms > 0) {
175+
markers.push(`single-glossary-${getKebabCase(dictionary.name)}`);
176+
}
177+
const totalMeta = currentDictionaryInfo?.counts?.termMeta;
178+
if (totalMeta && totalMeta.freq && totalMeta.freq > 0) {
179+
markers.push(`single-frequency-number-${getKebabCase(dictionary.name)}`);
180+
}
164181
}
165182
return markers;
166183
}

ext/js/data/options-util.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ export class OptionsUtil {
578578
this._updateVersion64,
579579
this._updateVersion65,
580580
this._updateVersion66,
581+
this._updateVersion67,
581582
];
582583
/* eslint-enable @typescript-eslint/unbound-method */
583584
if (typeof targetVersion === 'number' && targetVersion < result.length) {
@@ -1745,6 +1746,14 @@ export class OptionsUtil {
17451746
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v66.handlebars');
17461747
}
17471748

1749+
/**
1750+
* - Added dynamic handlebars for single frequency dictionaries.
1751+
* @type {import('options-util').UpdateFunction}
1752+
*/
1753+
async _updateVersion67(options) {
1754+
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v67.handlebars');
1755+
}
1756+
17481757
/**
17491758
* @param {string} url
17501759
* @returns {Promise<chrome.tabs.Tab>}

ext/templates-modals.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,14 @@ <h1 class="modal-title">Pronunciation Dictionaries</h1>
11711171
Defaults to 0 occurrences when frequency data is not found, the lowest possible occurrence-based term or kanji usage.
11721172
</td>
11731173
</tr>
1174+
<tr>
1175+
<td><code class="anki-field-marker">{single-frequency-DICT-NAME}</code></td>
1176+
<td>All frequencies from the selected dictionary put in a list.</td>
1177+
</tr>
1178+
<tr>
1179+
<td><code class="anki-field-marker">{single-frequency-number-DICT-NAME}</code></td>
1180+
<td>A single frequency number from the selected dictionary with no formatting data.</td>
1181+
</tr>
11741182
<tr>
11751183
<td><code class="anki-field-marker">{screenshot}</code></td>
11761184
<td>Screenshot of the web page taken at the time the term or kanji was added.</td>

0 commit comments

Comments
 (0)