-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathoutput-typst.ts
More file actions
165 lines (150 loc) · 4.74 KB
/
output-typst.ts
File metadata and controls
165 lines (150 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* output-typst.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import {
dirname,
isAbsolute,
join,
normalize,
relative,
} from "../../deno_ral/path.ts";
import { ensureDirSync, safeRemoveSync } from "../../deno_ral/fs.ts";
import {
kFontPaths,
kKeepTyp,
kOutputExt,
kOutputFile,
kVariant,
} from "../../config/constants.ts";
import { Format } from "../../config/types.ts";
import { writeFileToStdout } from "../../core/console.ts";
import { dirAndStem, expandPath } from "../../core/path.ts";
import { kStdOut, replacePandocOutputArg } from "./flags.ts";
import { OutputRecipe, RenderOptions } from "./types.ts";
import { normalizeOutputPath } from "./output-shared.ts";
import {
typstCompile,
TypstCompileOptions,
validateRequiredTypstVersion,
} from "../../core/typst.ts";
import { asArray } from "../../core/array.ts";
import { ProjectContext } from "../../project/types.ts";
export function useTypstPdfOutputRecipe(
format: Format,
) {
return format.pandoc.to === "typst" &&
format.render[kOutputExt] === "pdf";
}
export function typstPdfOutputRecipe(
input: string,
finalOutput: string,
options: RenderOptions,
format: Format,
project?: ProjectContext,
): OutputRecipe {
// calculate output and args for pandoc (this is an intermediate file
// which we will then compile to a pdf and rename to .typ)
const [inputDir, inputStem] = dirAndStem(input);
const output = inputStem + ".typ";
let args = options.pandocArgs || [];
const pandoc = { ...format.pandoc };
if (options.flags?.output) {
args = replacePandocOutputArg(args, output);
} else {
pandoc[kOutputFile] = output;
}
// when pandoc is done, we need to run the pdf generator and then copy the
// output to the user's requested destination
const complete = async () => {
// input file is pandoc's output
const input = join(inputDir, output);
// run typst
await validateRequiredTypstVersion();
const pdfOutput = join(inputDir, inputStem + ".pdf");
// Resolve extension font paths to absolute paths since typst compile runs
// from the project root, not the input file's directory. Extension paths
// (containing _extensions or starting with ..) need resolution relative to
// inputDir, while other paths (like .quarto/font-cache) should remain
// relative to the working directory.
const fontPaths = asArray(format.metadata?.[kFontPaths]).map((p) => {
const fontPath = p as string;
if (isAbsolute(fontPath)) {
return fontPath;
}
// Extension-resolved paths need to be resolved relative to input file
if (fontPath.includes("_extensions") || fontPath.startsWith("..")) {
return join(inputDir, fontPath);
}
// Other relative paths (like .quarto/...) stay relative to working dir
return fontPath;
});
const typstOptions: TypstCompileOptions = {
quiet: options.flags?.quiet,
fontPaths,
};
if (project?.dir) {
typstOptions.rootDir = project.dir;
}
const result = await typstCompile(
input,
pdfOutput,
typstOptions,
);
if (!result.success) {
throw new Error();
}
// keep typ if requested
if (!format.render[kKeepTyp]) {
safeRemoveSync(input);
}
// copy (or write for stdout) compiled pdf to final output location
if (finalOutput) {
if (finalOutput === kStdOut) {
writeFileToStdout(pdfOutput);
safeRemoveSync(pdfOutput);
} else {
const outputPdf = expandPath(finalOutput);
if (normalize(pdfOutput) !== normalize(outputPdf)) {
// ensure the target directory exists
ensureDirSync(dirname(outputPdf));
Deno.renameSync(pdfOutput, outputPdf);
}
}
// final output needs to either absolute or input dir relative
// (however it may be working dir relative when it is passed in)
return normalizeOutputPath(input, finalOutput);
} else {
return normalizeOutputPath(input, pdfOutput);
}
};
const pdfOutput = finalOutput
? finalOutput === kStdOut
? undefined
: normalizeOutputPath(input, finalOutput)
: normalizeOutputPath(input, join(inputDir, inputStem + ".pdf"));
// return recipe
const recipe: OutputRecipe = {
output,
keepYaml: false,
args,
format: { ...format, pandoc },
complete,
finalOutput: pdfOutput ? relative(inputDir, pdfOutput) : undefined,
};
// if we have some variant declared, resolve it
// (use for opt-out citations extension)
if (format.render?.[kVariant]) {
const to = format.pandoc.to;
const variant = format.render[kVariant];
recipe.format = {
...recipe.format,
pandoc: {
...recipe.format.pandoc,
to: `${to}${variant}`,
},
};
}
return recipe;
}