Commit 45a2e357 authored by Diegodlh's avatar Diegodlh
Browse files

add optional "onlyApplicable" parameter to TemplateConfiguration's translateWith method

parent a08f5d0a
......@@ -86,22 +86,16 @@ export class Domain {
}
// translate target with templates for paths returned above
let templateOutputs = await this.templates.translateWith(
const templateOutputs = await this.templates.translateWith(
target,
templatePaths,
{
tryAllTemplates: options.allTemplates,
useFallback: options.forceTemplatePaths === undefined,
onlyApplicable: options.onlyApplicable,
}
);
// parse output with onlyApplicable option
if (options.onlyApplicable) {
templateOutputs = templateOutputs.filter(
(templateOutput) => templateOutput.applicable
);
}
let baseCitation: MediaWikiBaseFieldCitation | undefined;
if (options.fillWithCitoid) {
// baseCitation = (await target.cache.citoid.getData()).citation
......
......@@ -110,13 +110,22 @@ describe("Use an applicable template", () => {
expect(output.applicable).toBe(true);
});
it("skips non-applicable templates", async () => {
it("skips non-applicable templates by default", async () => {
const output = (
await configuration.translateWith(target, paths)
)[0] as TemplateOutput;
expect(output.template.label).toBe("second template");
});
it("optionally returns non-applicable template outputs", async () => {
const outputs = (await configuration.translateWith(target, paths, {
onlyApplicable: false,
})) as TemplateOutput[];
expect(outputs.length).toBe(2);
expect(outputs[0].applicable).toBe(false);
expect(outputs[1].applicable).toBe(true);
});
it("outputs the expected results", async () => {
const output = (
await configuration.translateWith(target, paths)
......
......@@ -183,7 +183,12 @@ export class TemplateConfiguration extends DomainConfiguration<
async translateWith(
target: Webpage,
paths: string[],
{ useFallback = true, preferSamePath = true, tryAllTemplates = false } = {}
{
useFallback = true,
preferSamePath = true,
tryAllTemplates = false,
onlyApplicable = true,
} = {}
): Promise<TemplateOutput[]> {
const templates: BaseTranslationTemplate[] = this.get(paths);
if (preferSamePath) {
......@@ -211,8 +216,12 @@ export class TemplateConfiguration extends DomainConfiguration<
for (const template of templates) {
// todo: catch errors?
const output = await template.translate(target);
outputs.push(output);
if (output.applicable) break;
if (output.applicable) {
outputs.push(output);
break;
} else if (!onlyApplicable) {
outputs.push(output);
}
}
}
return outputs;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment