-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbconvert-shim.ts
executable file
·50 lines (41 loc) · 1.41 KB
/
nbconvert-shim.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
#!/usr/bin/env bun
/**
* nbconvert-shim.ts
*
* Helper tool to speed up testing, mimicking `nbconvert`.
* Used by `files-to-prompt.test.ts`.
*/
import fs from 'node:fs';
import path from 'node:path';
function writeAsciidoc(filename: string): void {
const content = `+*In[1]:*+\n[source, ipython3]\n----\nprint('Hello, World!')\n----\n`;
const outputPath = path.join(path.dirname(filename), `${path.basename(filename, path.extname(filename))}.asciidoc`);
fs.writeFileSync(outputPath, content);
}
function writeMarkdown(filename: string): void {
const content = `\`\`\`python\nprint('Hello, World!')\n\`\`\`\n`;
const outputPath = path.join(path.dirname(filename), `${path.basename(filename, path.extname(filename))}.md`);
fs.writeFileSync(outputPath, content);
}
if (import.meta.main) {
const args = process.argv.slice(2);
// needed to satisfy test for existence of nbconvert command
if (args[0] === '--version') {
console.error('nbconvert-shim v0.0.0');
process.exit(0);
}
if (args.length < 3) {
console.error('Usage: nbconvert-shim --to asciidoc|markdown <filename>');
process.exit(1);
}
const outputFormat = args[1];
const filename = args[2];
if (outputFormat === 'asciidoc') {
writeAsciidoc(filename);
} else if (outputFormat === 'markdown') {
writeMarkdown(filename);
} else {
console.error(`Unknown output format: ${outputFormat}`);
process.exit(1);
}
}