Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/command/convert/cmd.ts
3583 views
1
/*
2
* cmd.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { existsSync } from "../../deno_ral/fs.ts";
8
import { join } from "../../deno_ral/path.ts";
9
import { info } from "../../deno_ral/log.ts";
10
11
import { Command } from "cliffy/command/mod.ts";
12
import { isJupyterNotebook } from "../../core/jupyter/jupyter.ts";
13
import { dirAndStem } from "../../core/path.ts";
14
import {
15
jupyterNotebookToMarkdown,
16
markdownToJupyterNotebook,
17
} from "./jupyter.ts";
18
import { initYamlIntelligenceResourcesFromFilesystem } from "../../core/schema/utils.ts";
19
20
const kNotebookFormat = "notebook";
21
const kMarkdownFormat = "markdown";
22
23
export const convertCommand = new Command()
24
.name("convert")
25
.arguments("<input:string>")
26
.description(
27
"Convert documents to alternate representations.",
28
)
29
.option(
30
"-o, --output [path:string]",
31
"Write output to PATH.",
32
)
33
.option(
34
"--with-ids",
35
"Include ids in conversion",
36
)
37
.example(
38
"Convert notebook to markdown",
39
"quarto convert mydocument.ipynb ",
40
)
41
.example(
42
"Convert markdown to notebook",
43
"quarto convert mydocument.qmd",
44
)
45
.example(
46
"Convert notebook to markdown, writing to file",
47
"quarto convert mydocument.ipynb --output mydoc.qmd",
48
)
49
// deno-lint-ignore no-explicit-any
50
.action(async (options: any, input: string) => {
51
await initYamlIntelligenceResourcesFromFilesystem();
52
53
if (!existsSync(input)) {
54
throw new Error(`File not found: '${input}'`);
55
}
56
57
// determine source format
58
const srcFormat = isJupyterNotebook(input)
59
? kNotebookFormat
60
: kMarkdownFormat;
61
62
// are we converting ids?
63
const withIds = options.withIds === undefined ? false : !!options.withIds;
64
65
// perform conversion
66
const converted = srcFormat === kNotebookFormat
67
? await jupyterNotebookToMarkdown(input, withIds)
68
: await markdownToJupyterNotebook(input, withIds);
69
70
// write output
71
const [dir, stem] = dirAndStem(input);
72
let output = options.output;
73
if (!output) {
74
output = join(
75
dir,
76
stem + (srcFormat === kMarkdownFormat ? ".ipynb" : ".qmd"),
77
);
78
}
79
Deno.writeTextFileSync(output, converted);
80
info(`Converted to ${output}`);
81
});
82
83