Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/smoke/manuscript/manuscript.ts
6434 views
1
/*
2
* site.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
import { existsSync } from "../../../src/deno_ral/fs.ts";
7
import { basename, dirname, extname, join } from "../../../src/deno_ral/path.ts";
8
import { testQuartoCmd, Verify } from "../../test.ts";
9
import { docs } from "../../utils.ts";
10
import {
11
ensureMECAValidates,
12
ensureXmlValidatesWithXsd,
13
fileExists,
14
noErrorsOrWarnings,
15
} from "../../verify.ts";
16
import { dirAndStem } from "../../../src/core/path.ts";
17
18
export type targetFormat = "html" | "jats" | "docx" | "pdf";
19
const enableJatsValidation = true;
20
21
export const testManuscriptRender = (
22
article: string,
23
to: "all" | targetFormat,
24
formats: targetFormat[],
25
expectedOutputs: string[],
26
articleVerify: Verify[] = [],
27
) => {
28
const articleDir = dirname(article);
29
30
const verifications: Verify[] = [];
31
const articleOuts = formats.map((fmt) => {
32
const output = manuscriptOutputForInput(article, fmt);
33
if (enableJatsValidation && fmt === "jats") {
34
const xsdPath = docs(
35
join("jats", "xsd", "JATS-Archiving-1-2-MathML2-DTD"),
36
);
37
38
// Test a basic JATS document that tests a variety of elements
39
verifications.push(ensureXmlValidatesWithXsd(output.outputPath, xsdPath));
40
41
// Validate the MECA file as well
42
const [dir, stem] = dirAndStem(output.outputPath);
43
const mecaFile = join(dir, `${stem}-meca.zip`);
44
45
verifications.push(ensureMECAValidates(mecaFile));
46
}
47
return output;
48
});
49
50
articleOuts.forEach((out) => {
51
verifications.push(fileExists(out.outputPath));
52
});
53
54
expectedOutputs.forEach((out) => {
55
verifications.push(fileExists(join(articleDir, "_manuscript", out)));
56
});
57
58
59
// Render the manuscript
60
testQuartoCmd(
61
"render",
62
[articleDir, "--to", to],
63
[noErrorsOrWarnings, ...verifications, ...articleVerify],
64
{
65
teardown: () => {
66
articleOuts.forEach((out) => {
67
if (existsSync(out.manuscriptDir)) {
68
Deno.removeSync(out.manuscriptDir, { recursive: true });
69
}
70
});
71
return Promise.resolve();
72
},
73
},
74
);
75
};
76
77
function manuscriptOutputForInput(
78
input: string,
79
format: targetFormat,
80
) {
81
const inputStem = basename(input, extname(input));
82
let ext = "html";
83
let stem = inputStem;
84
85
let supporting = true;
86
if (format === "jats") {
87
ext = "xml";
88
} else if (format === "pdf") {
89
ext = "pdf";
90
supporting = false;
91
} else if (format === "docx") {
92
ext = "docx";
93
supporting = false;
94
} else if (format === "html") {
95
stem = "index";
96
}
97
98
const dir = join(dirname(input), "_manuscript");
99
100
const outputPath = join(dir, `${stem}.${ext}`);
101
const supportPath = join(dir, `${stem}_files`);
102
103
return {
104
outputPath,
105
supportPath: supporting ? supportPath : undefined,
106
manuscriptDir: dir,
107
};
108
}
109
110