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

View File

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

View File

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

View File

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