-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathnotebook-contributor-jats.ts
More file actions
146 lines (131 loc) · 4.03 KB
/
notebook-contributor-jats.ts
File metadata and controls
146 lines (131 loc) · 4.03 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* notebook-contributor-jats.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { renderFile } from "../../command/render/render-files.ts";
import {
ExecutedFile,
RenderedFile,
RenderServices,
} from "../../command/render/types.ts";
import {
kClearCellOptions,
kClearHiddenClasses,
kIpynbProduceSourceNotebook,
kJatsSubarticleId,
kKeepHidden,
kNotebookPreserveCells,
kOutputFile,
kRemoveHidden,
kTemplate,
kTo,
kVariant,
} from "../../config/constants.ts";
import { InternalError } from "../../core/lib/error.ts";
import { dirAndStem } from "../../core/path.ts";
import {
kJatsSubarticle,
kLintXml,
} from "../../format/jats/format-jats-types.ts";
import { subarticleTemplatePath } from "../../format/jats/format-jats-paths.ts";
import { ProjectContext } from "../../project/types.ts";
import { NotebookContributor, NotebookMetadata } from "./notebook-types.ts";
import * as ld from "../../core/lodash.ts";
import { error } from "../../deno_ral/log.ts";
import { Format } from "../../config/types.ts";
import { safeCloneDeep } from "../../core/safe-clone-deep.ts";
export const jatsContributor: NotebookContributor = {
resolve: resolveJats,
render: renderJats,
outputFile,
};
function outputFile(
nbAbsPath: string,
): string {
return jatsOutputFile(nbAbsPath);
}
function resolveJats(
nbAbsPath: string,
token: string,
executedFile: ExecutedFile,
_notebookMetadata?: NotebookMetadata,
) {
const resolved = safeCloneDeep(executedFile);
const to =
resolved.recipe.format.render[kVariant]?.includes("+element_citations")
? "jats+element_citations"
: "jats";
resolved.recipe.format.metadata[kLintXml] = false;
resolved.recipe.format.metadata[kJatsSubarticle] = true;
resolved.recipe.format.metadata[kJatsSubarticleId] = token;
resolved.recipe.format.pandoc[kOutputFile] = outputFile(
nbAbsPath,
);
resolved.recipe.output = resolved.recipe.format.pandoc[kOutputFile];
resolved.recipe.format.pandoc.to = to;
resolved.recipe.format.pandoc[kTemplate] = subarticleTemplatePath;
// Configure echo for this rendering
resolved.recipe.format.execute.echo = false;
resolved.recipe.format.execute.warning = false;
resolved.recipe.format.render[kKeepHidden] = true;
resolved.recipe.format.metadata[kClearHiddenClasses] = "all";
resolved.recipe.format.metadata[kRemoveHidden] = "none";
// If this recipe is using a a source notebook, clear the cell options
// from the output when rendering
if (resolved.recipe.format.render[kIpynbProduceSourceNotebook]) {
resolved.recipe.format.render[kClearCellOptions] = true;
}
return resolved;
}
async function renderJats(
nbPath: string,
format: Format,
subArticleToken: string,
services: RenderServices,
_notebookMetadata: NotebookMetadata | undefined,
project: ProjectContext,
): Promise<RenderedFile> {
const to = format.render[kVariant]?.includes("+element_citations")
? "jats+element_citations"
: "jats";
const rendered = await renderFile(
{ path: nbPath, formats: ["jats"] },
{
services,
flags: {
metadata: {
[kTo]: to,
[kLintXml]: false,
[kJatsSubarticle]: true,
[kJatsSubarticleId]: subArticleToken,
[kOutputFile]: outputFile(nbPath),
[kTemplate]: subarticleTemplatePath,
[kNotebookPreserveCells]: true,
},
quiet: false,
},
echo: true,
warning: true,
quietPandoc: true,
},
services,
project,
);
// An error occurred rendering this subarticle
if (rendered.error) {
error("Rendering of subarticle produced an unexpected result");
throw (rendered.error);
}
// There should be only one file
if (rendered.files.length !== 1) {
throw new InternalError(
`Rendering a JATS subarticle should only result in a single file. This attempt resulted in ${rendered.files.length} file(s).`,
);
}
return rendered.files[0];
}
function jatsOutputFile(nbAbsPath: string) {
const [_dir, stem] = dirAndStem(nbAbsPath);
return `${stem}.subarticle.xml`;
}