import {
kDefaultImageExtension,
kIncludeAfterBody,
kLinkCitations,
kNotebookSubarticles,
kQuartoInternal,
kResources,
kVariant,
} from "../../config/constants.ts";
import { Format, Metadata, PandocFlags } from "../../config/types.ts";
import { ProjectContext } from "../../project/types.ts";
import { createFormat } from "../formats-shared.ts";
import { formatResourcePath } from "../../core/resources.ts";
import { RenderServices } from "../../command/render/types.ts";
import {
jatsDtd,
JatsRenderSubArticle,
jatsTagset,
kJatsSubarticle,
kLintXml,
kSubArticles,
xmlPlaceholder,
} from "./format-jats-types.ts";
import { join } from "../../deno_ral/path.ts";
import { warning } from "../../deno_ral/log.ts";
import {
reformatXmlPostProcessor,
renderSubarticlePostProcessor,
} from "./format-jats-postprocess.ts";
const kJatsExtended = "jats-extended";
const kJatsDtd = "jats-dtd";
const kElementsVariant = "+element_citations";
export function jatsFormat(displayName: string, ext: string): Format {
return createFormat(displayName, ext, {
pandoc: {
standalone: true,
[kDefaultImageExtension]: "png",
},
render: {
[kVariant]: kElementsVariant,
},
formatExtras: (
input: string,
_markdown: string,
_flags: PandocFlags,
format: Format,
_libDir: string,
services: RenderServices,
_offset: string | undefined,
project: ProjectContext,
quiet?: boolean,
) => {
const templateDir = formatResourcePath("jats", "pandoc");
const partials = [
"front.xml",
"authors.xml",
"institution.xml",
"affiliation.xml",
"name.xml",
];
const templateContext = {
template: join(templateDir, "template.xml"),
partials: partials.map((partial) => join(templateDir, partial)),
};
const tagset = jatsTagset(format.identifier["base-format"] || "jats");
const metadataOverride = {
[kLinkCitations]: true,
};
if (format.metadata[kLinkCitations] === false) {
warning(
"JATS formats require that `link-citations` is enabled to emit well formed JATS. Explicitly set value is being ignored.",
);
}
const postprocessors = [];
const internalMetadata = format.metadata[kQuartoInternal] as
| Metadata
| undefined;
const afterBody: string[] = [];
const subArticleResources: string[] = [];
if (internalMetadata && !format.metadata[kJatsSubarticle]) {
const subArticles = (internalMetadata[
kSubArticles
]) as Array<JatsRenderSubArticle> | undefined;
if (subArticles && format.render[kNotebookSubarticles] !== false) {
const placeholderFile = services.temp.createFile({
suffix: ".placeholder.xml",
});
const placeholders: string[] = [];
subArticles.forEach((subArticle) => {
const placeholder = xmlPlaceholder(
subArticle.token,
subArticle.input,
);
placeholders.push(placeholder);
});
Deno.writeTextFileSync(placeholderFile, placeholders.join("\n"));
afterBody.push(placeholderFile);
if (subArticles.length > 0 && !format.metadata[kJatsSubarticle]) {
postprocessors.push(
renderSubarticlePostProcessor(
input,
format,
subArticles,
services,
project,
quiet,
),
);
}
}
}
if (format.metadata[kLintXml] !== false) {
postprocessors.push(reformatXmlPostProcessor);
}
return {
[kIncludeAfterBody]: afterBody,
metadata: {
[kQuartoInternal]: {
[kJatsExtended]: tagset === "archiving" || tagset === "publishing",
[kJatsDtd]: jatsDtd(tagset),
},
[kResources]: subArticleResources,
},
templateContext,
metadataOverride,
postprocessors,
};
},
});
}