-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathrender.ts
More file actions
83 lines (74 loc) · 2.49 KB
/
render.ts
File metadata and controls
83 lines (74 loc) · 2.49 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
/*
* render.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { kOutputExt, kOutputFile, kOutputSuffix, kServer } from "../config/constants.ts";
import { Format, Metadata } from "../config/types.ts";
import { kJupyterEngine, kKnitrEngine } from "../execute/types.ts";
import { dirAndStem } from "./path.ts";
import { extname } from "../deno_ral/path.ts";
export function inputFilesDir(input: string) {
const [_, stem] = dirAndStem(input);
return stem + "_files";
}
// rmarkdown derived figures dir
// (https://github.com/rstudio/rmarkdown/blob/e561d8b1a2693a0975c3443f2e2d6cee475298a3/R/render.R#L633-L639)
export function figuresDir(pandocTo?: string) {
if (pandocTo === "html4") {
pandocTo = "html";
}
pandocTo = (pandocTo || "html").replace(/[\+\-].*$/, "");
return "figure-" + pandocTo;
}
export function isServerShiny(format?: Format) {
const server = format?.metadata[kServer] as Metadata | undefined;
return server?.["type"] === "shiny";
}
export function isServerShinyPython(
format: Format,
engine: string | undefined,
) {
return isServerShiny(format) && engine === kJupyterEngine;
}
export function isServerShinyKnitr(
format: Format,
engine: string | undefined,
) {
return isServerShiny(format) && engine === kKnitrEngine;
}
export function formatOutputFile(format: Format) {
let outputFile = format.pandoc[kOutputFile];
if (outputFile) {
// Apply output-suffix if specified (insert before extension)
const suffix = format.render[kOutputSuffix];
if (suffix) {
const ext = extname(outputFile);
if (ext) {
const stem = outputFile.slice(0, -ext.length);
outputFile = `${stem}${suffix}${ext}`;
} else {
// No extension, just append suffix
outputFile = `${outputFile}${suffix}`;
}
}
if (format.render[kOutputExt]) {
// Don't append the output extension if the same output
// extension is already present. If you update this logic,
// be sure to contemplate files with names like:
// file.name.ipynb
// which should result in an output file like:
// file.name.html
const existingExtension = extname(outputFile);
// If there the output file has no extension, or already has this
// extension, don't append it, otherwise add it.
if (
!existingExtension ||
existingExtension.substring(1) !== format.render[kOutputExt]
) {
outputFile = `${outputFile}.${format.render[kOutputExt]}`;
}
}
}
return outputFile;
}