Skip to content

Commit

Permalink
Merge pull request #75 from syumai/override-include
Browse files Browse the repository at this point in the history
support overriding include func
  • Loading branch information
syumai authored Jul 24, 2021
2 parents ed1f882 + 9cb40f1 commit b5b0238
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
11 changes: 5 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ async function include(path: string, params: Params): Promise<string> {
}

function sanitize(str: string): string {
return str
.replace(/\`/g, "\\\`")
.replace(/\$/g, "\\\$")
.replace(/\\+$/, ""); // Trim backslashes at line end. TODO: Fix this to render backslashes.
return str.replace(/\`/g, "\\`").replace(/\$/g, "\\$").replace(/\\+$/, ""); // Trim backslashes at line end. TODO: Fix this to render backslashes.
}

async function bufToStr(buf: Buffer): Promise<string> {
Expand All @@ -71,8 +68,8 @@ function NewTemplate(script: string): Template {
const output: Array<string> = [];
await new Promise((resolve, reject) => {
const args = {
...params,
include,
...params,
$$OUTPUT: output,
$$FINISHED: resolve,
$$ERROR: reject,
Expand Down Expand Up @@ -156,7 +153,9 @@ export async function compile(reader: Reader): Promise<Template> {
case ReadMode.Escaped:
statements.push(
`;$$OUTPUT.push($$ESCAPE(${
removeLastSemi(await bufToStr(statementBuf))
removeLastSemi(
await bufToStr(statementBuf),
)
}));`,
);
break;
Expand Down
18 changes: 18 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,21 @@ const decoder = new TextDecoder("utf-8");
});
}
})();

Deno.test({
name: "override include",
fn: async () => {
const buf = new Buffer();
const overriddenInclude = () => "overridden";
await copy(
await dejs.renderFile(`${cwd()}/testdata/raw-include.ejs`, {
param: {},
include: overriddenInclude,
}),
buf,
);
const actual = decoder.decode(await readAll(buf));
const expected = "overridden";
assertEquals(actual, expected);
},
});

0 comments on commit b5b0238

Please sign in to comment.