-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathutils.ts
More file actions
201 lines (182 loc) · 5.77 KB
/
utils.ts
File metadata and controls
201 lines (182 loc) · 5.77 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* utils.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*
*/
import { basename, dirname, extname, join, relative } from "../src/deno_ral/path.ts";
import { parseFormatString } from "../src/core/pandoc/pandoc-formats.ts";
import { kMetadataFormat, kOutputExt } from "../src/config/constants.ts";
import { pathWithForwardSlashes, safeExistsSync } from "../src/core/path.ts";
import { readYaml } from "../src/core/yaml.ts";
import { isWindows } from "../src/deno_ral/platform.ts";
// caller is responsible for cleanup!
export function inTempDirectory(fn: (dir: string) => unknown): unknown {
const dir = Deno.makeTempDirSync();
return fn(dir);
}
// Find a _quarto.yaml file in the directory hierarchy of the input file
export function findProjectDir(input: string, until?: RegExp | undefined): string | undefined {
let dir = dirname(input);
// This is used for smoke-all tests and should stop there
// to avoid side effect of _quarto.yml outside of Quarto tests folders
while (dir !== "" && dir !== "." && (until ? !until.test(pathWithForwardSlashes(dir)) : true)) {
const filename = ["_quarto.yml", "_quarto.yaml"].find((file) => {
const yamlPath = join(dir, file);
if (safeExistsSync(yamlPath)) {
return true;
}
});
if (filename) {
return dir;
}
const newDir = dirname(dir); // stops at the root for both Windows and Posix
if (newDir === dir) {
return;
}
dir = newDir;
}
}
export function findProjectOutputDir(projectdir: string | undefined) {
if (!projectdir) {
return;
}
const yaml = readYaml(join(projectdir, "_quarto.yml"));
let type = undefined;
try {
// deno-lint-ignore no-explicit-any
type = ((yaml as any).project as any).type;
} catch (error) {
throw new Error("Failed to read quarto project YAML" + String(error));
}
if (type === "book") {
return "_book";
}
if (type === "website") {
return (yaml as any)?.project?.["output-dir"] || "_site";
}
if (type === "manuscript") {
return (yaml as any)?.project?.["output-dir"] || "_manuscript";
}
// type default explicit or just unset
return (yaml as any)?.project?.["output-dir"] || "";
}
// Gets output that should be created for this input file and target format
export function outputForInput(
input: string,
to: string,
projectOutDir?: string,
projectRoot?: string,
// deno-lint-ignore no-explicit-any
metadata?: Record<string, any>,
) {
// TODO: Consider improving this (e.g. for cases like Beamer, or typst)
projectRoot = projectRoot ?? findProjectDir(input);
projectOutDir = projectOutDir ?? findProjectOutputDir(projectRoot);
const dir = projectRoot ? relative(projectRoot, dirname(input)) : dirname(input);
let stem = basename(input, extname(input));
let ext = metadata?.[kMetadataFormat]?.[to]?.[kOutputExt];
// TODO: there's a bug where output-ext keys from a custom format are
// not recognized (specifically this happens for confluence)
//
// we hack it here for the time being.
//
if (to === "confluence-publish") {
ext = "xml";
}
if (to === "docusaurus-md") {
ext = "mdx";
}
const formatDesc = parseFormatString(to);
const baseFormat = formatDesc.baseFormat;
if (formatDesc.baseFormat === "pdf") {
stem = `${stem}${formatDesc.variants.join("")}${
formatDesc.modifiers.join("")
}`;
}
let outputExt;
if (ext) {
outputExt = ext
} else {
outputExt = baseFormat || "html";
if (baseFormat === "latex" || baseFormat == "context") {
outputExt = "tex";
}
if (baseFormat === "beamer") {
outputExt = "pdf";
}
if (baseFormat === "revealjs") {
outputExt = "html";
}
if (["commonmark", "gfm", "markdown", "markdown_strict"].some((f) => f === baseFormat)) {
outputExt = "md";
}
if (baseFormat === "csljson") {
outputExt = "csl";
}
if (baseFormat === "bibtex" || baseFormat === "biblatex") {
outputExt = "bib";
}
if (baseFormat === "jats") {
outputExt = "xml";
}
if (baseFormat === "asciidoc") {
outputExt = "adoc";
}
if (baseFormat === "typst") {
outputExt = "pdf";
}
if (baseFormat === "dashboard") {
outputExt = "html";
}
if (baseFormat === "email") {
outputExt = "html";
}
}
const outputPath: string = projectRoot && projectOutDir !== undefined
? join(projectRoot, projectOutDir, dir, `${stem}.${outputExt}`)
: projectOutDir !== undefined
? join(projectOutDir, dir, `${stem}.${outputExt}`)
: join(dir, `${stem}.${outputExt}`);
const supportPath: string = projectRoot && projectOutDir !== undefined
? join(projectRoot, projectOutDir, dir, `${stem}_files`)
: projectOutDir !== undefined
? join(projectOutDir, dir, `${stem}_files`)
: join(dir, `${stem}_files`);
return {
outputPath,
supportPath,
};
}
export function projectOutputForInput(input: string) {
const projectDir = findProjectDir(input);
const projectOutDir = findProjectOutputDir(projectDir);
if (!projectDir) {
throw new Error("No project directory found");
}
const dir = join(projectDir, projectOutDir, relative(projectDir, dirname(input)));
const stem = basename(input, extname(input));
const outputPath = join(dir, `${stem}.html`);
const supportPath = join(dir, `site_libs`);
return {
outputPath,
supportPath,
};
}
export function docs(path: string): string {
return join("docs", path);
}
export function fileLoader(...path: string[]) {
return (file: string, to: string) => {
const input = docs(join(...path, file));
const output = outputForInput(input, to);
return {
input,
output,
};
};
}
// On Windows, `quarto.cmd` needs to be explicit in `execProcess()`
export function quartoDevCmd(): string {
return isWindows ? "quarto.cmd" : "quarto";
}