From 6b8cd5e623132b0697d628cd793bafb0d0b86ae0 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 26 Jun 2021 23:55:06 -0400 Subject: Template patcher updates (#1766) * Don't append anything if the additions is empty * Simplify update list --- ext/js/templates/template-patcher.js | 1 + test/test-options-util.js | 55 +++++++++++++++--------------------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/ext/js/templates/template-patcher.js b/ext/js/templates/template-patcher.js index 57178957..5658ca8a 100644 --- a/ext/js/templates/template-patcher.js +++ b/ext/js/templates/template-patcher.js @@ -77,6 +77,7 @@ class TemplatePatcher { // Private _addFieldTemplatesBeforeEnd(template, addition) { + if (addition.length === 0) { return template; } const newline = '\n'; let replaced = false; template = template.replace(this._lookupMarkerPattern, (g0) => { diff --git a/test/test-options-util.js b/test/test-options-util.js index d20eec3e..47b09b94 100644 --- a/test/test-options-util.js +++ b/test/test-options-util.js @@ -649,12 +649,23 @@ async function testFieldTemplatesUpdate(extDir) { const content = fs.readFileSync(path.join(extDir, fileName), {encoding: 'utf8'}); return templatePatcher.parsePatch(content).addition; }; - const update2 = loadDataFile('data/templates/anki-field-templates-upgrade-v2.handlebars'); - const update4 = loadDataFile('data/templates/anki-field-templates-upgrade-v4.handlebars'); - const update6 = loadDataFile('data/templates/anki-field-templates-upgrade-v6.handlebars'); - const update8 = loadDataFile('data/templates/anki-field-templates-upgrade-v8.handlebars'); - const update10 = loadDataFile('data/templates/anki-field-templates-upgrade-v10.handlebars'); - const update12 = loadDataFile('data/templates/anki-field-templates-upgrade-v12.handlebars'); + const updates = [ + {version: 2, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v2.handlebars')}, + {version: 4, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v4.handlebars')}, + {version: 6, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v6.handlebars')}, + {version: 8, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v8.handlebars')}, + {version: 10, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v10.handlebars')}, + {version: 12, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v12.handlebars')} + ]; + const getUpdateAdditions = (startVersion=0) => { + let value = ''; + for (const {version, changes} of updates) { + if (version < startVersion || changes.length === 0) { continue; } + if (value.length > 0) { value += '\n'; } + value += changes; + } + return value; + }; const data = [ // Standard format @@ -671,12 +682,7 @@ async function testFieldTemplatesUpdate(extDir) { {{~definition.character~}} {{/inline}} -${update2} -${update4} -${update6} -${update8} -${update10} -${update12} +${getUpdateAdditions()} {{~> (lookup . "marker") ~}}`.trimStart() }, // Non-standard marker format @@ -694,12 +700,7 @@ ${update12} {{/inline}} {{~> (lookup . "marker2") ~}} -${update2} -${update4} -${update6} -${update8} -${update10} -${update12}`.trimStart() +${getUpdateAdditions()}`.trimStart() }, // Empty test { @@ -707,12 +708,7 @@ ${update12}`.trimStart() {{~> (lookup . "marker") ~}}`.trimStart(), expected: ` -${update2} -${update4} -${update6} -${update8} -${update10} -${update12} +${getUpdateAdditions()} {{~> (lookup . "marker") ~}}`.trimStart() }, // Definition tags update @@ -782,12 +778,7 @@ ${update12} {{~> glossary-single definition brief=brief compactGlossaries=../compactGlossaries data=../.~}} {{/inline}} -${update2} -${update4} -${update6} -${update8} -${update10} -${update12} +${getUpdateAdditions()} {{~> (lookup . "marker") ~}} `.trimStart() }, @@ -927,9 +918,7 @@ ${update12} {{~> glossary brief=true ~}} {{/inline}} -${update8} -${update10} -${update12} +${getUpdateAdditions(7)} {{~> (lookup . "marker") ~}}`.trimStart() } ]; -- cgit v1.2.3