Path: blob/main/tests/smoke/manuscript/manuscript.ts
6434 views
/*1* site.ts2*3* Copyright (C) 2020-2022 Posit Software, PBC4*/5import { existsSync } from "../../../src/deno_ral/fs.ts";6import { basename, dirname, extname, join } from "../../../src/deno_ral/path.ts";7import { testQuartoCmd, Verify } from "../../test.ts";8import { docs } from "../../utils.ts";9import {10ensureMECAValidates,11ensureXmlValidatesWithXsd,12fileExists,13noErrorsOrWarnings,14} from "../../verify.ts";15import { dirAndStem } from "../../../src/core/path.ts";1617export type targetFormat = "html" | "jats" | "docx" | "pdf";18const enableJatsValidation = true;1920export const testManuscriptRender = (21article: string,22to: "all" | targetFormat,23formats: targetFormat[],24expectedOutputs: string[],25articleVerify: Verify[] = [],26) => {27const articleDir = dirname(article);2829const verifications: Verify[] = [];30const articleOuts = formats.map((fmt) => {31const output = manuscriptOutputForInput(article, fmt);32if (enableJatsValidation && fmt === "jats") {33const xsdPath = docs(34join("jats", "xsd", "JATS-Archiving-1-2-MathML2-DTD"),35);3637// Test a basic JATS document that tests a variety of elements38verifications.push(ensureXmlValidatesWithXsd(output.outputPath, xsdPath));3940// Validate the MECA file as well41const [dir, stem] = dirAndStem(output.outputPath);42const mecaFile = join(dir, `${stem}-meca.zip`);4344verifications.push(ensureMECAValidates(mecaFile));45}46return output;47});4849articleOuts.forEach((out) => {50verifications.push(fileExists(out.outputPath));51});5253expectedOutputs.forEach((out) => {54verifications.push(fileExists(join(articleDir, "_manuscript", out)));55});565758// Render the manuscript59testQuartoCmd(60"render",61[articleDir, "--to", to],62[noErrorsOrWarnings, ...verifications, ...articleVerify],63{64teardown: () => {65articleOuts.forEach((out) => {66if (existsSync(out.manuscriptDir)) {67Deno.removeSync(out.manuscriptDir, { recursive: true });68}69});70return Promise.resolve();71},72},73);74};7576function manuscriptOutputForInput(77input: string,78format: targetFormat,79) {80const inputStem = basename(input, extname(input));81let ext = "html";82let stem = inputStem;8384let supporting = true;85if (format === "jats") {86ext = "xml";87} else if (format === "pdf") {88ext = "pdf";89supporting = false;90} else if (format === "docx") {91ext = "docx";92supporting = false;93} else if (format === "html") {94stem = "index";95}9697const dir = join(dirname(input), "_manuscript");9899const outputPath = join(dir, `${stem}.${ext}`);100const supportPath = join(dir, `${stem}_files`);101102return {103outputPath,104supportPath: supporting ? supportPath : undefined,105manuscriptDir: dir,106};107}108109110