Simplify get media structure (#1817)

* Remove format argument of getMedia

* Implement escaping
This commit is contained in:
toasted-nutbread 2021-07-09 20:14:05 -04:00 committed by GitHub
parent 22f048e527
commit 7a1570885e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 28 deletions

View File

@ -1,11 +1,11 @@
{{#*inline "selection-text"}} {{#*inline "selection-text"}}
{{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText" format="text"}}{{/getMedia}}{{/if~}} {{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText"}}{{/getMedia}}{{/if~}}
{{/inline}} {{/inline}}
{{#*inline "sentence-furigana"}} {{#*inline "sentence-furigana"}}
{{~#if definition.cloze~}} {{~#if definition.cloze~}}
{{~#if (hasMedia "textFurigana" definition.cloze.sentence)~}} {{~#if (hasMedia "textFurigana" definition.cloze.sentence)~}}
{{#getMedia "textFurigana" definition.cloze.sentence format="html"}}{{/getMedia}} {{#getMedia "textFurigana" definition.cloze.sentence escape=false}}{{/getMedia}}
{{~else~}} {{~else~}}
{{definition.cloze.sentence}} {{definition.cloze.sentence}}
{{~/if~}} {{~/if~}}
@ -36,7 +36,7 @@
{{~/if~}} {{~/if~}}
{{=======}} {{=======}}
{{~#if (hasMedia "audio")~}} {{~#if (hasMedia "audio")~}}
[sound:{{#getMedia "audio" format="fileName"}}{{/getMedia}}] [sound:{{#getMedia "audio"}}{{/getMedia}}]
{{~/if~}} {{~/if~}}
{{>>>>>>>}} {{>>>>>>>}}
@ -44,7 +44,7 @@
<img src="{{definition.screenshotFileName}}" /> <img src="{{definition.screenshotFileName}}" />
{{=======}} {{=======}}
{{~#if (hasMedia "screenshot")~}} {{~#if (hasMedia "screenshot")~}}
<img src="{{#getMedia "screenshot" format="fileName"}}{{/getMedia}}" /> <img src="{{#getMedia "screenshot"}}{{/getMedia}}" />
{{~/if~}} {{~/if~}}
{{>>>>>>>}} {{>>>>>>>}}
@ -54,12 +54,12 @@
{{~/if~}} {{~/if~}}
{{=======}} {{=======}}
{{~#if (hasMedia "clipboardImage")~}} {{~#if (hasMedia "clipboardImage")~}}
<img src="{{#getMedia "clipboardImage" format="fileName"}}{{/getMedia}}" /> <img src="{{#getMedia "clipboardImage"}}{{/getMedia}}" />
{{~/if~}} {{~/if~}}
{{>>>>>>>}} {{>>>>>>>}}
{{<<<<<<<}} {{<<<<<<<}}
{{~#if definition.clipboardText~}}{{definition.clipboardText}}{{~/if~}} {{~#if definition.clipboardText~}}{{definition.clipboardText}}{{~/if~}}
{{=======}} {{=======}}
{{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText" format="text"}}{{/getMedia}}{{/if~}} {{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText"}}{{/getMedia}}{{/if~}}
{{>>>>>>>}} {{>>>>>>>}}

View File

@ -32,7 +32,7 @@
{{#*inline "audio"}} {{#*inline "audio"}}
{{~#if (hasMedia "audio")~}} {{~#if (hasMedia "audio")~}}
[sound:{{#getMedia "audio" format="fileName"}}{{/getMedia}}] [sound:{{#getMedia "audio"}}{{/getMedia}}]
{{~/if~}} {{~/if~}}
{{/inline}} {{/inline}}
@ -174,7 +174,7 @@
{{#*inline "screenshot"}} {{#*inline "screenshot"}}
{{~#if (hasMedia "screenshot")~}} {{~#if (hasMedia "screenshot")~}}
<img src="{{#getMedia "screenshot" format="fileName"}}{{/getMedia}}" /> <img src="{{#getMedia "screenshot"}}{{/getMedia}}" />
{{~/if~}} {{~/if~}}
{{/inline}} {{/inline}}
@ -294,12 +294,12 @@
{{#*inline "clipboard-image"}} {{#*inline "clipboard-image"}}
{{~#if (hasMedia "clipboardImage")~}} {{~#if (hasMedia "clipboardImage")~}}
<img src="{{#getMedia "clipboardImage" format="fileName"}}{{/getMedia}}" /> <img src="{{#getMedia "clipboardImage"}}{{/getMedia}}" />
{{~/if~}} {{~/if~}}
{{/inline}} {{/inline}}
{{#*inline "clipboard-text"}} {{#*inline "clipboard-text"}}
{{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText" format="text"}}{{/getMedia}}{{/if~}} {{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText"}}{{/getMedia}}{{/if~}}
{{/inline}} {{/inline}}
{{#*inline "conjugation"}} {{#*inline "conjugation"}}
@ -379,13 +379,13 @@
{{/inline}} {{/inline}}
{{#*inline "selection-text"}} {{#*inline "selection-text"}}
{{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText" format="text"}}{{/getMedia}}{{/if~}} {{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText"}}{{/getMedia}}{{/if~}}
{{/inline}} {{/inline}}
{{#*inline "sentence-furigana"}} {{#*inline "sentence-furigana"}}
{{~#if definition.cloze~}} {{~#if definition.cloze~}}
{{~#if (hasMedia "textFurigana" definition.cloze.sentence)~}} {{~#if (hasMedia "textFurigana" definition.cloze.sentence)~}}
{{#getMedia "textFurigana" definition.cloze.sentence format="html"}}{{/getMedia}} {{#getMedia "textFurigana" definition.cloze.sentence escape=false}}{{/getMedia}}
{{~else~}} {{~else~}}
{{definition.cloze.sentence}} {{definition.cloze.sentence}}
{{~/if~}} {{~/if~}}

View File

@ -362,14 +362,14 @@ class AnkiNoteBuilder {
(dictionaryMedia[dictionary]) : (dictionaryMedia[dictionary]) :
(dictionaryMedia[dictionary] = {}) (dictionaryMedia[dictionary] = {})
); );
dictionaryMedia2[path] = {fileName}; dictionaryMedia2[path] = {value: fileName};
} }
const media = { const media = {
audio: (typeof audioFileName === 'string' ? {fileName: audioFileName} : null), audio: (typeof audioFileName === 'string' ? {value: audioFileName} : null),
screenshot: (typeof screenshotFileName === 'string' ? {fileName: screenshotFileName} : null), screenshot: (typeof screenshotFileName === 'string' ? {value: screenshotFileName} : null),
clipboardImage: (typeof clipboardImageFileName === 'string' ? {fileName: clipboardImageFileName} : null), clipboardImage: (typeof clipboardImageFileName === 'string' ? {value: clipboardImageFileName} : null),
clipboardText: (typeof clipboardText === 'string' ? {text: clipboardText} : null), clipboardText: (typeof clipboardText === 'string' ? {value: clipboardText} : null),
selectionText: (typeof selectionText === 'string' ? {text: selectionText} : null), selectionText: (typeof selectionText === 'string' ? {value: selectionText} : null),
textFurigana, textFurigana,
dictionaryMedia dictionaryMedia
}; };
@ -391,8 +391,8 @@ class AnkiNoteBuilder {
break; break;
} }
if (data !== null) { if (data !== null) {
const html = this._createFuriganaHtml(data, readingMode); const value = this._createFuriganaHtml(data, readingMode);
results.push({text, readingMode, details: {html}}); results.push({text, readingMode, details: {value}});
} }
} }
return results; return results;

View File

@ -15,6 +15,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/* global
* Handlebars
*/
class TemplateRendererMediaProvider { class TemplateRendererMediaProvider {
constructor() { constructor() {
this._requirements = null; this._requirements = null;
@ -38,8 +42,7 @@ class TemplateRendererMediaProvider {
const {media} = root; const {media} = root;
const data = this._getMediaData(media, args, namedArgs); const data = this._getMediaData(media, args, namedArgs);
if (data !== null) { if (data !== null) {
const {format} = namedArgs; const result = this._getFormattedValue(data, namedArgs);
const result = this._getFormattedValue(data, format);
if (typeof result === 'string') { return result; } if (typeof result === 'string') { return result; }
} }
const defaultValue = namedArgs.default; const defaultValue = namedArgs.default;
@ -53,8 +56,13 @@ class TemplateRendererMediaProvider {
this._requirements.push(value); this._requirements.push(value);
} }
_getFormattedValue(data, format) { _getFormattedValue(data, namedArgs) {
return Object.prototype.hasOwnProperty.call(data, format) ? data[format] : null; let {value} = data;
const {escape=true} = namedArgs;
if (escape) {
value = Handlebars.Utils.escapeExpression(value);
}
return value;
} }
_getMediaData(media, args, namedArgs) { _getMediaData(media, args, namedArgs) {

View File

@ -992,24 +992,24 @@ async function testFieldTemplatesUpdate(extDir) {
expected: ` expected: `
{{#*inline "audio"}} {{#*inline "audio"}}
{{~#if (hasMedia "audio")~}} {{~#if (hasMedia "audio")~}}
[sound:{{#getMedia "audio" format="fileName"}}{{/getMedia}}] [sound:{{#getMedia "audio"}}{{/getMedia}}]
{{~/if~}} {{~/if~}}
{{/inline}} {{/inline}}
{{#*inline "screenshot"}} {{#*inline "screenshot"}}
{{~#if (hasMedia "screenshot")~}} {{~#if (hasMedia "screenshot")~}}
<img src="{{#getMedia "screenshot" format="fileName"}}{{/getMedia}}" /> <img src="{{#getMedia "screenshot"}}{{/getMedia}}" />
{{~/if~}} {{~/if~}}
{{/inline}} {{/inline}}
{{#*inline "clipboard-image"}} {{#*inline "clipboard-image"}}
{{~#if (hasMedia "clipboardImage")~}} {{~#if (hasMedia "clipboardImage")~}}
<img src="{{#getMedia "clipboardImage" format="fileName"}}{{/getMedia}}" /> <img src="{{#getMedia "clipboardImage"}}{{/getMedia}}" />
{{~/if~}} {{~/if~}}
{{/inline}} {{/inline}}
{{#*inline "clipboard-text"}} {{#*inline "clipboard-text"}}
{{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText" format="text"}}{{/getMedia}}{{/if~}} {{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText"}}{{/getMedia}}{{/if~}}
{{/inline}} {{/inline}}
<<<UPDATE-ADDITIONS>>> <<<UPDATE-ADDITIONS>>>