Skip to content

Commit

Permalink
Add glossary-first handlebars (#921)
Browse files Browse the repository at this point in the history
* Add glossary-first handlebars

* Add docs

* Fix missing /if

* Write handlebars test data
  • Loading branch information
Kuuuube authored May 13, 2024
1 parent 0b02bf4 commit 8139046
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/anki-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Flashcard fields can be configured with the following steps:
| `{glossary}` | List of definitions for the term (output format depends on whether running in _grouped_ mode). |
| `{glossary-brief}` | List of definitions for the term in a more compact format. |
| `{glossary-no-dictionary}` | List of definitions for the term, except the dictionary tag is omitted. |
| `{glossary-first}` | First definition for the term (output format depends on whether running in _grouped_ mode). |
| `{glossary-first-brief}` | First definition for the term in a more compact format. |
| `{glossary-first-no-dictionary}` | First definition for the term, except the dictionary tag is omitted. |
| `{part-of-speech}` | Part of speech information for the term. |
| `{phonetic-transcriptions}` | List of phonetic transcriptions for the term. |
| `{pitch-accents}` | List of pitch accent downstep notations for the term. |
Expand Down
33 changes: 33 additions & 0 deletions ext/data/templates/anki-field-templates-upgrade-v35.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{<<<<<<<}}
{{#*inline "glossary-brief"}}
{{~> glossary brief=true ~}}
{{/inline}}
{{=======}}
{{#*inline "glossary-brief"}}
{{~> glossary brief=true ~}}
{{/inline}}

{{~#*inline "glossary-first"~}}
<div style="text-align: left;">
{{~#scope~}}
{{~#if (op "===" definition.type "term")~}}
{{~> glossary-single definition brief=brief noDictionaryTag=noDictionaryTag ~}}
{{~else if (op "||" (op "===" definition.type "termGrouped") (op "===" definition.type "termMerged"))~}}
{{~#if (op ">" definition.definitions.length 1)~}}
{{~#with definition.definitions.[0]~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}{{~/with~}}
{{~else~}}
{{~#with definition.definitions.[0]~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}{{~/with~}}
{{~/if~}}
{{~/if~}}
{{~/scope~}}
</div>
{{~/inline~}}

{{#*inline "glossary-first-no-dictionary"}}
{{~> glossary-first noDictionaryTag=true ~}}
{{/inline}}

{{#*inline "glossary-first-brief"}}
{{~> glossary-first brief=true ~}}
{{/inline}}
{{>>>>>>>}}
24 changes: 24 additions & 0 deletions ext/data/templates/default-anki-field-templates.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@
{{~> glossary brief=true ~}}
{{/inline}}

{{~#*inline "glossary-first"~}}
<div style="text-align: left;">
{{~#scope~}}
{{~#if (op "===" definition.type "term")~}}
{{~> glossary-single definition brief=brief noDictionaryTag=noDictionaryTag ~}}
{{~else if (op "||" (op "===" definition.type "termGrouped") (op "===" definition.type "termMerged"))~}}
{{~#if (op ">" definition.definitions.length 1)~}}
{{~#with definition.definitions.[0]~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}{{~/with~}}
{{~else~}}
{{~#with definition.definitions.[0]~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}{{~/with~}}
{{~/if~}}
{{~/if~}}
{{~/scope~}}
</div>
{{~/inline~}}

{{#*inline "glossary-first-no-dictionary"}}
{{~> glossary-first noDictionaryTag=true ~}}
{{/inline}}

{{#*inline "glossary-first-brief"}}
{{~> glossary-first brief=true ~}}
{{/inline}}

{{#*inline "kunyomi"}}
{{~#each definition.kunyomi}}{{.}}{{#unless @last}}, {{/unless}}{{/each~}}
{{/inline}}
Expand Down
3 changes: 3 additions & 0 deletions ext/js/data/anki-template-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export function getStandardFieldMarkers(type) {
'glossary',
'glossary-brief',
'glossary-no-dictionary',
'glossary-first',
'glossary-first-brief',
'glossary-first-no-dictionary',
'part-of-speech',
'pitch-accents',
'pitch-accent-graphs',
Expand Down
11 changes: 10 additions & 1 deletion ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ export class OptionsUtil {
this._updateVersion31,
this._updateVersion32,
this._updateVersion33,
this._updateVersion34
this._updateVersion34,
this._updateVersion35
];
/* eslint-enable @typescript-eslint/unbound-method */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
Expand Down Expand Up @@ -1277,6 +1278,14 @@ export class OptionsUtil {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v34.handlebars');
}

/**
* - Added dynamic handlebars for first dictionary entry only.
* @type {import('options-util').UpdateFunction}
*/
async _updateVersion35(options) {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v35.handlebars');
}


/**
* @param {string} url
Expand Down
12 changes: 12 additions & 0 deletions ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -2983,6 +2983,18 @@ <h1>Yomitan Settings</h1>
<td><code class="anki-field-marker">{glossary-no-dictionary}</code></td>
<td>List of definitions for the term, except the dictionary tag is omitted.</td>
</tr>
<tr>
<td><code class="anki-field-marker">{glossary}</code></td>
<td>First definition for the term.</td>
</tr>
<tr>
<td><code class="anki-field-marker">{glossary-brief}</code></td>
<td>First definition for the term in a more compact format.</td>
</tr>
<tr>
<td><code class="anki-field-marker">{glossary-no-dictionary}</code></td>
<td>First definition for the term, except the dictionary tag is omitted.</td>
</tr>
<tr>
<td><code class="anki-field-marker">{part-of-speech}</code></td>
<td>Part of speech information for the term.</td>
Expand Down
Loading

0 comments on commit 8139046

Please sign in to comment.