aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpraschke <stel@comfy.monster>2023-10-17 21:52:31 +0100
committerpraschke <stel@comfy.monster>2023-10-17 21:52:31 +0100
commit376a1b096874e601296321fa6307836e2736a05c (patch)
tree5ef7fa6f35f90de6aec2ed5e8091ab28fdba30f8
parentae91e2442d7b3746c633871ff956a8addd7046b5 (diff)
fix: formatGlossary
-rw-r--r--docs/templates.md4
-rw-r--r--ext/data/templates/anki-field-templates-upgrade-v21.handlebars18
-rw-r--r--ext/data/templates/default-anki-field-templates.handlebars6
-rw-r--r--ext/js/templates/sandbox/anki-template-renderer.js5
-rw-r--r--test/test-options-util.js74
5 files changed, 99 insertions, 8 deletions
diff --git a/docs/templates.md b/docs/templates.md
index bf5bd897..ce1e8d7e 100644
--- a/docs/templates.md
+++ b/docs/templates.md
@@ -657,7 +657,7 @@ structured-content generation.
<details>
<summary>Syntax:</summary>
- <code>{{#formatGlossary <i>dictionary</i>}}{{{definitionEntry}}}{{/pitchCategories}}</code><br>
+ <code>{{formatGlossary <i>dictionary</i> <i>definitionEntry</i>}}</code><br>
* _`dictionary`_ <br>
The dictionary that the glossary entry belongs to.
@@ -668,7 +668,7 @@ structured-content generation.
<summary>Example:</summary>
```handlebars
- {{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{/each}}
+ {{#each glossary}}{{formatGlossary ../dictionary .}}{{/each}}
```
Output:
diff --git a/ext/data/templates/anki-field-templates-upgrade-v21.handlebars b/ext/data/templates/anki-field-templates-upgrade-v21.handlebars
index 92034995..99db50b6 100644
--- a/ext/data/templates/anki-field-templates-upgrade-v21.handlebars
+++ b/ext/data/templates/anki-field-templates-upgrade-v21.handlebars
@@ -1,5 +1,23 @@
{{<<<<<<<}}
+{{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{/each}}
+{{=======}}
+{{#each glossary}}{{formatGlossary ../dictionary .}}{{/each}}
+{{>>>>>>>}}
+
+{{<<<<<<<}}
+{{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{#unless @last}} | {{/unless}}{{/each}}
+{{=======}}
+{{#each glossary}}{{formatGlossary ../dictionary .}}{{#unless @last}} | {{/unless}}{{/each}}
+{{>>>>>>>}}
+
+{{<<<<<<<}}
+<ul>{{#each glossary}}<li>{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}</li>{{/each}}</ul>
+{{=======}}
+<ul>{{#each glossary}}<li>{{formatGlossary ../dictionary .}}</li>{{/each}}</ul>
+{{>>>>>>>}}
+
+{{<<<<<<<}}
<span class="expression-{{termFrequency}}">{{~#furigana}}{{{.}}}{{/furigana~}}</span>
{{=======}}
<span class="expression-{{termFrequency}}">{{~furigana .~}}</span>
diff --git a/ext/data/templates/default-anki-field-templates.handlebars b/ext/data/templates/default-anki-field-templates.handlebars
index 751236f4..58bc68e2 100644
--- a/ext/data/templates/default-anki-field-templates.handlebars
+++ b/ext/data/templates/default-anki-field-templates.handlebars
@@ -21,11 +21,11 @@
{{~#if only~}}({{#each only}}{{.}}{{#unless @last}}, {{/unless}}{{/each}} only) {{/if~}}
{{~/unless~}}
{{~#if (op "<=" glossary.length 1)~}}
- {{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{/each}}
+ {{#each glossary}}{{formatGlossary ../dictionary .}}{{/each}}
{{~else if @root.compactGlossaries~}}
- {{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{#unless @last}} | {{/unless}}{{/each}}
+ {{#each glossary}}{{formatGlossary ../dictionary .}}{{#unless @last}} | {{/unless}}{{/each}}
{{~else~}}
- <ul>{{#each glossary}}<li>{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}</li>{{/each}}</ul>
+ <ul>{{#each glossary}}<li>{{formatGlossary ../dictionary .}}</li>{{/each}}</ul>
{{~/if~}}
{{~#set "previousDictionary" dictionary~}}{{~/set~}}
{{/inline}}
diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js
index e15bf895..62bf71b8 100644
--- a/ext/js/templates/sandbox/anki-template-renderer.js
+++ b/ext/js/templates/sandbox/anki-template-renderer.js
@@ -497,7 +497,7 @@ class AnkiTemplateRenderer {
this._normalizeHtml(container, styleApplier, datasetKeyIgnorePattern);
const result = container.innerHTML;
container.textContent = '';
- return result;
+ return this._safeString(result);
}
_normalizeHtml(root, styleApplier, datasetKeyIgnorePattern) {
@@ -550,9 +550,8 @@ class AnkiTemplateRenderer {
return instance;
}
- _formatGlossary(context, dictionary, options) {
+ _formatGlossary(context, dictionary, content, options) {
const data = options.data.root;
- const content = options.fn(context);
if (typeof content === 'string') { return this._stringToMultiLineHtml(this._escape(content)); }
if (!(typeof content === 'object' && content !== null)) { return ''; }
switch (content.type) {
diff --git a/test/test-options-util.js b/test/test-options-util.js
index e9504b0d..c2780650 100644
--- a/test/test-options-util.js
+++ b/test/test-options-util.js
@@ -1304,6 +1304,80 @@ async function testFieldTemplatesUpdate(extDir) {
{{/inline}}
{{~> (lookup . "marker") ~}}`.trimStart()
+ },
+ // block helper update: formatGlossary
+ {
+ oldVersion: 20,
+ newVersion: 21,
+ old: `
+{{#*inline "glossary-single"}}
+ {{~#unless brief~}}
+ {{~#scope~}}
+ {{~#set "any" false}}{{/set~}}
+ {{~#each definitionTags~}}
+ {{~#if (op "||" (op "!" @root.compactTags) (op "!" redundant))~}}
+ {{~#if (get "any")}}, {{else}}<i>({{/if~}}
+ {{name}}
+ {{~#set "any" true}}{{/set~}}
+ {{~/if~}}
+ {{~/each~}}
+ {{~#unless noDictionaryTag~}}
+ {{~#if (op "||" (op "!" @root.compactTags) (op "!==" dictionary (get "previousDictionary")))~}}
+ {{~#if (get "any")}}, {{else}}<i>({{/if~}}
+ {{dictionary}}
+ {{~#set "any" true}}{{/set~}}
+ {{~/if~}}
+ {{~/unless~}}
+ {{~#if (get "any")}})</i> {{/if~}}
+ {{~/scope~}}
+ {{~#if only~}}({{#each only}}{{.}}{{#unless @last}}, {{/unless}}{{/each}} only) {{/if~}}
+ {{~/unless~}}
+ {{~#if (op "<=" glossary.length 1)~}}
+ {{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{/each}}
+ {{~else if @root.compactGlossaries~}}
+ {{#each glossary}}{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}{{#unless @last}} | {{/unless}}{{/each}}
+ {{~else~}}
+ <ul>{{#each glossary}}<li>{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}</li>{{/each}}</ul>
+ {{~/if~}}
+ {{~#set "previousDictionary" dictionary~}}{{~/set~}}
+{{/inline}}
+
+{{~> (lookup . "marker") ~}}`.trimStart(),
+
+ expected: `
+{{#*inline "glossary-single"}}
+ {{~#unless brief~}}
+ {{~#scope~}}
+ {{~#set "any" false}}{{/set~}}
+ {{~#each definitionTags~}}
+ {{~#if (op "||" (op "!" @root.compactTags) (op "!" redundant))~}}
+ {{~#if (get "any")}}, {{else}}<i>({{/if~}}
+ {{name}}
+ {{~#set "any" true}}{{/set~}}
+ {{~/if~}}
+ {{~/each~}}
+ {{~#unless noDictionaryTag~}}
+ {{~#if (op "||" (op "!" @root.compactTags) (op "!==" dictionary (get "previousDictionary")))~}}
+ {{~#if (get "any")}}, {{else}}<i>({{/if~}}
+ {{dictionary}}
+ {{~#set "any" true}}{{/set~}}
+ {{~/if~}}
+ {{~/unless~}}
+ {{~#if (get "any")}})</i> {{/if~}}
+ {{~/scope~}}
+ {{~#if only~}}({{#each only}}{{.}}{{#unless @last}}, {{/unless}}{{/each}} only) {{/if~}}
+ {{~/unless~}}
+ {{~#if (op "<=" glossary.length 1)~}}
+ {{#each glossary}}{{formatGlossary ../dictionary .}}{{/each}}
+ {{~else if @root.compactGlossaries~}}
+ {{#each glossary}}{{formatGlossary ../dictionary .}}{{#unless @last}} | {{/unless}}{{/each}}
+ {{~else~}}
+ <ul>{{#each glossary}}<li>{{formatGlossary ../dictionary .}}</li>{{/each}}</ul>
+ {{~/if~}}
+ {{~#set "previousDictionary" dictionary~}}{{~/set~}}
+{{/inline}}
+
+{{~> (lookup . "marker") ~}}`.trimStart()
}
];