From 9b798601b48a40b65e38b433924a99d155872ba7 Mon Sep 17 00:00:00 2001 From: praschke Date: Tue, 17 Oct 2023 22:25:30 +0100 Subject: update: hasMedia and getMedia --- docs/templates.md | 18 ++--- .../anki-field-templates-upgrade-v21.handlebars | 36 ++++++++++ .../default-anki-field-templates.handlebars | 12 ++-- test/test-options-util.js | 82 ++++++++++++++++++++++ 4 files changed, 133 insertions(+), 15 deletions(-) diff --git a/docs/templates.md b/docs/templates.md index e19309a7..0aa26b84 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -686,8 +686,8 @@ These functions are used together in order to request media and other types of o
Syntax: - {{#hasMedia type args...}}{{/hasMedia}}
- {{#getMedia type args... [escape=true|false]}}{{/getMedia}}
+ {{hasMedia type args...}}
+ {{getMedia type args... [escape=true|false]}}
* _`type`_
The type of media to check for. @@ -710,19 +710,19 @@ These functions are used together in order to request media and other types of o Examples: ```handlebars - {{#if (hasMedia "audio")}}The audio file name is: {{#getMedia "audio"}}{{/getMedia}}{{/if}} + {{#if (hasMedia "audio")}}The audio file name is: {{getMedia "audio"}}{{/if}} - {{#if (hasMedia "screenshot")}}The screenshot file name is: {{#getMedia "screenshot"}}{{/getMedia}}{{/if}} + {{#if (hasMedia "screenshot")}}The screenshot file name is: {{getMedia "screenshot"}}{{/if}} - {{#if (hasMedia "clipboardImage")}}The clipboard image file name is: {{#getMedia "clipboardImage"}}{{/getMedia}}{{/if}} + {{#if (hasMedia "clipboardImage")}}The clipboard image file name is: {{getMedia "clipboardImage"}}{{/if}} - {{#if (hasMedia "clipboardText")}}The clipboard text is: {{#getMedia "clipboardText"}}{{/getMedia}}{{/if}} + {{#if (hasMedia "clipboardText")}}The clipboard text is: {{getMedia "clipboardText"}}{{/if}} - {{#if (hasMedia "selectionText")}}The selection text is: {{#getMedia "selectionText"}}{{/getMedia}}{{/if}} + {{#if (hasMedia "selectionText")}}The selection text is: {{getMedia "selectionText"}}{{/if}} - {{#if (hasMedia "textFurigana" "日本語")}}This is an example of text with generated furigana: {{#getMedia "textFurigana" "日本語" escape=false}}{{/getMedia}}{{/if}} + {{#if (hasMedia "textFurigana" "日本語")}}This is an example of text with generated furigana: {{getMedia "textFurigana" "日本語" escape=false}}{{/if}} - {{#if (hasMedia "dictionaryMedia" "image.png" dictionary="Example Dictionary")}}The remapped file name for image.png is: {{#getMedia "dictionaryMedia" "image.png" dictionary="Example Dictionary"}}{{/getMedia}}{{/if}} + {{#if (hasMedia "dictionaryMedia" "image.png" dictionary="Example Dictionary")}}The remapped file name for image.png is: {{getMedia "dictionaryMedia" "image.png" dictionary="Example Dictionary"}}{{/if}} ``` Output: diff --git a/ext/data/templates/anki-field-templates-upgrade-v21.handlebars b/ext/data/templates/anki-field-templates-upgrade-v21.handlebars index 2fa0f0aa..0bfd95a5 100644 --- a/ext/data/templates/anki-field-templates-upgrade-v21.handlebars +++ b/ext/data/templates/anki-field-templates-upgrade-v21.handlebars @@ -34,6 +34,12 @@ {{~set "previousDictionary" dictionary~}} {{>>>>>>>}} +{{<<<<<<<}} +[sound:{{#getMedia "audio"}}{{/getMedia}}] +{{=======}} +[sound:{{getMedia "audio"}}] +{{>>>>>>>}} + {{<<<<<<<}} {{~#furigana}}{{{.}}}{{/furigana~}} {{=======}} @@ -58,6 +64,24 @@ {{furiganaPlain definition}} {{>>>>>>>}} +{{<<<<<<<}} + +{{=======}} + +{{>>>>>>>}} + +{{<<<<<<<}} + +{{=======}} + +{{>>>>>>>}} + +{{<<<<<<<}} +{{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText"}}{{/getMedia}}{{/if~}} +{{=======}} +{{~#if (hasMedia "clipboardText")}}{{getMedia "clipboardText"}}{{/if~}} +{{>>>>>>>}} + {{<<<<<<<}} {{~#furigana expression reading~}}{{~/furigana~}} {{=======}} @@ -107,3 +131,15 @@ {{~set (concat "used_" .) true~}} {{~set "first" false~}} {{>>>>>>>}} + +{{<<<<<<<}} +{{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText"}}{{/getMedia}}{{/if~}} +{{=======}} +{{~#if (hasMedia "selectionText")}}{{getMedia "selectionText"}}{{/if~}} +{{>>>>>>>}} + +{{<<<<<<<}} +{{#getMedia "textFurigana" definition.cloze.sentence escape=false}}{{/getMedia}} +{{=======}} +{{getMedia "textFurigana" definition.cloze.sentence escape=false}} +{{>>>>>>>}} diff --git a/ext/data/templates/default-anki-field-templates.handlebars b/ext/data/templates/default-anki-field-templates.handlebars index 69c7e7a1..08950f78 100644 --- a/ext/data/templates/default-anki-field-templates.handlebars +++ b/ext/data/templates/default-anki-field-templates.handlebars @@ -32,7 +32,7 @@ {{#*inline "audio"}} {{~#if (hasMedia "audio")~}} - [sound:{{#getMedia "audio"}}{{/getMedia}}] + [sound:{{getMedia "audio"}}] {{~/if~}} {{/inline}} @@ -174,7 +174,7 @@ {{#*inline "screenshot"}} {{~#if (hasMedia "screenshot")~}} - + {{~/if~}} {{/inline}} @@ -231,12 +231,12 @@ {{#*inline "clipboard-image"}} {{~#if (hasMedia "clipboardImage")~}} - + {{~/if~}} {{/inline}} {{#*inline "clipboard-text"}} - {{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText"}}{{/getMedia}}{{/if~}} + {{~#if (hasMedia "clipboardText")}}{{getMedia "clipboardText"}}{{/if~}} {{/inline}} {{#*inline "conjugation"}} @@ -316,13 +316,13 @@ {{/inline}} {{#*inline "selection-text"}} - {{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText"}}{{/getMedia}}{{/if~}} + {{~#if (hasMedia "selectionText")}}{{getMedia "selectionText"}}{{/if~}} {{/inline}} {{#*inline "sentence-furigana"}} {{~#if definition.cloze~}} {{~#if (hasMedia "textFurigana" definition.cloze.sentence)~}} - {{#getMedia "textFurigana" definition.cloze.sentence escape=false}}{{/getMedia}} + {{getMedia "textFurigana" definition.cloze.sentence escape=false}} {{~else~}} {{definition.cloze.sentence}} {{~/if~}} diff --git a/test/test-options-util.js b/test/test-options-util.js index 78204de9..23cfc121 100644 --- a/test/test-options-util.js +++ b/test/test-options-util.js @@ -1479,6 +1479,88 @@ async function testFieldTemplatesUpdate(extDir) { {{~/scope~}} {{/inline}} +{{~> (lookup . "marker") ~}}`.trimStart() + }, + // block helper update: hasMedia and getMedia + { + oldVersion: 20, + newVersion: 21, + old: ` +{{#*inline "audio"}} + {{~#if (hasMedia "audio")~}} + [sound:{{#getMedia "audio"}}{{/getMedia}}] + {{~/if~}} +{{/inline}} + +{{#*inline "screenshot"}} + {{~#if (hasMedia "screenshot")~}} + + {{~/if~}} +{{/inline}} + +{{#*inline "clipboard-image"}} + {{~#if (hasMedia "clipboardImage")~}} + + {{~/if~}} +{{/inline}} + +{{#*inline "clipboard-text"}} + {{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText"}}{{/getMedia}}{{/if~}} +{{/inline}} + +{{#*inline "selection-text"}} + {{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText"}}{{/getMedia}}{{/if~}} +{{/inline}} + +{{#*inline "sentence-furigana"}} + {{~#if definition.cloze~}} + {{~#if (hasMedia "textFurigana" definition.cloze.sentence)~}} + {{#getMedia "textFurigana" definition.cloze.sentence escape=false}}{{/getMedia}} + {{~else~}} + {{definition.cloze.sentence}} + {{~/if~}} + {{~/if~}} +{{/inline}} + +{{~> (lookup . "marker") ~}}`.trimStart(), + + expected: ` +{{#*inline "audio"}} + {{~#if (hasMedia "audio")~}} + [sound:{{getMedia "audio"}}] + {{~/if~}} +{{/inline}} + +{{#*inline "screenshot"}} + {{~#if (hasMedia "screenshot")~}} + + {{~/if~}} +{{/inline}} + +{{#*inline "clipboard-image"}} + {{~#if (hasMedia "clipboardImage")~}} + + {{~/if~}} +{{/inline}} + +{{#*inline "clipboard-text"}} + {{~#if (hasMedia "clipboardText")}}{{getMedia "clipboardText"}}{{/if~}} +{{/inline}} + +{{#*inline "selection-text"}} + {{~#if (hasMedia "selectionText")}}{{getMedia "selectionText"}}{{/if~}} +{{/inline}} + +{{#*inline "sentence-furigana"}} + {{~#if definition.cloze~}} + {{~#if (hasMedia "textFurigana" definition.cloze.sentence)~}} + {{getMedia "textFurigana" definition.cloze.sentence escape=false}} + {{~else~}} + {{definition.cloze.sentence}} + {{~/if~}} + {{~/if~}} +{{/inline}} + {{~> (lookup . "marker") ~}}`.trimStart() } ]; -- cgit v1.2.3