Skip to content

Commit f951dde

Browse files
script to copy typst partials to create extension
and add preamble
1 parent 1fd644a commit f951dde

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tools/copy-typst-partials.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as path from 'stdlib/path';
2+
3+
const srcTemplate = path.parse('../src/resources/formats/typst/pandoc/quarto/typst-template.typ');
4+
const destTemplate = path.parse('../src/resources/create/extensions/format-typst/_extensions/qstart-filesafename-qend/typst-template.typ');
5+
const srcShow = path.parse('../src/resources/formats/typst/pandoc/quarto/typst-show.typ');
6+
const destShow = path.parse('../src/resources/create/extensions/format-typst/_extensions/qstart-filesafename-qend/typst-show.typ');
7+
8+
const templatePreamble = `
9+
// This is an example typst template (based on the default template that ships
10+
// with Quarto). It defines a typst function named 'article' which provides
11+
// various customization options. This function is called from the
12+
// 'typst-show.typ' file (which maps Pandoc metadata function arguments)
13+
//
14+
// If you are creating or packaging a custom typst template you will likely
15+
// want to replace this file and 'typst-show.typ' entirely. You can find
16+
// documentation on creating typst templates and some examples here:
17+
// - https://typst.app/docs/tutorial/making-a-template/
18+
// - https://github.com/typst/templates
19+
`;
20+
21+
const showPreamble = `
22+
// Typst custom formats typically consist of a 'typst-template.typ' (which is
23+
// the source code for a typst template) and a 'typst-show.typ' which calls the
24+
// template's function (forwarding Pandoc metadata values as required)
25+
//
26+
// This is an example 'typst-show.typ' file (based on the default template
27+
// that ships with Quarto). It calls the typst function named 'article' which
28+
// is defined in the 'typst-template.typ' file.
29+
//
30+
// If you are creating or packaging a custom typst template you will likely
31+
// want to replace this file and 'typst-template.typ' entirely. You can find
32+
// documentation on creating typst templates here and some examples here:
33+
// - https://typst.app/docs/tutorial/making-a-template/
34+
// - https://github.com/typst/templates
35+
`;
36+
37+
const encoder = new TextEncoder(), decoder = new TextDecoder();
38+
const scriptDir = import.meta.dirname;
39+
40+
async function splicePartial(preamble : string, source : string, dest : string) {
41+
const template = await Deno.readFile(path.join(scriptDir, path.format(source)));
42+
const templateOut = preamble + decoder.decode(template);
43+
await Deno.writeFile(path.join(scriptDir, path.format(dest)), encoder.encode(templateOut));
44+
}
45+
46+
await splicePartial(templatePreamble, srcTemplate, destTemplate);
47+
await splicePartial(showPreamble, srcShow, destShow);

0 commit comments

Comments
 (0)