-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathnotebook-contributor-qmd.ts
163 lines (146 loc) · 4.73 KB
/
notebook-contributor-qmd.ts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* notebook-contributor-qmd.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 {
kClearHiddenClasses,
kIpynbProduceSourceNotebook,
kIPynbTitleBlockTemplate,
kKeepHidden,
kNotebookPreserveCells,
kOutputFile,
kRemoveHidden,
kTo,
kUnrollMarkdownCells,
} from "../../config/constants.ts";
import { InternalError } from "../../core/lib/error.ts";
import { dirAndStem } from "../../core/path.ts";
import { ProjectContext } from "../../project/types.ts";
import {
NotebookContributor,
NotebookMetadata,
NotebookOutput,
} 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 { ipynbTitleTemplatePath } from "../../format/ipynb/format-ipynb.ts";
import { projectScratchPath } from "../../project/project-scratch.ts";
import { ensureDirSync, existsSync } from "fs/mod.ts";
import { dirname, join, relative } from "../../deno_ral/path.ts";
export const qmdNotebookContributor: NotebookContributor = {
resolve: resolveOutputNotebook,
render: renderOutputNotebook,
outputFile,
cache,
cachedPath,
};
function cache(output: NotebookOutput, project?: ProjectContext) {
if (project) {
// copy the embed into the scratch directory
const path = cachePath(output.path, project);
ensureDirSync(dirname(path));
Deno.copyFileSync(output.path, path);
}
}
function cachedPath(nbAbsPath: string, project?: ProjectContext) {
if (project) {
// see if the embed exists in the scratch directory
const output = outputFile(nbAbsPath);
const outputPath = join(dirname(nbAbsPath), output);
const path = cachePath(outputPath, project);
if (existsSync(path)) {
return path;
}
}
}
function cachePath(nbAbsPath: string, project: ProjectContext) {
const basePath = projectScratchPath(project.dir, "embed");
const outputRel = relative(project.dir, nbAbsPath);
return join(basePath, outputRel);
}
function outputFile(
nbAbsPath: string,
): string {
return ipynbOutputFile(nbAbsPath);
}
function resolveOutputNotebook(
nbAbsPath: string,
_token: string,
executedFile: ExecutedFile,
_notebookMetadata?: NotebookMetadata,
) {
const resolved = ld.cloneDeep(executedFile);
resolved.recipe.format.pandoc[kOutputFile] = ipynbOutputFile(nbAbsPath);
resolved.recipe.output = resolved.recipe.format.pandoc[kOutputFile];
resolved.recipe.format.pandoc.to = "ipynb";
// TODO: Allow YAML to pass through as raw or markdown block
const template = ipynbTitleTemplatePath();
// 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.render[kNotebookPreserveCells] = true;
resolved.recipe.format.metadata[kClearHiddenClasses] = "all";
resolved.recipe.format.metadata[kRemoveHidden] = "none";
resolved.recipe.format.metadata[kIPynbTitleBlockTemplate] = template;
resolved.recipe.format.render[kIpynbProduceSourceNotebook] = true;
resolved.recipe.format.pandoc.citeproc = false;
// Configure markdown behavior for this rendering
resolved.recipe.format.metadata[kUnrollMarkdownCells] = false;
return resolved;
}
async function renderOutputNotebook(
nbPath: string,
_format: Format,
_subArticleToken: string,
services: RenderServices,
_notebookMetadata: NotebookMetadata | undefined,
project: ProjectContext,
): Promise<RenderedFile> {
const rendered = await renderFile(
{ path: nbPath, formats: ["ipynb"] },
{
services,
flags: {
metadata: {
[kTo]: "ipynb",
[kOutputFile]: ipynbOutputFile(nbPath),
[kNotebookPreserveCells]: true,
[kIpynbProduceSourceNotebook]: true,
citeproc: false,
},
quiet: false,
},
echo: true,
warning: true,
quietPandoc: true,
},
services,
project,
false, // Don't enforce project constraints on format since this is an intermediary rendering
);
// An error occurred rendering this subarticle
if (rendered.error) {
error("Rendering of qmd notebook produced an unexpected result");
throw (rendered.error);
}
// There should be only one file
if (rendered.files.length !== 1) {
throw new InternalError(
`Rendering an qmd notebook should only result in a single file. This attempt resulted in ${rendered.files.length} file(s).`,
);
}
return rendered.files[0];
}
function ipynbOutputFile(nbAbsPath: string) {
const [_dir, stem] = dirAndStem(nbAbsPath);
return `${stem}.embed.ipynb`;
}