Skip to content

Commit 766f591

Browse files
authored
New Version 1.11.0
New Version 1.11.0
2 parents 59c7409 + e7c727f commit 766f591

18 files changed

Lines changed: 184 additions & 34 deletions

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77

8+
## [1.11.0]
9+
### Added
10+
- Support for OCaml (Thanks to @nieomylnieja)
11+
- Support for Swift (Thanks to @ihomway)
12+
13+
### Changed
14+
- Improved support for C compiler gcc (Thanks to @melo-afk)
15+
16+
817
## [1.10.0]
918
### Added
1019
- Support for zig (Thanks to @slar)

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The result is shown only after the execution is finished. It is not possible to
1414
![Video that shows how the plugin works.](https://github.com/twibiral/obsidian-execute-code/blob/master/images/execute_code_example.gif?raw=true)
1515

1616

17-
The following [languages are supported](#supported-programming-languages-): C, CPP, Dart, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Ruby, Wolfram Mathematica, Haskell, Scala, Racket, F#, Batch, Shell & Powershell, Octave, Maxima and Zig.
17+
The following [languages are supported](#supported-programming-languages-): C, CPP, Dart, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Ruby, Wolfram Mathematica, Haskell, Scala, Racket, F#, Batch, Shell & Powershell, Octave, Maxima, Zig and OCaml.
1818

1919

2020
Python, Rust, and Octave support embedded plots. All languages support ["magic" commands](#magic-commands-) that help you to access paths in obsidian or show images in your notes.
@@ -32,9 +32,15 @@ Take a look at the [changelog](CHANGELOG.md) to see what has changed in recent v
3232

3333

3434
## Featured In
35-
[![Video by I Versus AI](https://img.youtube.com/vi/eQz4eAW3ZDk/0.jpg)](https://www.youtube.com/watch?v=eQz4eAW3ZDk)
3635

37-
"Escape ChatGPT. Make your own Code Interpreter EASY" by I Versus AI
36+
| [![Video by I Versus AI](https://img.youtube.com/vi/eQz4eAW3ZDk/0.jpg)](https://www.youtube.com/watch?v=eQz4eAW3ZDk) | ![![Video by Michel's Science Speedrun](https://www.youtube.com/watch?v=w7vyavrMYqw)](https://img.youtube.com/vi/w7vyavrMYqw/0.jpg) |
37+
|---|---|
38+
| "Escape ChatGPT. Make your own Code Interpreter EASY" by _I Versus AI_ | "Obsidian & quarto integration" by _Michel's Science Speedrun_ |
39+
40+
In blogs:
41+
- ["Using Obsidian: Coding Notes" by _Kera Cudmore_](https://www.codu.co/articles/using-obsidian-coding-notes-pqjyljkh)
42+
43+
<small>Are you featuring this plugin in your content? Let me know and I will add it here.</small>
3844

3945

4046
## Supported programming languages 💻
@@ -446,6 +452,15 @@ plot2d(sin(x), [x,0,%pi]);
446452

447453
</details>
448454

455+
<details>
456+
<summary>OCaml</summary>
457+
458+
- Requirements: OCaml is installed and the correct path is set in the settings.
459+
460+
```ocaml
461+
print_endline "Hello, OCaml!"
462+
</details>
463+
449464
<details>
450465
<summary>Swift</summary>
451466

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "execute-code",
33
"name": "Execute Code",
4-
"version": "1.10.0",
4+
"version": "1.11.0",
55
"minAppVersion": "1.2.8",
66
"description": "Allows to execute code snippets within a note. Supported programming languages: C, CPP, Dart, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Ruby, Wolfram Mathematica, Haskell, Scala, Racket, F#, Batch, Shell & Powershell.",
77
"author": "twibiral",

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "execute-code",
3-
"version": "1.10.0",
3+
"version": "1.11.0",
44
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
55
"main": "src/main.js",
66
"scripts": {

src/executors/CExecutor.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export default class CExecutor extends ClingExecutor {
99
}
1010

1111
override run(codeBlockContent: string, outputter: Outputter, cmd: string, cmdArgs: string, ext: string) {
12-
return super.run(codeBlockContent, outputter, cmd, `-x c ${cmdArgs}`, "cpp");
12+
const install_path = this.settings[`clingPath`];
13+
if (install_path.endsWith("cling") || install_path.endsWith("cling.exe")) {
14+
return super.run(codeBlockContent, outputter, cmd, this.settings[`cArgs`], "cpp");
15+
} else {
16+
return super.run(codeBlockContent, outputter, cmd, this.settings[`cArgs`], "c");
17+
}
1318
}
1419
}

src/executors/ClingExecutor.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ export default abstract class ClingExecutor extends NonInteractiveCodeExecutor {
1313
}
1414

1515
override run(codeBlockContent: string, outputter: Outputter, cmd: string, args: string, ext: string) {
16-
1716
// Run code with a main block
1817
if (this.settings[`${this.language}UseMain`]) {
1918
// Generate a new temp file id and don't set to undefined to super.run() uses the same file id
2019
this.getTempFile(ext);
21-
// Cling expects the main function to have the same name as the file
22-
const code = codeBlockContent.replace(/main\(\)/g, `temp_${this.tempFileId}()`);
23-
20+
// Cling expects the main function to have the same name as the file / the extension is only c when gcc is used
21+
let code: string;
22+
if (ext != "c") {
23+
code = codeBlockContent.replace(/main\(\)/g, `temp_${this.tempFileId}()`);
24+
} else {
25+
code = codeBlockContent;
26+
}
2427
return super.run(code, outputter, this.settings.clingPath, args, ext);
2528
}
2629

27-
// Run code without a main block
30+
// Run code without a main block (cling only)
2831
return new Promise<void>((resolve, reject) => {
2932
const childArgs = [...args.split(" "), ...codeBlockContent.split("\n")];
3033
const child = child_process.spawn(this.settings.clingPath, childArgs, {env: process.env, shell: this.usesShell});

src/executors/Executor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ export default abstract class Executor extends EventEmitter {
6363
protected getTempFile(ext: string) {
6464
if (this.tempFileId === undefined)
6565
this.tempFileId = Date.now().toString();
66-
return `${os.tmpdir()}/temp_${this.tempFileId}.${ext}`;
66+
return `${os.tmpdir()}\\temp_${this.tempFileId}.${ext}`;
6767
}
6868
}

src/executors/NonInteractiveCodeExecutor.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {Outputter} from "src/Outputter";
66
import {LanguageId} from "src/main";
77
import { ExecutorSettings } from "../settings/Settings.js";
88
import windowsPathToWsl from "../transforms/windowsPathToWsl.js";
9+
import { error } from "console";
910

1011
export default class NonInteractiveCodeExecutor extends Executor {
1112
usesShell: boolean
@@ -44,16 +45,38 @@ export default class NonInteractiveCodeExecutor extends Executor {
4445
} else {
4546
args.push(tempFileName);
4647
}
47-
48-
const child = child_process.spawn(cmd, args, {env: process.env, shell: this.usesShell});
49-
50-
this.handleChildOutput(child, outputter, tempFileName).then(() => {
51-
this.tempFileId = undefined; // Reset the file id to use a new file next time
52-
});
48+
49+
50+
let child: child_process.ChildProcessWithoutNullStreams;
51+
52+
// check if compiled by gcc
53+
if (cmd.endsWith("gcc") || cmd.endsWith("gcc.exe")) {
54+
// remove .c from tempFileName and add .out for the compiled output and add output path to args
55+
const tempFileNameWExe: string = tempFileName.slice(0, -2) + ".out";
56+
args.push("-o", tempFileNameWExe);
57+
58+
// compile c file with gcc and handle possible output
59+
const childGCC = child_process.spawn(cmd, args, {env: process.env, shell: this.usesShell});
60+
this.handleChildOutput(childGCC, outputter, tempFileName);
61+
childGCC.on('exit', (code) => {
62+
if (code === 0) {
63+
// executing the compiled file
64+
child = child_process.spawn(tempFileNameWExe, { env: process.env, shell: this.usesShell });
65+
this.handleChildOutput(child, outputter, tempFileNameWExe).then(() => {
66+
this.tempFileId = undefined; // Reset the file id to use a new file next time
67+
});
68+
}
69+
});
70+
} else {
71+
child = child_process.spawn(cmd, args, { env: process.env, shell: this.usesShell });
72+
this.handleChildOutput(child, outputter, tempFileName).then(() => {
73+
this.tempFileId = undefined; // Reset the file id to use a new file next time
74+
});
75+
}
5376

5477
// We don't resolve the promise here - 'handleChildOutput' registers a listener
5578
// For when the child_process closes, and will resolve the promise there
56-
this.resolveRun = resolve;
79+
this.resolveRun = resolve;
5780
}).catch((err) => {
5881
this.notifyError(cmd, cmdArgs, tempFileName, err, outputter);
5982
resolve();

src/main.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import ExecutorManagerView, {
2323

2424
import runAllCodeBlocks from './runAllCodeBlocks';
2525

26-
export const languageAliases = ["javascript", "typescript", "bash", "csharp", "wolfram", "nb", "wl", "hs", "py", "scpt"] as const;
26+
export const languageAliases = ["javascript", "typescript", "bash", "csharp", "wolfram", "nb", "wl", "hs", "py"] as const;
2727
export const canonicalLanguages = ["js", "ts", "cs", "lean", "lua", "python", "cpp", "prolog", "shell", "groovy", "r",
28-
"go", "rust", "java", "powershell", "kotlin", "mathematica", "haskell", "scala", "racket", "fsharp", "c", "dart",
29-
"ruby", "batch", "sql", "octave", "maxima", "applescript", "zig"] as const;
28+
"go", "rust", "java", "powershell", "kotlin", "mathematica", "haskell", "scala", "swift", "racket", "fsharp", "c", "dart",
29+
"ruby", "batch", "sql", "octave", "maxima", "applescript", "zig", "ocaml"] as const;
3030
export const supportedLanguages = [...languageAliases, ...canonicalLanguages] as const;
3131
export type LanguageId = typeof canonicalLanguages[number];
3232

@@ -353,6 +353,13 @@ export default class ExecuteCodePlugin extends Plugin {
353353
const transformedCode = await new CodeInjector(this.app, this.settings, language).injectCode(srcCode);
354354
this.runCodeInShell(transformedCode, out, button, this.settings.scalaPath, this.settings.scalaArgs, this.settings.scalaFileExtension, language, file);
355355
});
356+
} else if (language === "swift") {
357+
button.addEventListener("click", async () => {
358+
button.className = runButtonDisabledClass;
359+
const transformedCode = await new CodeInjector(this.app, this.settings, language).injectCode(srcCode);
360+
this.runCodeInShell(transformedCode, out, button, this.settings.swiftPath, this.settings.swiftArgs, this.settings.swiftFileExtension, language, file);
361+
});
362+
356363
} else if (language === "c") {
357364
button.addEventListener("click", async () => {
358365
button.className = runButtonDisabledClass;
@@ -403,6 +410,12 @@ export default class ExecuteCodePlugin extends Plugin {
403410
const transformedCode = await new CodeInjector(this.app, this.settings, language).injectCode(srcCode);
404411
this.runCodeInShell(transformedCode, out, button, this.settings.zigPath, this.settings.zigArgs, "zig", language, file);
405412
})
413+
} else if (language === "ocaml") {
414+
button.addEventListener("click", async () => {
415+
button.className = runButtonDisabledClass;
416+
const transformedCode = await new CodeInjector(this.app, this.settings, language).injectCode(srcCode);
417+
this.runCodeInShell(transformedCode, out, button, this.settings.ocamlPath, this.settings.ocamlArgs, "ocaml", language, file);
418+
})
406419
}
407420

408421
}

0 commit comments

Comments
 (0)