Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/render/notebook/notebook-contributor-jats.ts
6474 views
1
/*
2
* notebook-contributor-jats.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { renderFile } from "../../command/render/render-files.ts";
8
import {
9
ExecutedFile,
10
RenderedFile,
11
RenderServices,
12
} from "../../command/render/types.ts";
13
import {
14
kClearCellOptions,
15
kClearHiddenClasses,
16
kIpynbProduceSourceNotebook,
17
kJatsSubarticleId,
18
kKeepHidden,
19
kNotebookPreserveCells,
20
kOutputFile,
21
kRemoveHidden,
22
kTemplate,
23
kTo,
24
kVariant,
25
} from "../../config/constants.ts";
26
import { InternalError } from "../../core/lib/error.ts";
27
import { dirAndStem } from "../../core/path.ts";
28
import {
29
kJatsSubarticle,
30
kLintXml,
31
} from "../../format/jats/format-jats-types.ts";
32
import { subarticleTemplatePath } from "../../format/jats/format-jats-paths.ts";
33
import { ProjectContext } from "../../project/types.ts";
34
import { NotebookContributor, NotebookMetadata } from "./notebook-types.ts";
35
import { error } from "../../deno_ral/log.ts";
36
import { Format } from "../../config/types.ts";
37
import { safeCloneDeep } from "../../core/safe-clone-deep.ts";
38
39
export const jatsContributor: NotebookContributor = {
40
resolve: resolveJats,
41
render: renderJats,
42
outputFile,
43
};
44
45
function outputFile(
46
nbAbsPath: string,
47
): string {
48
return jatsOutputFile(nbAbsPath);
49
}
50
51
function resolveJats(
52
nbAbsPath: string,
53
token: string,
54
executedFile: ExecutedFile,
55
_notebookMetadata?: NotebookMetadata,
56
) {
57
const resolved = safeCloneDeep(executedFile);
58
const to =
59
resolved.recipe.format.render[kVariant]?.includes("+element_citations")
60
? "jats+element_citations"
61
: "jats";
62
63
resolved.recipe.format.metadata[kLintXml] = false;
64
resolved.recipe.format.metadata[kJatsSubarticle] = true;
65
resolved.recipe.format.metadata[kJatsSubarticleId] = token;
66
resolved.recipe.format.pandoc[kOutputFile] = outputFile(
67
nbAbsPath,
68
);
69
resolved.recipe.output = resolved.recipe.format.pandoc[kOutputFile];
70
resolved.recipe.format.pandoc.to = to;
71
resolved.recipe.format.pandoc[kTemplate] = subarticleTemplatePath;
72
73
// Configure echo for this rendering
74
resolved.recipe.format.execute.echo = false;
75
resolved.recipe.format.execute.warning = false;
76
resolved.recipe.format.render[kKeepHidden] = true;
77
resolved.recipe.format.metadata[kClearHiddenClasses] = "all";
78
resolved.recipe.format.metadata[kRemoveHidden] = "none";
79
80
// If this recipe is using a a source notebook, clear the cell options
81
// from the output when rendering
82
if (resolved.recipe.format.render[kIpynbProduceSourceNotebook]) {
83
resolved.recipe.format.render[kClearCellOptions] = true;
84
}
85
86
return resolved;
87
}
88
async function renderJats(
89
nbPath: string,
90
format: Format,
91
subArticleToken: string,
92
services: RenderServices,
93
_notebookMetadata: NotebookMetadata | undefined,
94
project: ProjectContext,
95
): Promise<RenderedFile> {
96
const to = format.render[kVariant]?.includes("+element_citations")
97
? "jats+element_citations"
98
: "jats";
99
100
const rendered = await renderFile(
101
{ path: nbPath, formats: ["jats"] },
102
{
103
services,
104
flags: {
105
metadata: {
106
[kTo]: to,
107
[kLintXml]: false,
108
[kJatsSubarticle]: true,
109
[kJatsSubarticleId]: subArticleToken,
110
[kOutputFile]: outputFile(nbPath),
111
[kTemplate]: subarticleTemplatePath,
112
[kNotebookPreserveCells]: true,
113
},
114
quiet: false,
115
},
116
echo: true,
117
warning: true,
118
quietPandoc: true,
119
},
120
services,
121
project,
122
);
123
124
// An error occurred rendering this subarticle
125
if (rendered.error) {
126
error("Rendering of subarticle produced an unexpected result");
127
throw (rendered.error);
128
}
129
130
// There should be only one file
131
if (rendered.files.length !== 1) {
132
throw new InternalError(
133
`Rendering a JATS subarticle should only result in a single file. This attempt resulted in ${rendered.files.length} file(s).`,
134
);
135
}
136
137
return rendered.files[0];
138
}
139
140
function jatsOutputFile(nbAbsPath: string) {
141
const [_dir, stem] = dirAndStem(nbAbsPath);
142
return `${stem}.subarticle.xml`;
143
}
144
145