Stroke count marker (#1251)

* Add stroke-count definition

* Update options

* Update documentation

* Add helper
This commit is contained in:
toasted-nutbread 2021-01-16 15:29:42 -05:00 committed by GitHub
parent 0ac3577119
commit b234294540
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 4 deletions

View File

@ -195,6 +195,7 @@ Flashcard fields can be configured with the following steps:
`{onyomi}` | Onyomi (Chinese reading) for the kanji expressed as hiragana.
`{screenshot}` | Screenshot of the web page taken at the time the kanji was added.
`{sentence}` | Sentence, quote, or phrase that the character appears in from the source content.
`{stroke-count}` | Number of strokes that the kanji character has.
`{url}` | Address of the web page in which the kanji appeared in.
When creating your model for Yomichan, *make sure that you pick a unique field to be first*; fields that will

View File

@ -0,0 +1,14 @@
{{#*inline "stroke-count"}}
{{~#scope~}}
{{~#set "found" false}}{{/set~}}
{{~#each definition.stats.misc~}}
{{~#if (op "===" name "strokes")~}}
{{~#set "found" true}}{{/set~}}
Stroke count: {{value}}
{{~/if~}}
{{~/each~}}
{{~#if (op "!" (get "found"))~}}
Stroke count: Unknown
{{~/if~}}
{{~/scope~}}
{{/inline}}

View File

@ -323,4 +323,19 @@
{{~/if~}}
{{/inline}}
{{#*inline "stroke-count"}}
{{~#scope~}}
{{~#set "found" false}}{{/set~}}
{{~#each definition.stats.misc~}}
{{~#if (op "===" name "strokes")~}}
{{~#set "found" true}}{{/set~}}
Stroke count: {{value}}
{{~/if~}}
{{~/each~}}
{{~#if (op "!" (get "found"))~}}
Stroke count: Unknown
{{~/if~}}
{{~/scope~}}
{{/inline}}
{{~> (lookup . "marker") ~}}

View File

@ -487,7 +487,7 @@ class OptionsUtil {
update: this._updateVersion7.bind(this)
},
{
async: false,
async: true,
update: this._updateVersion8.bind(this)
}
];
@ -680,7 +680,7 @@ class OptionsUtil {
return options;
}
_updateVersion8(options) {
async _updateVersion8(options) {
// Version 8 changes:
// Added translation.textReplacements.
// Moved anki.sentenceExt to sentenceParsing.scanExtent.
@ -690,6 +690,8 @@ class OptionsUtil {
// Added inputs.hotkeys.
// Added anki.suspendNewCards.
// Added popupWindow.
// Updated handlebars templates to include "stroke-count" definition.
await this._addFieldTemplatesToOptions(options, '/bg/data/anki-field-templates-upgrade-v8.handlebars');
for (const profile of options.profiles) {
profile.options.translation.textReplacements = {
searchOriginal: true,

View File

@ -118,6 +118,7 @@ class AnkiController {
'onyomi',
'screenshot',
'sentence',
'stroke-count',
'tags',
'url'
];

View File

@ -107,7 +107,8 @@ class TemplateRenderer {
['property', this._property.bind(this)],
['noop', this._noop.bind(this)],
['isMoraPitchHigh', this._isMoraPitchHigh.bind(this)],
['getKanaMorae', this._getKanaMorae.bind(this)]
['getKanaMorae', this._getKanaMorae.bind(this)],
['typeof', this._getTypeof.bind(this)]
];
for (const [name, helper] of helpers) {
@ -406,4 +407,10 @@ class TemplateRenderer {
_getKanaMorae(context, text) {
return this._japaneseUtil.getKanaMorae(`${text}`);
}
_getTypeof(context, ...args) {
const ii = args.length - 1;
const value = (ii > 0 ? args[0] : args[ii].fn(context));
return typeof value;
}
}

View File

@ -2602,6 +2602,10 @@
<td><code class="anki-field-marker">{onyomi}</code></td>
<td>Onyomi (Chinese reading) for the kanji, expressed as hiragana.</td>
</tr>
<tr>
<td><code class="anki-field-marker">{stroke-count}</code></td>
<td>Number of strokes that the kanji character has.</td>
</tr>
</tbody>
<thead>
<tr>

View File

@ -614,6 +614,7 @@ async function testFieldTemplatesUpdate(extDir) {
const update2 = loadDataFile('bg/data/anki-field-templates-upgrade-v2.handlebars');
const update4 = loadDataFile('bg/data/anki-field-templates-upgrade-v4.handlebars');
const update6 = loadDataFile('bg/data/anki-field-templates-upgrade-v6.handlebars');
const update8 = loadDataFile('bg/data/anki-field-templates-upgrade-v8.handlebars');
const data = [
// Standard format
@ -633,6 +634,7 @@ async function testFieldTemplatesUpdate(extDir) {
${update2}
${update4}
${update6}
${update8}
{{~> (lookup . "marker") ~}}`.trimStart()
},
// Non-standard marker format
@ -652,7 +654,8 @@ ${update6}
{{~> (lookup . "marker2") ~}}
${update2}
${update4}
${update6}`.trimStart()
${update6}
${update8}`.trimStart()
},
// Empty test
{
@ -663,6 +666,7 @@ ${update6}`.trimStart()
${update2}
${update4}
${update6}
${update8}
{{~> (lookup . "marker") ~}}`.trimStart()
},
// Definition tags update
@ -735,6 +739,7 @@ ${update6}
${update2}
${update4}
${update6}
${update8}
{{~> (lookup . "marker") ~}}
`.trimStart()
}